]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
Merge branch 'next' of git.mxchange.org:/var/cache/git/repos/simgear into next
[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 #include <vector>
33  
34 // forward decls
35 class SGPath;
36       
37 namespace simgear
38 {
39      
40 class LogCallback
41 {
42 public:
43     virtual ~LogCallback() {}
44     virtual void operator()(sgDebugClass c, sgDebugPriority p, 
45         const char* file, int line, const std::string& aMessage) = 0;
46
47         void setLogLevels(sgDebugClass c, sgDebugPriority p);
48 protected:
49         LogCallback(sgDebugClass c, sgDebugPriority p);
50
51         bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
52 private:
53         sgDebugClass m_class;
54         sgDebugPriority m_priority;
55 };
56
57 /**
58  * Helper force a console on platforms where it might optional, when
59  * we need to show a console. This basically means Windows at the
60  * moment - on other plaforms it's a no-op
61  */
62 void requestConsole();
63
64 void shutdownLogging();
65
66 } // of namespace simgear
67
68 /**
69  * Class to manage the debug logging stream.
70  */
71 class logstream
72 {
73 public:
74     ~logstream();
75     
76     static void initGlobalLogstream();
77     /**
78      * Set the global log class and priority level.
79      * @param c debug class
80      * @param p priority
81      */
82     void setLogLevels( sgDebugClass c, sgDebugPriority p );
83
84     bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
85
86     void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
87
88     void set_log_priority( sgDebugPriority p);
89     
90     void set_log_classes( sgDebugClass c);
91     
92     sgDebugClass get_log_classes() const;
93     
94     sgDebugPriority get_log_priority() const;
95
96     /**
97      * the core logging method
98      */
99     void log( sgDebugClass c, sgDebugPriority p,
100             const char* fileName, int line, const std::string& msg);
101
102     /**
103      * support for the SG_POPUP logging class
104      * set the content of the popup message
105      */
106     void popup( const std::string& msg);
107
108     /**
109      * retrieve the contents of the popup message and clear it's internal
110      * content. The return value may be an empty string.
111      */
112     std::string get_popup();
113
114     /**
115      * return true if a new popup message is available. false otherwise.
116      */
117     bool has_popup();
118
119    /**
120     * \relates logstream
121     * Return the one and only logstream instance.
122     * We use a function instead of a global object so we are assured that cerr
123     * has been initialised.
124     * @return current logstream
125     */
126     friend logstream& sglog();
127     
128     /**
129      * register a logging callback. Note callbacks are run in a
130      * dedicated thread, so callbacks which pass data to other threads
131      * must use appropriate locking.
132      */
133     void addCallback(simgear::LogCallback* cb);
134      
135     void removeCallback(simgear::LogCallback* cb);
136
137 private:
138     // constructor
139     logstream();
140
141     std::vector<std::string> popup_msgs;
142 };
143
144 logstream& sglog();
145
146
147
148 /** \def SG_LOG(C,P,M)
149  * Log a message.
150  * @param C debug class
151  * @param P priority
152  * @param M message
153  */
154 # define SG_LOGX(C,P,M) \
155     do { if(sglog().would_log(C,P)) {                         \
156         std::ostringstream os; os << M;                  \
157         sglog().log(C, P, __FILE__, __LINE__, os.str()); \
158         if (P == SG_POPUP) sglog().popup(os.str());      \
159     } } while(0)
160 #ifdef FG_NDEBUG
161 # define SG_LOG(C,P,M)  do { if(P == SG_POPUP) SG_LOGX(C,P,M) } while(0)
162 #else
163 # define SG_LOG(C,P,M)  SG_LOGX(C,P,M)
164 #endif
165
166 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
167
168 #endif // _LOGSTREAM_H
169