fysxasteroids/engine/Logging.cc

208 lines
4.1 KiB
C++
Raw Normal View History

2010-04-05 23:38:59 +02:00
#include "Logging.h"
#include <iostream>
#include <cstdarg>
#include <cstdlib>
#include <assert.h>
2010-04-08 19:34:57 +02:00
#include <ctime>
2010-04-05 23:38:59 +02:00
namespace Engine {
static Logging *LoggingInstance = NULL;
static LogLevel requested_level = LOG_DEFAULT_LEVEL;
2010-04-08 19:34:57 +02:00
static std::string requested_logfilename("");
2010-04-05 23:38:59 +02:00
/*
* Inherited Module functions
*/
int Logging::OnInit (int argc, char* argv[]) {
mPrintLevel = requested_level;
2010-04-08 19:34:57 +02:00
if (requested_logfilename != "")
SetLogFilename (requested_logfilename.c_str());
2010-04-05 23:38:59 +02:00
Log (LogLevelDebug, "Logging Init");
LoggingInstance = this;
return 0;
}
void Logging::OnDestroy () {
Log (LogLevelDebug, "Logging Destroy");
2010-04-08 19:34:57 +02:00
if (mLogFileOut)
mLogFileOut.close();
2010-04-05 23:38:59 +02:00
LoggingInstance = NULL;
}
/*
* Module specific functions
*/
void Logging::Log (LogLevel level, const char *str, ...) {
static char msg[LOG_MAX_MESSAGE_LENGTH];
static int last_length = LOG_MAX_MESSAGE_LENGTH;
static int i;
for (i = 0; i < last_length; i++)
msg[i] = 0;
va_list ap;
va_start(ap, str);
vsprintf(msg, str, ap);
va_end(ap);
last_length = strlen (msg);
assert (last_length < LOG_MAX_MESSAGE_LENGTH);
2010-04-08 20:04:39 +02:00
if (level >= mPrintLevel)
std::cout << msg << std::endl;
2010-04-05 23:38:59 +02:00
LogEntry log_entry (level, msg);
mLogEntries.push_back (log_entry);
2010-04-08 19:34:57 +02:00
2010-04-08 20:04:39 +02:00
// write the logs to file
2010-04-08 19:34:57 +02:00
if (mLogFileOut) {
2010-04-08 20:04:39 +02:00
/// \TODO also write out the log level
2010-04-08 19:34:57 +02:00
time_t timer = time(NULL);
std::string timestr (asctime(localtime(&timer)));
mLogFileOut << timestr.substr(0, timestr.size() - 1) << ' ' << msg << std::endl;
}
2010-04-05 23:38:59 +02:00
}
void Logging::SetLogPrintLevel (LogLevel print_level) {
mPrintLevel = print_level;
}
2010-04-08 19:34:57 +02:00
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 = "";
};
2010-04-05 23:38:59 +02:00
const LogEntry &Logging::GetLastEntry () {
static LogEntry null_message (LogLevelMessage, "");
if (mLogEntries.size() > 0) {
return mLogEntries[mLogEntries.size() - 1];
}
return null_message;
}
/*
* Globally visible functions
*/
void SetLogPrintLevel (LogLevel print_level) {
if (!LoggingInstance) {
requested_level = print_level;
return;
}
LoggingInstance->SetLogPrintLevel (print_level);
}
2010-04-08 19:34:57 +02:00
void SetLogFilename (const char *filename) {
if (!LoggingInstance) {
requested_logfilename = filename;
return;
}
LoggingInstance->SetLogFilename (filename);
}
2010-04-05 23:38:59 +02:00
void LogError (const char* str, ...) {
assert (LoggingInstance);
static char msg[LOG_MAX_MESSAGE_LENGTH];
static int last_length = LOG_MAX_MESSAGE_LENGTH;
static int i;
for (i = 0; i < last_length; i++)
msg[i] = 0;
va_list ap;
va_start(ap, str);
vsprintf(msg, str, ap);
va_end(ap);
last_length = strlen (msg);
assert (last_length < LOG_MAX_MESSAGE_LENGTH);
LoggingInstance->Log (LogLevelError, msg);
}
void LogWarning (const char* str, ...) {
assert (LoggingInstance);
static char msg[LOG_MAX_MESSAGE_LENGTH];
static int last_length = LOG_MAX_MESSAGE_LENGTH;
static int i;
for (i = 0; i < last_length; i++)
msg[i] = 0;
va_list ap;
va_start(ap, str);
vsprintf(msg, str, ap);
va_end(ap);
last_length = strlen (msg);
assert (last_length < LOG_MAX_MESSAGE_LENGTH);
LoggingInstance->Log (LogLevelWarning, msg);
}
void LogMessage (const char* str, ...) {
assert (LoggingInstance);
static char msg[LOG_MAX_MESSAGE_LENGTH];
static int last_length = LOG_MAX_MESSAGE_LENGTH;
static int i;
for (i = 0; i < last_length; i++)
msg[i] = 0;
va_list ap;
va_start(ap, str);
vsprintf(msg, str, ap);
va_end(ap);
last_length = strlen (msg);
assert (last_length < LOG_MAX_MESSAGE_LENGTH);
LoggingInstance->Log (LogLevelMessage, msg);
}
void LogDebug (const char* str, ...) {
assert (LoggingInstance);
static char msg[LOG_MAX_MESSAGE_LENGTH];
static int last_length = LOG_MAX_MESSAGE_LENGTH;
static int i;
for (i = 0; i < last_length; i++)
msg[i] = 0;
va_list ap;
va_start(ap, str);
vsprintf(msg, str, ap);
va_end(ap);
last_length = strlen (msg);
assert (last_length < LOG_MAX_MESSAGE_LENGTH);
LoggingInstance->Log (LogLevelDebug, msg);
}
const LogEntry& GetLastLogEntry () {
assert (LoggingInstance);
return LoggingInstance->GetLastEntry ();
}
}