/* $Id: LogBuffer.h 15564 2013-01-07 14:25:32Z sloot $ $URL: https://ilk.uvt.nl/svn/sources/libticcutils/trunk/include/ticcutils/LogBuffer.h $ Copyright (c) 1998 - 2013 ILK - Tilburg University CLiPS - University of Antwerp This file is part of ticcutils ticcutils is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ticcutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . For questions and suggestions, see: http://ilk.uvt.nl/software.html or send mail to: timbl@uvt.nl */ #ifndef TICC_LOGBUFFER_H #define TICC_LOGBUFFER_H #include #include #include #include #include #include #include #include enum LogLevel{ LogSilent, LogNormal, LogDebug, LogHeavy, LogExtreme }; enum LogFlag { NoStamp=0, StampTime=1, StampMessage=2, StampBoth=3 }; template > class basic_log_buffer : public std::basic_streambuf { public: basic_log_buffer( std::basic_ostream&, const std::string& = "", const LogFlag = StampBoth ); ~basic_log_buffer(); // // setters/getters LogLevel Level() const; void Level( const LogLevel l ); LogLevel Threshold() const; void Threshold( const LogLevel l ); const std::string& Message() const; void Message( const std::string& ); std::basic_ostream& AssocStream() const; void AssocStream( std::basic_ostream& ); LogFlag StampFlag() const; void StampFlag( const LogFlag ); protected: int sync(); int overflow( int ); private: std::basic_ostream *ass_stream; LogFlag stamp_flag; bool in_sync; LogLevel level; LogLevel threshold_level; std::string ass_mess; void buffer_out(); // prohibit copying and assignment basic_log_buffer( const basic_log_buffer& ); basic_log_buffer& operator=( const basic_log_buffer& ); }; typedef basic_log_buffer > LogBuffer; typedef basic_log_buffer > wLogBuffer; template basic_log_buffer::basic_log_buffer( std::basic_ostream& a, const std::string& mess, const LogFlag stamp ) { ass_stream = &a; ass_mess = mess; stamp_flag = stamp; in_sync = true; level = LogNormal; threshold_level = LogSilent; } template basic_log_buffer::~basic_log_buffer(){ sync(); } inline long millitm() { struct timeval tp; gettimeofday(&tp,NULL); return tp.tv_usec/1000; } inline std::string time_stamp(){ char time_line[50]; time_t lTime; struct tm *curtime; time(&lTime); struct tm tmp; curtime = localtime_r(&lTime,&tmp); strftime( time_line, 45, "%Y%m%d:%H%M%S", curtime ); sprintf( time_line+strlen(time_line), ":%03ld:", millitm() ); return time_line; } // // for a derived output stream, we must provide implementations for // both overflow and sync. // both use a helper function buffer_out to do the real work. // template int basic_log_buffer::overflow( int c ) { buffer_out(); if ( level > threshold_level && c != '\r' ){ if ( c != EOF ){ char z = static_cast(c); ass_stream->put( z ); } else return EOF; } return c; } template int basic_log_buffer::sync() { ass_stream->flush(); in_sync = true; return 0; } template void basic_log_buffer::buffer_out(){ if ( level > threshold_level ){ // only output when we are on a high enough level if ( in_sync ) { // stamps and messages are only displayed when in sync // that is: when we have had a newline and NOT when we just // overflowed due to a long line if ( stamp_flag & StampTime ){ *ass_stream << time_stamp(); } if ( !ass_mess.empty() && ( stamp_flag & StampMessage ) ) *ass_stream << ass_mess << ":"; in_sync = false; } } } // // Getters and Setters for the private parts.. // template const std::string& basic_log_buffer::Message() const { return ass_mess; } template void basic_log_buffer::Message( const std::string& s ){ ass_mess = s; } template void basic_log_buffer::Threshold( LogLevel l ){ if ( threshold_level != l ){ threshold_level = l; } } template LogLevel basic_log_buffer::Threshold() const { return threshold_level; } template void basic_log_buffer::Level( LogLevel l ){ if ( level != l ){ level = l; } } template LogLevel basic_log_buffer::Level() const { return level; } template std::basic_ostream& basic_log_buffer::AssocStream() const { return *ass_stream; } template void basic_log_buffer::AssocStream( std::basic_ostream& os ){ ass_stream = &os; } template void basic_log_buffer::StampFlag( const LogFlag b ){ if ( stamp_flag != b ){ stamp_flag = b; } } template LogFlag basic_log_buffer::StampFlag() const { return stamp_flag; } #endif // LOGBUFFER_H