]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
Windows logging tweaks.
[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 <vector>
32 #include <sstream>
33 #include <memory> // for std::auto_ptr
34      
35 typedef std::vector<std::string> string_list;
36  
37 // forward decls
38 class SGPath;
39       
40 namespace simgear
41 {
42      
43 class LogCallback
44 {
45 public:
46     virtual ~LogCallback() {}
47     virtual void operator()(sgDebugClass c, sgDebugPriority p, 
48         const char* file, int line, const std::string& aMessage) = 0;
49 };
50      
51      
52 class BufferedLogCallback : public LogCallback
53 {
54 public:
55     BufferedLogCallback(sgDebugClass c, sgDebugPriority p);
56     virtual ~BufferedLogCallback();
57     
58     virtual void operator()(sgDebugClass c, sgDebugPriority p, 
59         const char* file, int line, const std::string& aMessage);
60     
61     /**
62      * copy the buffered log data into the provided output list
63      * (which will be cleared first). This method is safe to call from
64      * any thread.
65      */
66     void threadsafeCopy(string_list& aOutput);
67 private:
68     class BufferedLogCallbackPrivate;
69     std::auto_ptr<BufferedLogCallbackPrivate> d;
70 };
71      
72 } // of namespace simgear
73
74 /**
75  * Class to manage the debug logging stream.
76  */
77 class logstream
78 {
79 public:
80     static void initGlobalLogstream();
81     /**
82      * Set the global log class and priority level.
83      * @param c debug class
84      * @param p priority
85      */
86     void setLogLevels( sgDebugClass c, sgDebugPriority p );
87
88     bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
89
90     void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
91
92     void set_log_priority( sgDebugPriority p);
93     
94     void set_log_classes( sgDebugClass c);
95     
96     sgDebugClass get_log_classes() const;
97     
98     sgDebugPriority get_log_priority() const;
99
100     /**
101      * the core logging method
102      */
103     void log( sgDebugClass c, sgDebugPriority p,
104             const char* fileName, int line, const std::string& msg);
105
106    /**
107     * \relates logstream
108     * Return the one and only logstream instance.
109     * We use a function instead of a global object so we are assured that cerr
110     * has been initialised.
111     * @return current logstream
112     */
113     friend logstream& sglog();
114     
115     /**
116      * register a logging callback. Note callbacks are run in a
117      * dedicated thread, so callbacks which pass data to other threads
118      * must use appropriate locking.
119      */
120     void addCallback(simgear::LogCallback* cb);
121     
122   //  friend logstream& sglog();
123 //    static logstream *initGlobalLogstream();
124
125 private:
126     // constructor
127     logstream();
128 };
129
130 logstream& sglog();
131
132 /** \def SG_LOG(C,P,M)
133  * Log a message.
134  * @param C debug class
135  * @param P priority
136  * @param M message
137  */
138 #ifdef FG_NDEBUG
139 # define SG_LOG(C,P,M)
140 #else
141 # define SG_LOG(C,P,M) do {                     \
142         if(sglog().would_log(C,P)) {                      \
143         std::ostringstream os;                             \
144         os << M;                                       \
145         sglog().log(C, P, __FILE__, __LINE__, os.str()); \
146     } \
147 } while(0)
148 #endif
149
150 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
151
152 #endif // _LOGSTREAM_H
153