2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef LOGGER_HPP_INCLUDED
25 #define LOGGER_HPP_INCLUDED
32 /// A simple class to store an error message.
33 /// Each message has a string and a severity level.
47 ErrorMessage(Severity sev_
, std::string msg_
)
55 * A logger class, which is able to record error messages.
57 * This class records error messages, stores them in a list.
58 * One can get the list by calling get_messages(). A const_iterator
59 * is provided to allow the user iterating through the messages.
61 * Each logger object remembers if it stores an error message;
62 * upon deletion, the destructor will check if there were any
63 * messages still unread. If that is the case, an error
64 * message is printed to the console. Currently, a GLib
65 * log handler is also installed by the misc/logger.
67 * The Logger class keeps track of all Logger objects in
68 * existence, using the loggers static variable.
69 * Global error logging functions are provided for simple
70 * usage - they allow callers to use the logging facility
71 * without the need of passing the references to a logger
74 * The global log functions always log errors to the most recently
75 * created Logger object. The scheme to use this thing is:
77 * { // a code block for the logger object
78 * Logger l; // create a logger
81 * ... // do things that use the global gd_message() etc
85 * // there were errors reported
86 * for (Logger::ConstIterator it=l.begin(); it!=l.end(); ++it)
87 * std::cout << it->message << std::endl;
90 * } // logger is deleted here
93 * It is up to the caller to create the logger objects.
94 * It is recommended to create a "global" logger object int the
95 * main() function, which will receive all log messages, when
96 * no other logger objects exist.
100 * Logger global_logger;
104 * global_misc/logger.clear();
110 static std::vector
<Logger
*> loggers
;
112 typedef std::vector
<ErrorMessage
> Container
;
113 typedef Container::const_iterator ConstIterator
;
116 bool ignore
; ///< if true, all errors reported are ignored
117 bool read
; ///< if false, not all messages are seen by the user.
118 Container messages
; ///< list of messages
119 std::string context
; ///< context which is added to all messages
121 Logger(Logger
const &); // deliberately not implemented
122 Logger
&operator=(Logger
const &); // deliberately not implemented
124 void set_context(std::string
const &new_context
= std::string());
125 std::string
const &get_context() const;
128 Logger(bool ignore_
= false);
132 Container
const &get_messages() const;
133 std::string
get_messages_in_one_string() const;
134 void log(ErrorMessage::Severity sev
, std::string
const &message
);
136 friend void log(ErrorMessage::Severity sev
, std::string
const &message
);
137 friend Logger
&get_active_logger();
138 friend class SetLoggerContextForFunction
;
143 void log(ErrorMessage::Severity sev
, std::string
const &message
);
144 Logger
&get_active_logger();
146 void gd_critical(const char *message
);
147 void gd_warning(const char *message
);
148 void gd_message(const char *message
);
150 void gd_debug(const char *message
);
153 /** Set the logger context for the lifetime of the object.
154 * To be used to set the context while inside a function or a statement block:
158 * SetLoggerContextForFunction slc(get_active_logger(), "Reading file");
160 * // Log messages generated here will have the context
162 * } // slc object goes out of scope here, context is set back to original value
165 class SetLoggerContextForFunction
{
167 std::string orig_context
;
169 SetLoggerContextForFunction(std::string
const &context
, Logger
&l
= get_active_logger())
170 : l(l
), orig_context(l
.get_context()) {
171 l
.set_context(orig_context
.empty() ? context
: (orig_context
+ ", " + context
));
173 ~SetLoggerContextForFunction() {
174 l
.set_context(orig_context
);
179 #endif /* GD_LOGGER */