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 +++++++++++++++++++++++++++++++++++----------- src/dedicated/console.h | 23 ++++++++------- src/dedicated/dedicated.cc | 14 +++++---- 3 files changed, 77 insertions(+), 32 deletions(-) (limited to 'src/dedicated') 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()) diff --git a/src/dedicated/console.h b/src/dedicated/console.h index b0bafdb..2b00c05 100644 --- a/src/dedicated/console.h +++ b/src/dedicated/console.h @@ -18,6 +18,9 @@ public: Console(); ~Console(); + /// dump console content to cout + void open_log(); + /// initialize the server console static void init(); /// shutdown the server console @@ -40,11 +43,10 @@ protected: void draw_text(); /// draw the console input (ncurses) void draw_input(); - /// dump console content to cout - void dump(); - /// print one line of text (do nothing) +#endif + /// print one line of text virtual void print(const std::string & text); - +#ifdef HAVE_CURSES private: typedef std::deque History; @@ -52,15 +54,16 @@ private: void set_color(const char *color_code); // input history - History history; - History::reverse_iterator history_pos; + History history; + History::reverse_iterator history_pos; - size_t input_pos; - size_t console_scroll; + size_t input_pos; + size_t console_scroll; - int console_width; - int console_height; + int console_width; + int console_height; #endif + std::string console_logfilename; }; Console *console(); diff --git a/src/dedicated/dedicated.cc b/src/dedicated/dedicated.cc index 63a1f43..adaba15 100644 --- a/src/dedicated/dedicated.cc +++ b/src/dedicated/dedicated.cc @@ -18,11 +18,12 @@ namespace dedicated void run(int count, char **arguments) { - std::cout << core::name() << " " << core::version() << std::endl; - - for (int i = 0; i < count; i++) - std::cout << arguments[i] << " "; - std::cout << std::endl; + + con_print << " command line "; + for (int i = 0; i < count; i++) { + con_print << arguments[i] << " "; + } + con_print << std::endl; Dedicated app; app.init(count, arguments); @@ -41,6 +42,9 @@ void Dedicated::init(int count, char **arguments) core::Cvar::set("sv_dedicated", "1", core::Cvar::ReadOnly); core::Application::init(count, arguments); + + // start logging + console()->open_log(); // the command line is in the buffer, execute it core::CommandBuffer::exec(); -- cgit v1.2.3