From 21ad7eac963bc329395fa893300fb5c8f721fd77 Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Sun, 23 Oct 2011 04:10:55 +0300 Subject: Logging facility, Config class, tray icon, custom QApplication subclass --- client/Logger.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 client/Logger.cpp (limited to 'client/Logger.cpp') diff --git a/client/Logger.cpp b/client/Logger.cpp new file mode 100644 index 0000000..4cbb8aa --- /dev/null +++ b/client/Logger.cpp @@ -0,0 +1,98 @@ + +#include +#include +#include +#include +#include "Logger.h" + + +LogLevelType Logger::LogLevel = TraceLevel; + +#ifdef DEBUG + +void Logger::Trace(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(TraceLevel, msgfmt, args); +} + +void Logger::Debug(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(DebugLevel, msgfmt, args); +} + +void Logger::Info(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(InfoLevel, msgfmt, args); +} + +void Logger::Warn(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(WarnLevel, msgfmt, args); +} + +void Logger::Error(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(ErrorLevel, msgfmt, args); +} + +void Logger::Fatal(char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(FatalLevel, msgfmt, args); +} + +void Logger::Log(LogLevelType logLevel, char* msgfmt, ...) +{ + va_list args; + va_start(args, msgfmt); + Logger::Log(logLevel, msgfmt, args); +} + +void Logger::Log(LogLevelType logLevel, char* msgfmt, va_list args) +{ + time_t rawtime = time(NULL); + struct tm *lctime = localtime(&rawtime); + char time_str[10] = {0}; + strftime(time_str, 10, "%H:%M:%S", lctime); + + char log_level_str[6] = {0}; + switch (logLevel) + { + default: + case TraceLevel: + strcpy(log_level_str, "Trace"); + break; + case DebugLevel: + strcpy(log_level_str, "Debug"); + break; + case InfoLevel: + strcpy(log_level_str, "Info"); + break; + case WarnLevel: + strcpy(log_level_str, "Warn"); + break; + case ErrorLevel: + strcpy(log_level_str, "Error"); + break; + case FatalLevel: + strcpy(log_level_str, "Fatal"); + break; + } + + char *newfmt = new char[strlen(msgfmt) + 24]; + sprintf(newfmt, "[%s] %-6s: %s", time_str, log_level_str, msgfmt); + vfprintf(stderr, newfmt, args); +} + +#endif \ No newline at end of file -- cgit v1.2.3