summaryrefslogtreecommitdiffstats
path: root/src/Core/LogManager.h
blob: c569238d4e8633ed229ed1ca7ebae0a6d0ec6e84 (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
/*
 * LogManager.h
 *
 * 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/>.
 */

#ifndef MAD_CORE_LOGMANAGER_H_
#define MAD_CORE_LOGMANAGER_H_

#include "export.h"

#include "Configurable.h"
#include "Exception.h"
#include "Logger.h"

#include <queue>
#include <set>

#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>

namespace Mad {
namespace Core {

class Application;
class ThreadManager;

class MAD_CORE_EXPORT LogManager : public Configurable {
  private:
    friend class Application;
    friend class ThreadManager;

    typedef Logger::MessageCategory MessageCategory;
    typedef Logger::MessageLevel MessageLevel;

    struct Message {
      MessageCategory category;
      MessageLevel level;
      boost::posix_time::ptime timestamp;
      std::string message;
      std::string source;
    };

    class ConsoleLogger : public Logger {
      private:
        // For long messages, writing to cerr is not atomic
        // -> lock cerr to prevent messages mixing up
        boost::mutex cerrMutex;

      protected:
        virtual void logMessage(MessageCategory category, MessageLevel, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source);

      public:
        ConsoleLogger() {}

        void logMessageDirect(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source);
    };


    Application *application;

    boost::shared_ptr<ConsoleLogger> consoleLogger;

    std::set<boost::shared_ptr<Logger> > loggers;
    bool configured, running;

    boost::mutex queueMutex;
    boost::condition_variable queueCond;

    boost::mutex loggerMutex;

    std::queue<Message> messageQueue;

    void loggerThread();
    void stopLoggerThread() {
      boost::lock_guard<boost::mutex> lock(queueMutex);
      running = false;
      queueCond.notify_one();
    }

    LogManager(Application *application0);
    ~LogManager();

  protected:
    virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled);
    virtual void configFinished();

  public:
    static MessageLevel parseLevel(const std::string &str) throw (Exception);

    void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source = std::string());

    void registerLogger(boost::shared_ptr<Logger> logger) {
      boost::lock_guard<boost::mutex> lock(loggerMutex);
      loggers.insert(logger);
    }

    void unregisterLogger(boost::shared_ptr<Logger> logger) {
      boost::lock_guard<boost::mutex> lock(loggerMutex);
      loggers.erase(logger);
    }
};

}
}

#endif /* MAD_CORE_LOGMANAGER_H_ */