summaryrefslogtreecommitdiff
path: root/client/Logger.cpp
diff options
context:
space:
mode:
authorAlex Borisov <borisov.alexandr@rambler.ru>2012-02-05 13:01:47 +0200
committerAlex Borisov <borisov.alexandr@rambler.ru>2012-02-05 13:01:47 +0200
commitcce3545422b24ea0acda858f28083f0f3f2dd374 (patch)
tree9f4863bc01634b7b10f0eee77edd2a5b053b1b1c /client/Logger.cpp
parente5f439b2f0226c54f76c0d7a4d7b6c5cd60e8650 (diff)
Ability to write logs to file
Diffstat (limited to 'client/Logger.cpp')
-rw-r--r--client/Logger.cpp21
1 files changed, 20 insertions, 1 deletions
diff --git a/client/Logger.cpp b/client/Logger.cpp
index 67c4907..74449ae 100644
--- a/client/Logger.cpp
+++ b/client/Logger.cpp
@@ -8,9 +8,19 @@
using std::string;
LogLevelType Logger::LogLevel = TraceLevel;
+FILE* Logger::logFile = NULL;
+
#ifdef DEBUG
+void Logger::InitLogFile()
+{
+ if (logFile == NULL)
+ {
+ logFile = fopen("client.log", "a");
+ }
+}
+
void Logger::Trace(string msg, ...)
{
va_list args;
@@ -58,6 +68,7 @@ void Logger::Log(LogLevelType logLevel, string msg, ...)
va_list args;
va_start(args, msg);
Logger::Log(logLevel, msg, args);
+ va_end(args);
}
void Logger::Log(LogLevelType logLevel, string msg, va_list args)
@@ -68,6 +79,7 @@ void Logger::Log(LogLevelType logLevel, string msg, va_list args)
strftime(time_str, 10, "%H:%M:%S", lctime);
string log_level_str;
+
switch (logLevel)
{
default:
@@ -91,9 +103,16 @@ void Logger::Log(LogLevelType logLevel, string msg, va_list args)
break;
}
+ char logstr[1024] = {0};
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);
+ vsprintf(logstr, newfmt, args);
+ fprintf(stderr, logstr);
+ if (logFile != NULL)
+ {
+ fprintf(logFile, logstr);
+ }
+ delete [] newfmt;
}
#endif \ No newline at end of file