diff options
author | Alex Borisov <borisov.alexandr@rambler.ru> | 2012-02-05 13:01:47 +0200 |
---|---|---|
committer | Alex Borisov <borisov.alexandr@rambler.ru> | 2012-02-05 13:01:47 +0200 |
commit | cce3545422b24ea0acda858f28083f0f3f2dd374 (patch) | |
tree | 9f4863bc01634b7b10f0eee77edd2a5b053b1b1c | |
parent | e5f439b2f0226c54f76c0d7a4d7b6c5cd60e8650 (diff) |
Ability to write logs to file
-rw-r--r-- | client/Logger.cpp | 21 | ||||
-rw-r--r-- | client/Logger.h | 4 | ||||
-rw-r--r-- | client/SslClient.cpp | 8 | ||||
-rw-r--r-- | client/main.cpp | 1 |
4 files changed, 28 insertions, 6 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 diff --git a/client/Logger.h b/client/Logger.h index cecd454..f8fa5db 100644 --- a/client/Logger.h +++ b/client/Logger.h @@ -13,7 +13,6 @@ enum LogLevelType FatalLevel }; - #ifdef DEBUG #include <stdarg.h> @@ -21,8 +20,10 @@ enum LogLevelType class Logger { public: + static FILE* logFile; static LogLevelType LogLevel; + static void InitLogFile(); static void Trace(std::string msg, ...); static void Debug(std::string msg, ...); static void Info(std::string msg, ...); @@ -39,6 +40,7 @@ private: class Logger { public: + static FILE* logFile; static LogLevelType LogLevel; static void Trace(std::string msg, ...) {} diff --git a/client/SslClient.cpp b/client/SslClient.cpp index 3909210..ee9de34 100644 --- a/client/SslClient.cpp +++ b/client/SslClient.cpp @@ -220,20 +220,20 @@ void SslClient::DataRecieved() return; } - fprintf(stderr, "[ "); + fprintf(Logger::logFile, "[ "); const char * tmp = pkt.constData(); for (int i = 0; i < pkt.size(); i++) { if (tmp[i] < 0x20) { - fprintf(stderr, "0x%X ", tmp[i]); + fprintf(Logger::logFile, "0x%X ", tmp[i]); } else { - fprintf(stderr, "%c ", tmp[i]); + fprintf(Logger::logFile, "%c ", tmp[i]); } } - fprintf(stderr, "]\n"); + fprintf(Logger::logFile, "]\n"); /* remove header and tail */ pkt.remove(pkt.size()-3, 3); diff --git a/client/main.cpp b/client/main.cpp index e561b15..95b1cba 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -19,6 +19,7 @@ ProxyClientApp *this_app; int main(int argc, char *argv[]) { + Logger::InitLogFile(); Logger::Info("Starting client application\n"); string path = argv[0]; QString dir; |