// Copyright © 2010-2012 b0ris //. // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. //. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. //. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include #include #include "Logger.h" using std::string; LogLevelType Logger::LogLevel = TraceLevel; #ifdef DEBUG void Logger::Trace(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(TraceLevel, msg, args); } void Logger::Debug(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(DebugLevel, msg, args); } void Logger::Info(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(InfoLevel, msg, args); } void Logger::Warn(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(WarnLevel, msg, args); } void Logger::Error(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(ErrorLevel, msg, args); } void Logger::Fatal(string msg, ...) { va_list args; va_start(args, msg); Logger::Log(FatalLevel, msg, args); } void Logger::Log(LogLevelType logLevel, string msg, ...) { va_list args; va_start(args, msg); Logger::Log(logLevel, msg, args); } void Logger::Log(LogLevelType logLevel, string msg, 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); string log_level_str; switch (logLevel) { default: case TraceLevel: log_level_str = "Trace"; break; case DebugLevel: log_level_str = "Debug"; break; case InfoLevel: log_level_str = "Info"; break; case WarnLevel: log_level_str = "Warn"; break; case ErrorLevel: log_level_str = "Error"; break; case FatalLevel: log_level_str = "Fatal"; break; } char *newfmt = new char[msg.size() + 24]; sprintf(newfmt, "[%s] %-6s: %s", time_str, log_level_str.c_str(), msg.c_str()); vfprintf(stderr, newfmt, args); } #endif