RtB/src/Log.cpp

59 lines
1.4 KiB
C++

/* ================================================================
Log
----------------
This file defines the class for Log.
================================================================ */
#include "Log.hpp"
#ifdef __ANDROID__
#include <jni.h>
#include <android/log.h>
#endif
void (*logHook)(const char*, const char*) = NULL;
Log::Log() {}
std::ostringstream& Log::Get(LogLevel level) {
l = level;
time_t current_time = time(0);
struct tm *ltm;
#ifdef _MSC_VER
struct tm lltm;
localtime_s(&lltm, &current_time);
ltm = &lltm;
#else
ltm = localtime(&current_time);
#endif
os << 1900+ltm->tm_year << "/" << 1+ltm->tm_mon << "/" << ltm->tm_mday << " " << ltm->tm_hour << ":" << ltm->tm_min << ':' << ltm->tm_sec;
os << std::endl;
return os;
}
Log::~Log() {
os << std::endl;
char const *title = "Undefined";
switch(l) {
case LOG_INFO:
title = "Info";
break;
case LOG_ERROR:
title = "Error";
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, os.str().c_str(), NULL);
break;
case LOG_WARNING:
title = "Warning";
break;
case LOG_DEBUG:
title = "Debug";
break;
}
if (logHook != NULL) {
logHook(title, os.str().c_str());
}
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, "RtB", "%s: %s", title, os.str().c_str());
#else
fprintf(stderr, "%s: %s\n", title, os.str().c_str());
fflush(stderr);
#endif
}