LogManager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "adchpp.h"
00020
00021 #include "LogManager.h"
00022
00023 #include "File.h"
00024
00025 namespace adchpp {
00026
00027 using namespace std;
00028
00029 LogManager* LogManager::instance = 0;
00030
00031 LogManager::LogManager() : logFile("logs/adchpp%Y%m.log"), enabled(true) { }
00032
00033 void LogManager::log(const string& area, const string& msg) throw() {
00034 char buf[64];
00035 time_t now = time(NULL);
00036 size_t s = strftime(buf, 64, "%Y-%m-%d %H:%M:%S: ", localtime(&now));
00037 string tmp(buf, s);
00038 tmp += area;
00039 tmp += ": ";
00040 tmp += msg;
00041 dolog(tmp);
00042 }
00043
00044 void LogManager::dolog(const string& msg) throw() {
00045 dcdebug("Logging: %s\n", msg.c_str());
00046 signalLog_(msg);
00047 if(getEnabled()) {
00048 string logFile = Util::formatTime(File::makeAbsolutePath(Util::getCfgPath(), getLogFile()));
00049 FastMutex::Lock l(mtx);
00050 try {
00051 File f(logFile, File::WRITE, File::OPEN | File::CREATE);
00052 f.setEndPos(0);
00053 f.write(msg + "\r\n");
00054 return;
00055 } catch(const FileException& e) {
00056 dcdebug("LogManager::log: %s\n", e.getError().c_str());
00057 }
00058 try {
00059 File::ensureDirectory(logFile);
00060
00061 File f(logFile, File::WRITE, File::OPEN | File::CREATE);
00062 f.setEndPos(0);
00063 f.write(msg + "\r\n");
00064 } catch(const FileException& ee) {
00065 dcdebug("LogManager::log2: %s\n", ee.getError().c_str());
00066 }
00067 }
00068 }
00069
00070 }