1 // -*- C++ -*-
2 // Module:  Log4CPLUS
3 // File:    logger.h
4 // Created: 6/2001
5 // Author:  Tad E. Smith
6 //
7 //
8 // Copyright 2001-2013 Tad E. Smith
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 //     http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 
22 /** @file
23  * This header defines the Logger class and the logging macros. */
24 
25 #ifndef LOG4CPLUS_LOGGERHEADER_
26 #define LOG4CPLUS_LOGGERHEADER_
27 
28 #include <log4cplus/config.hxx>
29 
30 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
31 #pragma once
32 #endif
33 
34 #include <log4cplus/loglevel.h>
35 #include <log4cplus/tstring.h>
36 #include <log4cplus/spi/appenderattachable.h>
37 #include <log4cplus/spi/loggerfactory.h>
38 
39 #include <vector>
40 
41 
42 namespace log4cplus
43 {
44     // Forward declarations
45 
46     class Appender;
47     class Hierarchy;
48     class HierarchyLocker;
49     class DefaultLoggerFactory;
50 
51     namespace spi
52     {
53 
54         class LoggerImpl;
55 
56     }
57 
58 
59     /** \typedef std::vector<Logger> LoggerList
60      * This is a list of {@link Logger Loggers}. */
61     typedef std::vector<Logger> LoggerList;
62 
63 
64     /**
65      * This is the central class in the log4cplus package. One of the
66      * distintive features of log4cplus are hierarchical loggers and their
67      * evaluation.
68      *
69      * See the <a href="../../../../manual.html">user manual</a> for an
70      * introduction on this class.
71      */
72     class LOG4CPLUS_EXPORT Logger
73         : public log4cplus::spi::AppenderAttachable
74     {
75     public:
76       // Static Methods
77         /**
78          * Returns <code>true </code>if the named logger exists
79          * (in the default hierarchy).
80          *
81          * @param name The name of the logger to search for.
82          */
83         static bool exists(const log4cplus::tstring& name);
84 
85         /*
86          * Returns all the currently defined loggers in the default
87          * hierarchy.
88          *
89          * The root logger is <em>not</em> included in the returned
90          * list.
91         */
92         static LoggerList getCurrentLoggers();
93 
94         /**
95          * Return the default Hierarchy instance.
96          */
97         static Hierarchy& getDefaultHierarchy();
98 
99         /**
100          * Retrieve a logger with name <code>name</code>.  If the named
101          * logger already exists, then the existing instance will be returned.
102          * Otherwise, a new instance is created.
103          *
104          * By default, loggers do not have a set LogLevel but inherit
105          * it from the hierarchy. This is one of the central features of
106          * log4cplus.
107          *
108          * @param name The name of the logger to retrieve.
109          */
110         static Logger getInstance(const log4cplus::tstring& name);
111 
112         /**
113          * Like getInstance() except that the type of logger
114          * instantiated depends on the type returned by the {@link
115          * spi::LoggerFactory#makeNewLoggerInstance} method of the
116          * <code>factory</code> parameter.
117          *
118          * This method is intended to be used by sub-classes.
119          *
120          * @param name The name of the logger to retrieve.
121          * @param factory A {@link spi::LoggerFactory} implementation that will
122          * actually create a new Instance.
123          */
124         static Logger getInstance(const log4cplus::tstring& name, spi::LoggerFactory& factory);
125 
126         /**
127          * Return the root of the default logger hierrachy.
128          *
129          * The root logger is always instantiated and available. It's
130          * name is "root".
131          *
132          * Nevertheless, calling {@link #getInstance
133          * Logger.getInstance("root")} does not retrieve the root logger
134          * but a logger just under root named "root".
135          */
136         static Logger getRoot();
137 
138         /**
139          * Calling this method will <em>safely</em> close and remove all
140          * appenders in all the loggers including root contained in the
141          * default hierachy.
142          *
143          * Some appenders such as SocketAppender need to be closed before the
144          * application exits. Otherwise, pending logging events might be
145          * lost.
146          *
147          * The <code>shutdown</code> method is careful to close nested
148          * appenders before closing regular appenders. This is allows
149          * configurations where a regular appender is attached to a logger
150          * and again to a nested appender.
151          */
152         static void shutdown();
153 
154       // Non-Static Methods
155         /**
156          * If <code>assertionVal</code> parameter is <code>false</code>, then
157          * logs <code>msg</code> with FATAL_LOG_LEVEL log level.
158          *
159          * @param assertionVal Truth value of assertion condition.
160          * @param msg The message to print if <code>assertion</code> is
161          * false.
162          */
163         void assertion(bool assertionVal, const log4cplus::tstring& msg) const;
164 
165         /**
166          * Close all attached appenders implementing the AppenderAttachable
167          * interface.
168          */
169         void closeNestedAppenders() const;
170 
171         /**
172          * Check whether this logger is enabled for a given
173          * LogLevel passed as parameter.
174          *
175          * @return boolean True if this logger is enabled for <code>ll</code>.
176          */
177         bool isEnabledFor(LogLevel ll) const;
178 
179         /**
180          * This generic form is intended to be used by wrappers.
181          */
182         void log(LogLevel ll, const log4cplus::tstring& message,
183                  const char* file=NULL, int line=-1) const;
184 
185         void log(spi::InternalLoggingEvent const &) const;
186 
187         /**
188          * This method creates a new logging event and logs the event
189          * without further checks.
190          */
191         void forcedLog(LogLevel ll, const log4cplus::tstring& message,
192                        const char* file=NULL, int line=-1) const;
193 
194         void forcedLog(spi::InternalLoggingEvent const &) const;
195 
196         /**
197          * Call the appenders in the hierrachy starting at
198          * <code>this</code>.  If no appenders could be found, emit a
199          * warning.
200          *
201          * This method calls all the appenders inherited from the
202          * hierarchy circumventing any evaluation of whether to log or not
203          * to log the particular log request.
204          *
205          * @param event the event to log.
206          */
207         void callAppenders(const spi::InternalLoggingEvent& event) const;
208 
209         /**
210          * Starting from this logger, search the logger hierarchy for a
211          * "set" LogLevel and return it. Otherwise, return the LogLevel of the
212          * root logger.
213          *
214          * The Logger class is designed so that this method executes as
215          * quickly as possible.
216          */
217         LogLevel getChainedLogLevel() const;
218 
219         /**
220          * Returns the assigned LogLevel, if any, for this Logger.
221          *
222          * @return LogLevel - the assigned LogLevel, can be <code>NOT_SET_LOG_LEVEL</code>.
223          */
224         LogLevel getLogLevel() const;
225 
226         /**
227          * Set the LogLevel of this Logger.
228          */
229         void setLogLevel(LogLevel ll);
230 
231         /**
232          * Return the the {@link Hierarchy} where this <code>Logger</code> instance is
233          * attached.
234          */
235         Hierarchy& getHierarchy() const;
236 
237         /**
238          * Return the logger name.
239          */
240         log4cplus::tstring const & getName() const;
241 
242         /**
243          * Get the additivity flag for this Logger instance.
244          */
245         bool getAdditivity() const;
246 
247         /**
248          * Set the additivity flag for this Logger instance.
249          */
250         void setAdditivity(bool additive);
251 
252       // AppenderAttachable Methods
253         virtual void addAppender(SharedAppenderPtr newAppender);
254 
255         virtual SharedAppenderPtrList getAllAppenders();
256 
257         virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name);
258 
259         virtual void removeAllAppenders();
260 
261         virtual void removeAppender(SharedAppenderPtr appender);
262 
263         virtual void removeAppender(const log4cplus::tstring& name);
264 
265         Logger ();
266         Logger(const Logger& rhs);
267         Logger& operator=(const Logger& rhs);
268 #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
269         Logger (Logger && rhs);
270         Logger & operator = (Logger && rhs);
271 #endif
272         virtual ~Logger();
273 
274         void swap (Logger &);
275 
276         /**
277          * Used to retrieve the parent of this Logger in the
278          * Logger tree.
279          */
280         Logger getParent() const;
281 
282     protected:
283       // Data
284         /** This is a pointer to the implementation class. */
285         spi::LoggerImpl * value;
286 
287     private:
288       // Ctors
289         /**
290          * This constructor created a new <code>Logger</code> instance
291          * with a pointer to a Logger implementation.
292          *
293          * You should not create loggers directly.
294          *
295          * @param ptr A pointer to the Logger implementation.  This value
296          *            cannot be NULL.
297          */
298         LOG4CPLUS_PRIVATE Logger(spi::LoggerImpl * ptr);
299 
300       // Friends
301         friend class log4cplus::spi::LoggerImpl;
302         friend class log4cplus::Hierarchy;
303         friend class log4cplus::HierarchyLocker;
304         friend class log4cplus::DefaultLoggerFactory;
305     };
306 
307 
308     /**
309      * This class is used to create the default implementation of
310      * the Logger class
311      */
312     class LOG4CPLUS_EXPORT DefaultLoggerFactory : public spi::LoggerFactory {
313     public:
314         Logger makeNewLoggerInstance(const log4cplus::tstring& name, Hierarchy& h);
315     };
316 
317 
318 } // end namespace log4cplus
319 
320 
321 #endif // LOG4CPLUS_LOGGERHEADER_
322