]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
Ensure individual log-level setting works.
[simgear.git] / simgear / debug / logstream.hxx
1 /** \file logstream.hxx
2  * Stream based logging mechanism.
3  */
4
5 // Written by Bernie Bright, 1998
6 //
7 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25 #ifndef _LOGSTREAM_H
26 #define _LOGSTREAM_H
27
28 #include <simgear/compiler.h>
29 #include <simgear/debug/debug_types.h>
30
31 #include <sstream>
32  
33 // forward decls
34 class SGPath;
35       
36 namespace simgear
37 {
38      
39 class LogCallback
40 {
41 public:
42     virtual ~LogCallback() {}
43     virtual void operator()(sgDebugClass c, sgDebugPriority p, 
44         const char* file, int line, const std::string& aMessage) = 0;
45 };
46      
47 } // of namespace simgear
48
49 /**
50  * Class to manage the debug logging stream.
51  */
52 class logstream
53 {
54 public:
55     static void initGlobalLogstream();
56     /**
57      * Set the global log class and priority level.
58      * @param c debug class
59      * @param p priority
60      */
61     void setLogLevels( sgDebugClass c, sgDebugPriority p );
62
63     bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
64
65     void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
66
67     void set_log_priority( sgDebugPriority p);
68     
69     void set_log_classes( sgDebugClass c);
70     
71     sgDebugClass get_log_classes() const;
72     
73     sgDebugPriority get_log_priority() const;
74
75     /**
76      * the core logging method
77      */
78     void log( sgDebugClass c, sgDebugPriority p,
79             const char* fileName, int line, const std::string& msg);
80
81    /**
82     * \relates logstream
83     * Return the one and only logstream instance.
84     * We use a function instead of a global object so we are assured that cerr
85     * has been initialised.
86     * @return current logstream
87     */
88     friend logstream& sglog();
89     
90     /**
91      * register a logging callback. Note callbacks are run in a
92      * dedicated thread, so callbacks which pass data to other threads
93      * must use appropriate locking.
94      */
95     void addCallback(simgear::LogCallback* cb);
96      
97     void removeCallback(simgear::LogCallback* cb);
98
99 private:
100     // constructor
101     logstream();
102 };
103
104 logstream& sglog();
105
106 /** \def SG_LOG(C,P,M)
107  * Log a message.
108  * @param C debug class
109  * @param P priority
110  * @param M message
111  */
112 #ifdef FG_NDEBUG
113 # define SG_LOG(C,P,M)
114 #else
115 # define SG_LOG(C,P,M) do {                     \
116         if(sglog().would_log(C,P)) {                      \
117         std::ostringstream os;                             \
118         os << M;                                       \
119         sglog().log(C, P, __FILE__, __LINE__, os.str()); \
120     } \
121 } while(0)
122 #endif
123
124 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
125
126 #endif // _LOGSTREAM_H
127