1 /** \file logstream.hxx
2 * Stream based logging mechanism.
5 // Written by Bernie Bright, 1998
7 // Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
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.
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.
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.
28 #include <simgear/compiler.h>
29 #include <simgear/debug/debug_types.h>
33 #include <memory> // for std::auto_ptr
35 typedef std::vector<std::string> string_list;
46 virtual ~LogCallback() {}
47 virtual void operator()(sgDebugClass c, sgDebugPriority p,
48 const char* file, int line, const std::string& aMessage) = 0;
52 class BufferedLogCallback : public LogCallback
55 BufferedLogCallback(sgDebugClass c, sgDebugPriority p);
56 virtual ~BufferedLogCallback();
58 virtual void operator()(sgDebugClass c, sgDebugPriority p,
59 const char* file, int line, const std::string& aMessage);
62 * copy the buffered log data into the provided output list
63 * (which will be cleared first). This method is safe to call from
66 void threadsafeCopy(string_list& aOutput);
68 class BufferedLogCallbackPrivate;
69 std::auto_ptr<BufferedLogCallbackPrivate> d;
72 } // of namespace simgear
75 * Class to manage the debug logging stream.
80 static void initGlobalLogstream();
82 * Set the global log class and priority level.
83 * @param c debug class
86 void setLogLevels( sgDebugClass c, sgDebugPriority p );
88 bool would_log( sgDebugClass c, sgDebugPriority p ) const;
90 void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
92 void set_log_priority( sgDebugPriority p);
94 void set_log_classes( sgDebugClass c);
96 sgDebugClass get_log_classes() const;
98 sgDebugPriority get_log_priority() const;
101 * the core logging method
103 void log( sgDebugClass c, sgDebugPriority p,
104 const char* fileName, int line, const std::string& msg);
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
113 friend logstream& sglog();
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.
120 void addCallback(simgear::LogCallback* cb);
122 // friend logstream& sglog();
123 // static logstream *initGlobalLogstream();
132 /** \def SG_LOG(C,P,M)
134 * @param C debug class
139 # define SG_LOG(C,P,M)
141 # define SG_LOG(C,P,M) do { \
142 if(sglog().would_log(C,P)) { \
143 std::ostringstream os; \
145 sglog().log(C, P, __FILE__, __LINE__, os.str()); \
150 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
152 #endif // _LOGSTREAM_H