forked from mirror/openmw-tes3mp
[General] Modernize Log utility
* Reverse Log levels * Add LOG_TRACE * Spawn instance of Log in Get() function
This commit is contained in:
parent
d6dc75e94b
commit
916ada108f
3 changed files with 27 additions and 39 deletions
|
@ -153,8 +153,6 @@ int main(int argc, char *argv[])
|
|||
auto version = Version::getOpenmwVersion(variables["resources"].as<Files::EscapeHashString>().toStdString());
|
||||
|
||||
int logLevel = mgr.getInt("logLevel", "General");
|
||||
if (logLevel < Log::LOG_VERBOSE || logLevel > Log::LOG_FATAL)
|
||||
logLevel = Log::LOG_VERBOSE;
|
||||
|
||||
// Some objects used to redirect cout and cerr
|
||||
// Scope must be here, so this still works inside the catch block for logging exceptions
|
||||
|
|
|
@ -14,41 +14,25 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
Log *Log::sLog = nullptr;
|
||||
|
||||
Log::Log(int logLevel) : logLevel(logLevel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Log::Create(int logLevel)
|
||||
Log &Log::Get()
|
||||
{
|
||||
if (sLog != nullptr)
|
||||
return;
|
||||
sLog = new Log(logLevel);
|
||||
}
|
||||
|
||||
void Log::Delete()
|
||||
{
|
||||
if (sLog == nullptr)
|
||||
return;
|
||||
delete sLog;
|
||||
sLog = nullptr;
|
||||
}
|
||||
|
||||
const Log &Log::Get()
|
||||
{
|
||||
return *sLog;
|
||||
static Log instance(1000);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void Log::SetLevel(int level)
|
||||
{
|
||||
sLog->logLevel = level;
|
||||
logLevel = level;
|
||||
}
|
||||
|
||||
const char* getTime()
|
||||
{
|
||||
time_t t = time(0);
|
||||
time_t t = time(nullptr);
|
||||
struct tm *tm = localtime(&t);
|
||||
static char result[20];
|
||||
sprintf(result, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
|
||||
|
@ -59,7 +43,7 @@ const char* getTime()
|
|||
|
||||
void Log::print(int level, bool hasPrefix, const char *file, int line, const char *message, ...) const
|
||||
{
|
||||
if (level < logLevel) return;
|
||||
if (level > logLevel || logLevel == LOG_OFF) return;
|
||||
std::stringstream sstr;
|
||||
|
||||
if (hasPrefix)
|
||||
|
@ -67,7 +51,7 @@ void Log::print(int level, bool hasPrefix, const char *file, int line, const cha
|
|||
|
||||
sstr << "[" << getTime() << "] ";
|
||||
|
||||
if (file != 0 && line != 0)
|
||||
if (file != nullptr && line != 0)
|
||||
{
|
||||
sstr << "[" << file << ":";
|
||||
sstr << line << "] ";
|
||||
|
@ -85,6 +69,13 @@ void Log::print(int level, bool hasPrefix, const char *file, int line, const cha
|
|||
case LOG_FATAL:
|
||||
sstr << "FATAL";
|
||||
break;
|
||||
case LOG_TRACE:
|
||||
sstr << "TRACE";
|
||||
break;
|
||||
case LOG_VERBOSE:
|
||||
case LOG_INFO:
|
||||
sstr << "INFO";
|
||||
break;
|
||||
default:
|
||||
sstr << "INFO";
|
||||
}
|
||||
|
@ -108,10 +99,9 @@ void Log::print(int level, bool hasPrefix, const char *file, int line, const cha
|
|||
|
||||
string Log::getFilenameTimestamp()
|
||||
{
|
||||
time_t rawtime = time(0);
|
||||
time_t rawtime = time(nullptr);
|
||||
struct tm *timeinfo = localtime(&rawtime);
|
||||
char buffer[25];
|
||||
strftime(buffer, 25, "%Y-%m-%d-%H_%M_%S", timeinfo);
|
||||
std::string timestamp(buffer);
|
||||
return timestamp;
|
||||
return string(buffer);
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
#define LOG_MESSAGE(level, msg, ...)
|
||||
#define LOG_MESSAGE_SIMPLE(level, msg, ...)
|
||||
#else
|
||||
#define LOG_INIT(logLevel) Log::Create(logLevel)
|
||||
#define LOG_QUIT() Log::Delete()
|
||||
#define LOG_INIT(logLevel) Log::Get().SetLevel(logLevel)
|
||||
#define LOG_QUIT()
|
||||
#if defined(_MSC_VER)
|
||||
#define LOG_MESSAGE(level, msg, ...) Log::Get().print((level), (1), (__FILE__), (__LINE__), (msg), __VA_ARGS__)
|
||||
#define LOG_MESSAGE_SIMPLE(level, msg, ...) Log::Get().print((level), (1), (0), (0), (msg), __VA_ARGS__)
|
||||
|
@ -35,16 +35,17 @@ class Log
|
|||
public:
|
||||
enum
|
||||
{
|
||||
LOG_VERBOSE = 0,
|
||||
LOG_INFO,
|
||||
LOG_WARN,
|
||||
LOG_OFF = 0,
|
||||
LOG_FATAL,
|
||||
LOG_ERROR,
|
||||
LOG_FATAL
|
||||
LOG_WARN,
|
||||
LOG_INFO,
|
||||
LOG_VERBOSE,
|
||||
LOG_TRACE,
|
||||
};
|
||||
static void Create(int logLevel);
|
||||
static void Delete();
|
||||
static const Log &Get();
|
||||
static void SetLevel(int level);
|
||||
|
||||
static Log &Get();
|
||||
void SetLevel(int level);
|
||||
void print(int level, bool hasPrefix, const char *file, int line, const char *message, ...) const;
|
||||
|
||||
static std::string getFilenameTimestamp();
|
||||
|
@ -53,7 +54,6 @@ public:
|
|||
Log &operator=(Log &) = delete;
|
||||
private:
|
||||
explicit Log(int logLevel);
|
||||
static Log *sLog;
|
||||
int logLevel;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue