summaryrefslogtreecommitdiffstats
path: root/src/Core/LogManager.cpp
blob: 955ac76c2e547fad723c4fe926b0280d4ca2d145 (plain)
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
159
/*
 * 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 Core::String &message, const Core::String &source) {
  if(!(level == LOG_CRITICAL && source.isEmpty())) {// 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 Core::String &message, const Core::String &source) {
  if(source.isEmpty())
    std::cerr << message << std::endl;
  else
    std::cerr << message << " from " << source << std::endl;
}


LogManager::MessageLevel LogManager::parseLevel(const String &str) throw (Exception) {
  static const String DEBUG_LEVEL("debug");
  static const String VERBOSE_LEVEL("verbose");
  static const String DEFAULT_LEVEL("default");
  static const String WARNING_LEVEL("warning");
  static const String ERROR_LEVEL("error");
  static const String CRITICAL_LEVEL("critical");

  if(str.isEmpty())
    return Logger::LOG_DEFAULT;

  if(str.matches(DEBUG_LEVEL))
    return Logger::LOG_DEBUG;
  else if(str.matches(VERBOSE_LEVEL))
    return Logger::LOG_VERBOSE;
  else if(str.matches(DEFAULT_LEVEL))
    return Logger::LOG_DEFAULT;
  else if(str.matches(WARNING_LEVEL))
    return Logger::LOG_WARNING;
  else if(str.matches(ERROR_LEVEL))
    return Logger::LOG_ERROR;
  else if(str.matches(CRITICAL_LEVEL))
    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);
}


void LogManager::configure() {
  std::vector<const ConfigEntry*> entries = application->getConfigManager()->getEntries("Log");

  for(std::vector<const ConfigEntry*>::iterator entry = entries.begin(); entry != entries.end(); ++entry) {
    const std::vector<String> &values = (*entry)->getValues();

    if(values.empty() || !values.front().matches("Console"))
      continue;

    registerLogger(consoleLogger);

    bool remote = false;
    for(std::vector<String>::const_iterator value = values.begin()+1; value != values.end(); ++value) {
      if(value->matches("remote"))
        remote = true;
    }

    String level = (*entry)->get("Level");
    if(!level.isEmpty()) {
      try {
        if(remote)
          consoleLogger->setRemoteLevel(parseLevel(level));
        else
          consoleLogger->setLevel(parseLevel(level));
      }
      catch(Core::Exception e) {
        application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", level.toLocale().c_str());
      }
    }
  }

  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 Core::String &message, const Core::String &source) {
  if(level == Logger::LOG_CRITICAL && source.isEmpty())
    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();
    }
  }
}

}
}