LogManager.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 }
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3