added logging to a file

main
Martin Felis (berta) 2010-04-08 19:34:57 +02:00
parent 567bb432ef
commit 7c01aaf913
4 changed files with 39 additions and 0 deletions

View File

@ -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;

View File

@ -4,11 +4,13 @@
#include <cstdarg>
#include <cstdlib>
#include <assert.h>
#include <ctime>
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);

View File

@ -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<LogEntry> mLogEntries;
std::ofstream mLogFileOut;
};
}

View File

@ -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, ...);