fysxasteroids/engine/Logging.cc

174 lines
3.3 KiB
C++

#include "Logging.h"
#include <iostream>
#include <cstdarg>
#include <cstdlib>
#include <assert.h>
namespace Engine {
static Logging *LoggingInstance = NULL;
static LogLevel requested_level = LOG_DEFAULT_LEVEL;
/*
* Inherited Module functions
*/
int Logging::OnInit (int argc, char* argv[]) {
mPrintLevel = requested_level;
Log (LogLevelDebug, "Logging Init");
LoggingInstance = this;
return 0;
}
void Logging::OnDestroy () {
Log (LogLevelDebug, "Logging Destroy");
LoggingInstance = NULL;
}
/*
* Module specific functions
*/
void Logging::Log (LogLevel level, const char *str, ...) {
if (level < mPrintLevel)
return;
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);
std::cout << msg << std::endl;
LogEntry log_entry (level, msg);
mLogEntries.push_back (log_entry);
}
void Logging::SetLogPrintLevel (LogLevel print_level) {
mPrintLevel = print_level;
}
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);
}
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 ();
}
}