summaryrefslogtreecommitdiffstats
path: root/src/utils/logger.h
blob: 0669577b7cafac7f661c1e1980304886faae681d (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 *  The Mana World Server
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World  is free software; you can redistribute  it and/or modify it
 *  under the terms of the GNU General  Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or any later version.
 *
 *  The Mana  World 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 General Public License for
 *  more details.
 *
 *  You should  have received a  copy of the  GNU General Public  License along
 *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  $Id$
 */


#ifndef _TMWSERV_LOGGER_H_
#define _TMWSERV_LOGGER_H_

#include <iosfwd>
#include <sstream>

namespace utils
{

/**
 * A very simple logger that writes messages to a log file.
 * If the log file is not set, the messages are routed to the standard output
 * or the standard error depending on the level of the message.
 * By default, the messages will be timestamped but the logger can be
 * configured to not prefix the messages with a timestamp.
 *
 * Limitations:
 *     - not thread-safe.
 *
 * Example of use:
 *
 * <pre>
 * \#include "logger.h"
 *
 * int main(void)
 * {
 *     using namespace utils;
 *
 *     Logger::setLogFile("/path/to/logfile");
 *
 *     // log messages using helper macros.
 *     LOG_DEBUG("level: " << 3)
 *     LOG_INFO("init sound")
 *     LOG_WARN("not implemented")
 *     LOG_ERROR("resource not found")
 *     LOG_FATAL("unable to init graphics")
 *
 *     // log messages using APIs.
 *     std::ostringstream os;
 *     os << "level: " << 3;
 *     Logger::output(os.str(), Logger::DEBUG);
 *
 *     Logger::output("init sound", Logger::INFO);
 *     Logger::output("not implemented", Logger::WARN);
 *     Logger::output("resource not found", Logger::ERROR);
 *     Logger::output("unable to init graphics", Logger::FATAL);
 *
 *     return 0;
 * }
 * </pre>
 */
class Logger
{
    public:
        enum Level
        {
            FATAL = 0,
            ERROR,
            WARN,
            INFO,
            DEBUG
        };

        /**
         * Sets the log file.
         *
         * This method will open the log file for writing, the former file
         * contents are removed.
         *
         * @param logFile the log file name (may include path).
         *
         * @exception std::ios::failure if the log file could not be opened.
         */
        static void setLogFile(std::string const &logFile);

        /**
         * Add/removes the timestamp.
         *
         * @param flag if true, a log message will always be timestamped
         *             (default = true).
         */
        static void setTimestamp(bool flag = true)
        { mHasTimestamp = flag; }

        /**
         * Sets tee mode.
         *
         * @param flag if true, write messages to both the standard (or error)
         *        output and the log file (if set) (default = true).
         */
        static void setTeeMode(bool flag = true)
        { mTeeMode = flag; }

        /**
         * Sets the verbosity level of the logger.
         *
         * @param verbosity is the level of verbosity.
         */
        static void setVerbosity(Level verbosity)
        { mVerbosity = verbosity; }

        /**
         * Logs a generic message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        static void output(std::string const &msg, Level atVerbosity);

        static Level mVerbosity;   /**< Verbosity level. */
    private:
        static bool mHasTimestamp; /**< Timestamp flag. */
        static bool mTeeMode;      /**< Tee mode flag. */

        /**
         * Logs a generic message.
         *
         * @param os the output stream.
         * @param msg the message to log.
         * @param prefix the message prefix.
         *
         * @exception std::ios::failure.
         */
        static void output(std::ostream &os, std::string const &msg, char const *prefix);
};


} // namespace utils

// HELPER MACROS

#define LOG_DEBUG(msg)                                                 \
    do if (::utils::Logger::mVerbosity >= ::utils::Logger::DEBUG) {    \
        std::ostringstream os;                                         \
        os << msg;                                                     \
        ::utils::Logger::output(os.str(), ::utils::Logger::DEBUG);     \
    } while (0)

#define LOG_INFO(msg)                                                  \
    do if (::utils::Logger::mVerbosity >= ::utils::Logger::INFO) {     \
        std::ostringstream os;                                         \
        os << msg;                                                     \
        ::utils::Logger::output(os.str(), ::utils::Logger::INFO);      \
    } while (0)

#define LOG_WARN(msg)                                                  \
    do if (::utils::Logger::mVerbosity >= ::utils::Logger::WARN) {     \
        std::ostringstream os;                                         \
        os << msg;                                                     \
        ::utils::Logger::output(os.str(), ::utils::Logger::WARN);      \
    } while (0)

#define LOG_ERROR(msg)                                                 \
    do if (::utils::Logger::mVerbosity >= ::utils::Logger::ERROR) {    \
        std::ostringstream os;                                         \
        os << msg;                                                     \
        ::utils::Logger::output(os.str(), ::utils::Logger::ERROR);     \
    } while (0)

#define LOG_FATAL(msg)                                                 \
    do if (::utils::Logger::mVerbosity >= ::utils::Logger::FATAL) {    \
        std::ostringstream os;                                         \
        os << msg;                                                     \
        ::utils::Logger::output(os.str(), ::utils::Logger::FATAL);     \
    } while (0)

#endif // _TMWSERV_LOGGER_H_