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>
43 virtual ~LogCallback() {}
44 virtual void operator()(sgDebugClass c, sgDebugPriority p,
45 const char* file, int line, const std::string& aMessage) = 0;
47 void setLogLevels(sgDebugClass c, sgDebugPriority p);
49 LogCallback(sgDebugClass c, sgDebugPriority p);
51 bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
54 sgDebugPriority m_priority;
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
62 void requestConsole();
64 void shutdownLogging();
66 } // of namespace simgear
69 * Class to manage the debug logging stream.
76 static void initGlobalLogstream();
78 * Set the global log class and priority level.
79 * @param c debug class
82 void setLogLevels( sgDebugClass c, sgDebugPriority p );
84 bool would_log( sgDebugClass c, sgDebugPriority p ) const;
86 void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
88 void set_log_priority( sgDebugPriority p);
90 void set_log_classes( sgDebugClass c);
92 sgDebugClass get_log_classes() const;
94 sgDebugPriority get_log_priority() const;
97 * the core logging method
99 void log( sgDebugClass c, sgDebugPriority p,
100 const char* fileName, int line, const std::string& msg);
103 * support for the SG_POPUP logging class
104 * set the content of the popup message
106 void popup( const std::string& msg);
109 * retrieve the contents of the popup message and clear it's internal
110 * content. The return value may be an empty string.
112 std::string get_popup();
115 * return true if a new popup message is available. false otherwise.
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
126 friend logstream& sglog();
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.
133 void addCallback(simgear::LogCallback* cb);
135 void removeCallback(simgear::LogCallback* cb);
141 std::vector<std::string> popup_msgs;
148 /** \def SG_LOG(C,P,M)
150 * @param C debug class
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()); \
161 # define SG_LOG(C,P,M) do { if(P == SG_POPUP) SG_LOGX(C,P,M) } while(0)
163 # define SG_LOG(C,P,M) SG_LOGX(C,P,M)
166 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
168 #endif // _LOGSTREAM_H