1 // -*- C++ -*-
2 // Module:  Log4CPLUS
3 // File:    loglog.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 
24 #ifndef LOG4CPLUS_HELPERS_LOGLOG
25 #define LOG4CPLUS_HELPERS_LOGLOG
26 
27 #include <log4cplus/config.hxx>
28 
29 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30 #pragma once
31 #endif
32 
33 #include <log4cplus/tstring.h>
34 #include <log4cplus/streams.h>
35 #include <log4cplus/thread/syncprims.h>
36 
37 
38 namespace log4cplus {
39     namespace helpers {
40 
41         /**
42          * This class used to output log statements from within the log4cplus package.
43          *
44          * Log4cplus components cannot make log4cplus logging calls. However, it is
45          * sometimes useful for the user to learn about what log4cplus is
46          * doing. You can enable log4cplus internal logging by defining the
47          * <b>log4cplus.configDebug</b> variable.
48          *
49          * All log4cplus internal debug calls go to <code>cout</code>
50          * where as internal error messages are sent to
51          * <code>cerr</code>. All internal messages are prepended with
52          * the string "log4clus: ".
53          */
54         class LOG4CPLUS_EXPORT LogLog
55         {
56         public:
57             //! Return type of getLogLog().
58             typedef LogLog * Ptr;
59 
60             /**
61              * Returns a reference to the <code>LogLog</code> singleton.
62              */
63             static Ptr getLogLog();
64 
65 
66             /**
67              * Allows to enable/disable log4cplus internal logging.
68              */
69             void setInternalDebugging(bool enabled);
70 
71             /**
72              * In quite mode no LogLog generates strictly no output, not even
73              * for errors.
74              *
75              * @param quietMode A true for not
76              */
77             void setQuietMode(bool quietMode);
78 
79             /**
80              * This method is used to output log4cplus internal debug
81              * statements. Output goes to <code>std::cout</code>.
82              */
83             void debug(const log4cplus::tstring& msg) const;
84             void debug(tchar const * msg) const;
85 
86             /**
87              * This method is used to output log4cplus internal error
88              * statements. There is no way to disable error
89              * statements.  Output goes to
90              * <code>std::cerr</code>. Optionally, this method can
91              * throw std::runtime_error exception too.
92              */
93             void error(const log4cplus::tstring& msg, bool throw_flag = false) const;
94             void error(tchar const * msg, bool throw_flag = false) const;
95 
96             /**
97              * This method is used to output log4cplus internal warning
98              * statements. There is no way to disable warning statements.
99              * Output goes to <code>std::cerr</code>.
100              */
101             void warn(const log4cplus::tstring& msg) const;
102             void warn(tchar const * msg) const;
103 
104             // Public ctor and dtor to be used only by internal::DefaultContext.
105             LogLog();
106             virtual ~LogLog();
107 
108         private:
109             enum TriState
110             {
111                 TriUndef = -1,
112                 TriFalse,
113                 TriTrue
114             };
115 
116             template <typename StringType>
117             LOG4CPLUS_PRIVATE
118             void logging_worker (tostream & os,
119                 bool (LogLog:: * cond) () const, tchar const *,
120                 StringType const &, bool throw_flag = false) const;
121 
122             LOG4CPLUS_PRIVATE static void set_tristate_from_env (TriState *,
123                 tchar const * envvar);
124 
125             LOG4CPLUS_PRIVATE bool get_quiet_mode () const;
126             LOG4CPLUS_PRIVATE bool get_not_quiet_mode () const;
127             LOG4CPLUS_PRIVATE bool get_debug_mode () const;
128 
129             // Data
130             mutable TriState debugEnabled;
131             mutable TriState quietMode;
132             thread::Mutex mutex;
133 
134             LOG4CPLUS_PRIVATE LogLog(const LogLog&);
135             LOG4CPLUS_PRIVATE LogLog & operator = (LogLog const &);
136         };
137 
138         LOG4CPLUS_EXPORT LogLog & getLogLog ();
139 
140     } // end namespace helpers
141 } // end namespace log4cplus
142 
143 
144 #endif // LOG4CPLUS_HELPERS_LOGLOG
145 
146