]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
Linux test_HTTP fixes.
[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         void setLogLevels(sgDebugClass c, sgDebugPriority p);
47 protected:
48         LogCallback(sgDebugClass c, sgDebugPriority p);
49
50         bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
51 private:
52         sgDebugClass m_class;
53         sgDebugPriority m_priority;
54 };
55
56 /**
57  * Helper force a console on platforms where it might optional, when
58  * we need to show a console. This basically means Windows at the
59  * moment - on other plaforms it's a no-op
60  */
61 void requestConsole();
62      
63 } // of namespace simgear
64
65 /**
66  * Class to manage the debug logging stream.
67  */
68 class logstream
69 {
70 public:
71     static void initGlobalLogstream();
72     /**
73      * Set the global log class and priority level.
74      * @param c debug class
75      * @param p priority
76      */
77     void setLogLevels( sgDebugClass c, sgDebugPriority p );
78
79     bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
80
81     void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
82
83     void set_log_priority( sgDebugPriority p);
84     
85     void set_log_classes( sgDebugClass c);
86     
87     sgDebugClass get_log_classes() const;
88     
89     sgDebugPriority get_log_priority() const;
90
91     /**
92      * the core logging method
93      */
94     void log( sgDebugClass c, sgDebugPriority p,
95             const char* fileName, int line, const std::string& msg);
96
97    /**
98     * \relates logstream
99     * Return the one and only logstream instance.
100     * We use a function instead of a global object so we are assured that cerr
101     * has been initialised.
102     * @return current logstream
103     */
104     friend logstream& sglog();
105     
106     /**
107      * register a logging callback. Note callbacks are run in a
108      * dedicated thread, so callbacks which pass data to other threads
109      * must use appropriate locking.
110      */
111     void addCallback(simgear::LogCallback* cb);
112      
113     void removeCallback(simgear::LogCallback* cb);
114
115 private:
116     // constructor
117     logstream();
118 };
119
120 logstream& sglog();
121
122
123
124 /** \def SG_LOG(C,P,M)
125  * Log a message.
126  * @param C debug class
127  * @param P priority
128  * @param M message
129  */
130 #ifdef FG_NDEBUG
131 # define SG_LOG(C,P,M)
132 #else
133 # define SG_LOG(C,P,M) do {                     \
134         if(sglog().would_log(C,P)) {                      \
135         std::ostringstream os;                             \
136         os << M;                                       \
137         sglog().log(C, P, __FILE__, __LINE__, os.str()); \
138     } \
139 } while(0)
140 #endif
141
142 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
143
144 #endif // _LOGSTREAM_H
145