diff options
Diffstat (limited to 'src/log.cpp')
-rw-r--r-- | src/log.cpp | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/src/log.cpp b/src/log.cpp index e1db8d4f..df92e49e 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -24,27 +24,30 @@ #include <windows.h> #endif -FILE* logfile; +#include <sstream> -Logger::Logger(std::string logFilename) +Logger::Logger(const std::string &logFilename) { logFile.open(logFilename.c_str(), std::ios_base::trunc); - if ( !logFile.is_open() ) + + if (!logFile.is_open()) { - std::cout << "Warning: error while opening log file." << std::endl; + std::cout << "Warning: error while opening log file.\n"; } - } Logger::~Logger() { - logFile.close(); + if (logFile.is_open()) + { + logFile.close(); + } } void Logger::log(const char *log_text, ...) { - if ( logFile.is_open() ) + if (logFile.is_open()) { char* buf = new char[1024]; va_list ap; @@ -59,18 +62,19 @@ void Logger::log(const char *log_text, ...) time(&t); // Print the log entry - logFile << "["; - logFile << ((((t / 60) / 60) % 24 < 10) ? "0" : ""); - logFile << (int)(((t / 60) / 60) % 24); - logFile << ":"; - logFile << (((t / 60) % 60 < 10) ? "0" : ""); - logFile << (int)((t / 60) % 60); - logFile << ":"; - logFile << ((t % 60 < 10) ? "0" : ""); - logFile << (int)(t % 60); - logFile << "] "; + std::stringstream timeStr; + timeStr << "["; + timeStr << ((((t / 60) / 60) % 24 < 10) ? "0" : ""); + timeStr << (int)(((t / 60) / 60) % 24); + timeStr << ":"; + timeStr << (((t / 60) % 60 < 10) ? "0" : ""); + timeStr << (int)((t / 60) % 60); + timeStr << ":"; + timeStr << ((t % 60 < 10) ? "0" : ""); + timeStr << (int)(t % 60); + timeStr << "] "; - logFile << buf << std::endl; + logFile << timeStr.str() << buf << std::endl; // Delete temporary buffer delete[] buf; @@ -83,9 +87,8 @@ void Logger::error(const std::string &error_text) #ifdef WIN32 MessageBox(NULL, error_text.c_str(), "Error", MB_ICONERROR | MB_OK); #else - log("Error :"); - log(error_text.c_str()); + std::cerr << "Error: " << error_text << std::endl; + log("Error: %s", error_text.c_str()); #endif exit(1); } - |