1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* LogManager.cpp
*
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LogManager.h"
#include "Application.h"
#include "ConfigEntry.h"
#include "ConfigManager.h"
#include <iostream>
namespace Mad {
namespace Core {
void LogManager::ConsoleLogger::logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) {
if(!(level == LOG_CRITICAL && source.empty())) {// Critical messages are printed to cerr directly, so don't print them a second time
boost::lock_guard<boost::mutex> lock(cerrMutex);
logMessageDirect(category, level, timestamp, message, source);
}
}
void LogManager::ConsoleLogger::logMessageDirect(MessageCategory /*category*/, MessageLevel /*level*/, boost::posix_time::ptime /*timestamp*/, const std::string &message, const std::string &source) {
if(source.empty())
std::cerr << message << std::endl;
else
std::cerr << message << " from " << source << std::endl;
}
LogManager::MessageLevel LogManager::parseLevel(const std::string &str) throw (Exception) {
if(str.empty())
return Logger::LOG_DEFAULT;
std::string lowerStr = boost::algorithm::to_lower_copy(str);
if(lowerStr == "debug")
return Logger::LOG_DEBUG;
else if(lowerStr == "verbose")
return Logger::LOG_VERBOSE;
else if(lowerStr == "default")
return Logger::LOG_DEFAULT;
else if(lowerStr == "warning")
return Logger::LOG_WARNING;
else if(lowerStr == "error")
return Logger::LOG_ERROR;
else if(lowerStr == "critical")
return Logger::LOG_CRITICAL;
else
throw Exception(Exception::INVALID_INPUT);
}
LogManager::LogManager(Application *application0) : application(application0), consoleLogger(new ConsoleLogger), configured(false), running(false) {
application->getConfigManager()->registerConfigurable(this);
}
LogManager::~LogManager() {
application->getConfigManager()->unregisterConfigurable(this);
}
bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
if(entry[0].getKey().matches("Log")) {
if(entry[0][0].matches("Console")) {
if(entry[1].empty()) {
registerLogger(consoleLogger);
return true;
}
else if(entry[1].getKey().matches("Level")) {
if(entry[2].empty()) {
try {
if(boost::algorithm::to_lower_copy(static_cast<std::string>(entry[1][0])) == "remote")
consoleLogger->setRemoteLevel(parseLevel(entry[1][1]));
else
consoleLogger->setLevel(parseLevel(entry[1][0]));
}
catch(Core::Exception e) {
application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].c_str());
}
return true;
}
}
}
else if(entry[1].empty()) {
if(!handled) {
application->logf(Logger::LOG_WARNING, "Unknown logger '%s'.", entry[0][0].c_str());
return true;
}
}
}
return false;
}
void LogManager::configFinished() {
if(loggers.empty())
registerLogger(consoleLogger);
boost::lock_guard<boost::mutex> lock(queueMutex);
configured = true;
queueCond.notify_one();
}
void LogManager::log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) {
if(level == Logger::LOG_CRITICAL && source.empty())
consoleLogger->logMessageDirect(category, level, timestamp, message, source);
boost::lock_guard<boost::mutex> lock(queueMutex);
Message m = {category, level, timestamp, message, source};
messageQueue.push(m);
queueCond.notify_one();
}
void LogManager::loggerThread() {
boost::unique_lock<boost::mutex> lock(queueMutex);
running = true;
while(running) {
while(running && ((messageQueue.empty() && messageQueue.empty()) || !configured))
queueCond.wait(lock);
while(!messageQueue.empty()) {
Message message = messageQueue.front();
messageQueue.pop();
lock.unlock();
{
boost::lock_guard<boost::mutex> loggerLock(loggerMutex);
for(std::set<boost::shared_ptr<Logger> >::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) {
(*logger)->log(message.category, message.level, message.timestamp, message.message, message.source);
}
}
lock.lock();
}
}
}
}
}
|