From 7c01aaf913705a842050844832f65e52c157e3f9 Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Thu, 8 Apr 2010 19:34:57 +0200 Subject: [PATCH] added logging to a file --- asteroids/main.cc | 1 + engine/Logging.cc | 34 ++++++++++++++++++++++++++++++++++ engine/Logging.h | 2 ++ engine/LoggingGlobal.h | 2 ++ 4 files changed, 39 insertions(+) diff --git a/asteroids/main.cc b/asteroids/main.cc index bd7038d..fb941b3 100644 --- a/asteroids/main.cc +++ b/asteroids/main.cc @@ -23,6 +23,7 @@ int main (int argc, char* argv[]) { engine.SetView (new asteroids::View); SetLogPrintLevel (Engine::LogLevelDebug); + Engine::SetLogFilename ("game.log"); if (engine.Init (argc, argv) != 0) { cout << "Could not start engine!" << endl; diff --git a/engine/Logging.cc b/engine/Logging.cc index e9a8479..23ca1fa 100644 --- a/engine/Logging.cc +++ b/engine/Logging.cc @@ -4,11 +4,13 @@ #include #include #include +#include namespace Engine { static Logging *LoggingInstance = NULL; static LogLevel requested_level = LOG_DEFAULT_LEVEL; +static std::string requested_logfilename(""); /* * Inherited Module functions @@ -16,6 +18,9 @@ static LogLevel requested_level = LOG_DEFAULT_LEVEL; int Logging::OnInit (int argc, char* argv[]) { mPrintLevel = requested_level; + if (requested_logfilename != "") + SetLogFilename (requested_logfilename.c_str()); + Log (LogLevelDebug, "Logging Init"); LoggingInstance = this; @@ -25,6 +30,9 @@ int Logging::OnInit (int argc, char* argv[]) { void Logging::OnDestroy () { Log (LogLevelDebug, "Logging Destroy"); + if (mLogFileOut) + mLogFileOut.close(); + LoggingInstance = NULL; } @@ -54,12 +62,29 @@ void Logging::Log (LogLevel level, const char *str, ...) { LogEntry log_entry (level, msg); mLogEntries.push_back (log_entry); + + if (mLogFileOut) { + time_t timer = time(NULL); + std::string timestr (asctime(localtime(&timer))); + mLogFileOut << timestr.substr(0, timestr.size() - 1) << ' ' << msg << std::endl; + } } void Logging::SetLogPrintLevel (LogLevel print_level) { mPrintLevel = print_level; } +void Logging::SetLogFilename (const char *filename) { + mLogFileOut.open (filename, std::ios_base::trunc); + + if (!mLogFileOut) { + LogError ("Could not open logfile %s for writing!", filename); + assert (0); + } + + requested_logfilename = ""; +}; + const LogEntry &Logging::GetLastEntry () { static LogEntry null_message (LogLevelMessage, ""); @@ -82,6 +107,15 @@ void SetLogPrintLevel (LogLevel print_level) { LoggingInstance->SetLogPrintLevel (print_level); } +void SetLogFilename (const char *filename) { + if (!LoggingInstance) { + requested_logfilename = filename; + return; + } + + LoggingInstance->SetLogFilename (filename); +} + void LogError (const char* str, ...) { assert (LoggingInstance); diff --git a/engine/Logging.h b/engine/Logging.h index 6bb401a..d029837 100644 --- a/engine/Logging.h +++ b/engine/Logging.h @@ -12,6 +12,7 @@ class Logging : public Module { public: void Log (LogLevel level, const char *str, ...); void SetLogPrintLevel (LogLevel print_level); + void SetLogFilename (const char *filename); /** \brief Returns the last LogEntry that was sent to the Logging module */ const LogEntry& GetLastEntry (); @@ -28,6 +29,7 @@ class Logging : public Module { * \todo Restrict the number of entries to be stored! */ std::vector mLogEntries; + std::ofstream mLogFileOut; }; } diff --git a/engine/LoggingGlobal.h b/engine/LoggingGlobal.h index ffe5322..468eb8f 100644 --- a/engine/LoggingGlobal.h +++ b/engine/LoggingGlobal.h @@ -28,6 +28,8 @@ struct LogEntry { /** \brief Sets the level for which messages should be printed out */ void SetLogPrintLevel (LogLevel print_level); +/** \brief Sets the filename to which all the logging is sent, set to NULL to disable logging */ +void SetLogFilename (const char *filename); /** \brief Sends the Message to the Logging system */ void LogError (const char* str, ...);