From 5d959c5ba476eb0b103fe5953cca69939a6d836a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 18 Nov 2012 21:54:47 +0000 Subject: Log dedicated server console messages to server.log, print version number and command line to console on startup. --- src/dedicated/console.cc | 72 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 17 deletions(-) (limited to 'src/dedicated/console.cc') diff --git a/src/dedicated/console.cc b/src/dedicated/console.cc index 0c05d9c..5d16848 100644 --- a/src/dedicated/console.cc +++ b/src/dedicated/console.cc @@ -11,12 +11,14 @@ #include #include +#include "sys/sys.h" #include "auxiliary/functions.h" #include "core/application.h" #include "core/core.h" #include "core/commandbuffer.h" #include "dedicated/console.h" #include "sys/consoleinterface.h" +#include "filesystem/filesystem.h" #ifdef HAVE_CURSES @@ -71,7 +73,7 @@ void Console::init() init_pair(7, COLOR_WHITE, -1); init_pair(8, -1, -1); } -#endif // HAVE_CURSES +#endif // HAVE_CURSES con_print << "^BInitializing console..." << std::endl; @@ -96,28 +98,72 @@ void Console::shutdown() server_console.draw(); endwin(); console_initialized = false; - - server_console.dump(); -#endif +#endif // HAVE_CURSES } Console::Console() { + } Console::~Console() { } -#ifdef HAVE_CURSES -void Console::dump() +void Console::open_log() { - // dump console content - for (Queue::iterator it = log().begin(); it != log().end(); it++) { - sys::ConsoleInterface::print((*it)); + console_logfilename.assign(filesystem::writedir()); + console_logfilename.append("server.log"); + + std::ofstream logfile; + logfile.open(console_logfilename.c_str(), std::ios::app); + if (!logfile.is_open()) { + std::string errfilename(console_logfilename); + console_logfilename.clear(); + con_warn << "Could not open logfile " << errfilename << std::endl; + } else { + logfile.close(); + + // dump console content + for (Queue::iterator it = log().begin(); it != log().end(); it++) { + print((*it)); + } } } +void Console::print(const std::string & text) +{ +#ifdef HAVE_CURSES + // at this point the text is already saved in the console buffering + // Because w're using curses, the actual printing is handled async + if (console_initialized && !rcon()) { + console_updated = true; + draw(); + } +#else + // use fallback print + sys::ConsoleInterface::print(text); +#endif // HAVE_CURSES + if (!rcon() && console_logfilename.size()) { + std::ofstream logfile; + logfile.open(console_logfilename.c_str(), std::ios::app); + if (logfile.is_open()) { + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, milliseconds = 0; + sys::get_localtime(year, month, day, hour, min, sec, milliseconds); + logfile << std::setw(4) << std::setfill('0') << year << "-" + << std::setw(2) << std::setfill('0') << month << "-" + << std::setw(2) << std::setfill('0') << day << " " + << std::setw(2) << std::setfill('0') << hour << ":" + << std::setw(2) << std::setfill('0') << min << ":" + << std::setw(2) << std::setfill('0') << sec << " " + << std::setw(2) << " " + << aux::text_strip(text) << std::endl; + logfile.close(); + } + } +} + +#ifdef HAVE_CURSES void Console::resize() { if (!console_initialized) @@ -129,14 +175,6 @@ void Console::resize() draw(); } -void Console::print(const std::string & text) -{ - if (console_initialized && !rcon()) { - console_updated = true; - draw(); - } -} - void Console::set_color(const char *color_code) { if (!has_colors()) -- cgit v1.2.3