logstream improvements from Yon Uriarte

Avoid descending into iostream when a message won't be logged.
This commit is contained in:
timoore
2008-11-29 00:17:29 +00:00
parent fa7490fb38
commit ae5297e6d7
2 changed files with 35 additions and 8 deletions
+15 -2
View File
@@ -34,6 +34,17 @@ sgDebugClass logbuf::logClass = SG_NONE;
sgDebugPriority logbuf::logPriority = SG_INFO; sgDebugPriority logbuf::logPriority = SG_INFO;
streambuf* logbuf::sbuf = NULL; streambuf* logbuf::sbuf = NULL;
namespace {
struct ignore_me
{
ignore_me()
{
logstream::initGlobalLogstream();
}
};
static ignore_me im;
}
logbuf::logbuf() logbuf::logbuf()
{ {
// if ( sbuf == NULL ) // if ( sbuf == NULL )
@@ -92,10 +103,12 @@ logstream::setLogLevels( sgDebugClass c, sgDebugPriority p )
logbuf::set_log_level( c, p ); logbuf::set_log_level( c, p );
} }
void logstream *
logstream::initGlobalLogstream() logstream::initGlobalLogstream()
{ {
// Force initialization of cerr. // Force initialization of cerr.
static std::ios_base::Init initializer; static std::ios_base::Init initializer;
global_logstream = new logstream(std::cerr); if( !global_logstream )
global_logstream = new logstream(std::cerr);
return global_logstream;
} }
+20 -6
View File
@@ -78,6 +78,8 @@ public:
*/ */
void set_log_state( sgDebugClass c, sgDebugPriority p ); void set_log_state( sgDebugClass c, sgDebugPriority p );
bool would_log( sgDebugClass c, sgDebugPriority p ) const;
/** /**
* Set the global logging level. * Set the global logging level.
* @param c debug class * @param c debug class
@@ -164,6 +166,12 @@ logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
logging_enabled = ((c & logClass) != 0 && p >= logPriority); logging_enabled = ((c & logClass) != 0 && p >= logPriority);
} }
inline bool
logbuf::would_log( sgDebugClass c, sgDebugPriority p ) const
{
return ((c & logClass) != 0 && p >= logPriority);
}
inline logbuf::int_type inline logbuf::int_type
logbuf::overflow( int c ) logbuf::overflow( int c )
{ {
@@ -240,15 +248,20 @@ public:
*/ */
void setLogLevels( sgDebugClass c, sgDebugPriority p ); void setLogLevels( sgDebugClass c, sgDebugPriority p );
bool would_log( sgDebugClass c, sgDebugPriority p ) const
{
return lbuf.would_log( c, p );
};
/** /**
* Output operator to capture the debug level and priority of a message. * Output operator to capture the debug level and priority of a message.
* @param l log level * @param l log level
*/ */
inline std::ostream& operator<< ( const loglevel& l ); inline std::ostream& operator<< ( const loglevel& l );
friend logstream& sglog(); friend logstream& sglog();
static logstream *initGlobalLogstream();
protected: protected:
static logstream *global_logstream; static logstream *global_logstream;
static void initGlobalLogstream();
}; };
inline std::ostream& inline std::ostream&
@@ -268,10 +281,7 @@ logstream::operator<< ( const loglevel& l )
inline logstream& inline logstream&
sglog() sglog()
{ {
if (logstream::global_logstream == NULL) { return *logstream::initGlobalLogstream();
logstream::initGlobalLogstream();
}
return *logstream::global_logstream;
} }
@@ -284,7 +294,11 @@ sglog()
#ifdef FG_NDEBUG #ifdef FG_NDEBUG
# define SG_LOG(C,P,M) # define SG_LOG(C,P,M)
#else #else
# define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl # define SG_LOG(C,P,M) do { \
logstream& __tmplogstreamref(sglog()); \
if(__tmplogstreamref.would_log(C,P)) { \
__tmplogstreamref << loglevel(C,P) << M << std::endl; } \
} while(0)
#endif #endif
#define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__) #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)