summaryrefslogtreecommitdiff
path: root/src/utils/logger.h
blob: 6ce97a715bb4e1515342a2905105f7aa7fcd54d8 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 *  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 <fstream>
#include <sstream>

#include "singleton.h"

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.
 *
 * Notes:
 *     - this class implements the Meyer's singleton design pattern.
 *
 * Example of use:
 *
 * <pre>
 * \#include "logger.h"
 *
 * int main(void)
 * {
 *     using namespace utils;
 *
 *     Logger& logger = Logger::instance();
 *     logger.setLogFile("/path/to/logfile");
 *
 *     // log messages using helper macros.
 *     LOG("hello world")
 *     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.
 *     logger.log("hello world");
 *
 *     std::ostringstream os;
 *     os << "level: " << 3;
 *     logger.debug(os.str());
 *
 *     logger.info("init sound");
 *     logger.warn("not implemented");
 *     logger.error("resource not found");
 *     logger.fatal("unable to init graphics");
 *
 *     return 0;
 * }
 * </pre>
 */
class Logger: public Singleton<Logger>
{
    // friend so that Singleton can call the constructor.
    friend class Singleton<Logger>;

    public:
        /**
         * Set 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.
         */
        void
        setLogFile(const std::string& logFile);

        /**
         * Add/remove the timestamp.
         *
         * @param flag if true, a log message will always be timestamped
         *             (default = true).
         */
        void
        setTimestamp(bool flag = true)
            throw();

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

        /**
         * Set the verbosity level of the logger.
         *
         * @param verbosity is the level of verbosity.
         *        0 = Standard infos
         *        1 = + Infos on loading/unloading/reloading resources
         *        2 = + Packets names and messages sent.
         */
        void
        setVerbosity(unsigned short verbosity = 0) { mVerbosity = verbosity; }

        /**
         * Set tee mode.
         *
         * @param flag if true, write messages to both the standard (or error)
         *        output and the log file (if set) (default = true).
         */
        unsigned short
        getVerbosity() { return mVerbosity; }

        /**
         * Log a generic message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        log(const std::string &msg, unsigned short atVerbosity = 0);

        /**
         * Log a debug message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        debug(const std::string &msg, unsigned short atVerbosity = 0);

        /**
         * Log an info message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        info(const std::string &msg, unsigned short atVerbosity = 0);

        /**
         * Log a warn message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        warn(const std::string &msg, unsigned short atVerbosity = 0);

        /**
         * Log an error message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        error(const std::string &msg, unsigned short atVerbosity = 0);

        /**
         * Log a fatal error message.
         *
         * @param msg the message to log.
         *
         * @param atVerbosity the minimum verbosity level
         *        to log this
         *
         * @exception std::ios::failure.
         */
        void
        fatal(const std::string &msg, unsigned short atVerbosity = 0);

    private:
        /**
         * Default constructor.
         */
        Logger(void)
            throw();

        /**
         * Destructor.
         */
        ~Logger(void)
            throw();

        /**
         * Copy constructor.
         */
        Logger(const Logger& rhs);

        /**
         * Assignment operator.
         */
        Logger&
        operator=(const Logger& rhs);

        /**
         * Log a generic message.
         *
         * @param os the output stream.
         * @param msg the message to log.
         * @param prefix the message prefix (default = "").
         *
         * @exception std::ios::failure.
         */
        void
        log(std::ostream& os,
            const std::string& msg,
            const std::string& prefix = "");

        /**
         * Get the current time.
         *
         * @return the current time as string.
         */
        std::string
        getCurrentTime(void);


        std::ofstream mLogFile;    /**< the log file */
        bool mHasTimestamp;        /**< the timestamp flag */
        bool mTeeMode;             /**< the tee mode flag */
        unsigned short mVerbosity; /**< keeps the verbosity level */
};


} // namespace utils

// HELPER MACROS


#define LOG(msg, atVerbosity)                                            \
    do {                                                                 \
        std::ostringstream os;                                           \
        os << msg;                                                       \
        ::utils::Logger::instance().log(os.str(), atVerbosity); \
    } while(0)

#define LOG_DEBUG(msg, atVerbosity)                                        \
    do {                                                                   \
        std::ostringstream os;                                             \
        os << msg;                                                         \
        ::utils::Logger::instance().debug(os.str(), atVerbosity); \
    } while (0)

#define LOG_INFO(msg, atVerbosity)                              \
    do {                                                        \
        std::ostringstream os;                                  \
        os << msg;                                              \
        ::utils::Logger::instance().info(os.str(), atVerbosity); \
    } while (0)

#define LOG_WARN(msg, atVerbosity)                              \
    do {                                                        \
        std::ostringstream os;                                  \
        os << msg;                                              \
        ::utils::Logger::instance().warn(os.str(), atVerbosity); \
    } while (0)

#define LOG_ERROR(msg, atVerbosity)                             \
    do {                                                        \
        std::ostringstream os;                                  \
        os << msg;                                              \
        ::utils::Logger::instance().error(os.str(), atVerbosity); \
    } while (0)

#define LOG_FATAL(msg, atVerbosity)                             \
    do {                                                        \
        std::ostringstream os;                                  \
        os << msg;                                              \
        ::utils::Logger::instance().fatal(os.str(), atVerbosity); \
    } while (0)

#endif // _TMWSERV_LOGGER_H_