From: Reinhard Pfau Date: Mon, 20 Oct 2008 08:45:59 +0000 (+0000) Subject: libi2ncommon: (reinhard) updated spacing for logfunc. X-Git-Tag: v2.6~147 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=f61aeac1e4d7a08983c97c95dc9f65639c012131;p=libi2ncommon libi2ncommon: (reinhard) updated spacing for logfunc. --- diff --git a/src/logfunc.cpp b/src/logfunc.cpp index 7224b38..fff1c03 100644 --- a/src/logfunc.cpp +++ b/src/logfunc.cpp @@ -63,14 +63,14 @@ boost::shared_ptr< std::ofstream > g_log_stream_ptr; * @brief lookup array for translating our log levels to syslog level. */ int loglevel_2_syslog_level[ LogLevel::_LogLevel_END ] = { - LOG_EMERG, - LOG_ALERT, - LOG_CRIT, - LOG_ERR, - LOG_WARNING, - LOG_NOTICE, - LOG_INFO, - LOG_DEBUG + LOG_EMERG, + LOG_ALERT, + LOG_CRIT, + LOG_ERR, + LOG_WARNING, + LOG_NOTICE, + LOG_INFO, + LOG_DEBUG }; @@ -80,14 +80,14 @@ int loglevel_2_syslog_level[ LogLevel::_LogLevel_END ] = { * These tags are used when logs are written to stderr or into a log file. */ std::string loglevel_2_short_tag[ LogLevel::_LogLevel_END ] = { - "EMRG", - "ALRT", - "CRIT", - "ERR ", - "WARN", - "NOTE", - "INFO", - "DBUG" + "EMRG", + "ALRT", + "CRIT", + "ERR ", + "WARN", + "NOTE", + "INFO", + "DBUG" }; @@ -109,16 +109,16 @@ char* syslog_ident= NULL; */ void close_syslog() { - if (g_syslog_opened) - { - closelog(); - if (syslog_ident) - { - free(syslog_ident); - syslog_ident= NULL; - } - g_syslog_opened= false; - } + if (g_syslog_opened) + { + closelog(); + if (syslog_ident) + { + free(syslog_ident); + syslog_ident= NULL; + } + g_syslog_opened= false; + } } // eo close_syslog() @@ -127,10 +127,10 @@ void close_syslog() */ void open_syslog() { - close_syslog(); - syslog_ident= strdup(g_ident.c_str()); - openlog( syslog_ident, LOG_CONS|LOG_PID, g_facility); - g_syslog_opened= true; + close_syslog(); + syslog_ident= strdup(g_ident.c_str()); + openlog( syslog_ident, LOG_CONS|LOG_PID, g_facility); + g_syslog_opened= true; } // eo open_syslog() @@ -141,11 +141,11 @@ void open_syslog() */ int get_syslog_level( int level ) { - if (level >=0 && level <= LogLevel::_LogLevel_END) - { - return loglevel_2_syslog_level[level]; - } - return (level<0) ? (LOG_EMERG) : (LOG_DEBUG); + if (level >=0 && level <= LogLevel::_LogLevel_END) + { + return loglevel_2_syslog_level[level]; + } + return (level<0) ? (LOG_EMERG) : (LOG_DEBUG); } // eo get_syslog_level(int) @@ -156,11 +156,11 @@ int get_syslog_level( int level ) */ std::string get_level_tag( int level ) { - if (level >=0 && level <= LogLevel::_LogLevel_END) - { - return loglevel_2_short_tag[level]; - } - return (level<0) ? loglevel_2_short_tag[0] : loglevel_2_short_tag[ LogLevel::_LogLevel_END -1 ]; + if (level >=0 && level <= LogLevel::_LogLevel_END) + { + return loglevel_2_short_tag[level]; + } + return (level<0) ? loglevel_2_short_tag[0] : loglevel_2_short_tag[ LogLevel::_LogLevel_END -1 ]; } // eo get_level_tag(int) @@ -179,64 +179,64 @@ std::string get_level_tag( int level ) */ void log_msg( int level, const std::string& msg) { - if (not g_syslog_opened and not g_stderr_log and not g_log_stream_ptr) - { - // if nothing is opened for logging: we activate syslog! - enable_syslog(true); - } - - if (g_syslog_opened) - { - ::syslog( get_syslog_level(level), "%s", msg.c_str()); - } - // file(/stream) logging: - if (g_stderr_log or g_log_stream_ptr) // add more log "enabled" expressions here... - { - // here we need must do something more with the msg... - std::string new_msg; - std::string prefix; - { - std::ostringstream ostr; - // add time stamp (syslog like: "Mon DD HH:MM:SS") : - { - time_t t = time(NULL); - char buffer[32]; - std::strftime(buffer, sizeof(buffer),"%b %d %H:%M:%S ", std::localtime(&t)); - ostr << buffer; - } - ostr << get_level_tag(level) << " "; - ostr << g_ident << "[" << getpid() << "]: "; - prefix= ostr.str(); - } - { - { - std::string indent_string(prefix.size(), ' '); - std::list< std::string > parts; - split_string( chomp(msg,"\n"), parts, "\n"); + if (not g_syslog_opened and not g_stderr_log and not g_log_stream_ptr) + { + // if nothing is opened for logging: we activate syslog! + enable_syslog(true); + } + + if (g_syslog_opened) + { + ::syslog( get_syslog_level(level), "%s", msg.c_str()); + } + // file(/stream) logging: + if (g_stderr_log or g_log_stream_ptr) // add more log "enabled" expressions here... + { + // here we need must do something more with the msg... + std::string new_msg; + std::string prefix; + { std::ostringstream ostr; - ostr << prefix; - for(std::list< std::string >::const_iterator it= parts.begin(); - it != parts.end(); - ++it) + // add time stamp (syslog like: "Mon DD HH:MM:SS") : { - if (it != parts.begin()) - { - ostr << indent_string; - } - ostr << *it << std::endl; + time_t t = time(NULL); + char buffer[32]; + std::strftime(buffer, sizeof(buffer),"%b %d %H:%M:%S ", std::localtime(&t)); + ostr << buffer; } - new_msg= ostr.str(); - } - } - if (g_stderr_log) - { - std::cerr << new_msg; - } - if (g_log_stream_ptr) - { - *g_log_stream_ptr << new_msg << std::flush; - } - } + ostr << get_level_tag(level) << " "; + ostr << g_ident << "[" << getpid() << "]: "; + prefix= ostr.str(); + } + { + { + std::string indent_string(prefix.size(), ' '); + std::list< std::string > parts; + split_string( chomp(msg,"\n"), parts, "\n"); + std::ostringstream ostr; + ostr << prefix; + for(std::list< std::string >::const_iterator it= parts.begin(); + it != parts.end(); + ++it) + { + if (it != parts.begin()) + { + ostr << indent_string; + } + ostr << *it << std::endl; + } + new_msg= ostr.str(); + } + } + if (g_stderr_log) + { + std::cerr << new_msg; + } + if (g_log_stream_ptr) + { + *g_log_stream_ptr << new_msg << std::flush; + } + } } // eo log_msg @@ -249,38 +249,38 @@ void log_msg( int level, const std::string& msg) * basically calls @a log(), but prepends the part (if not empty) in square brackets to the message. */ void log_part_msg( - int level, - const std::string& part, - const std::string& msg) -{ - if (!part.empty()) - { - std::ostringstream ostr; - ostr << "[" << part << "] " << msg; - log_msg(level, ostr.str()); - } - else - { - log_msg(level, msg); - } + int level, + const std::string& part, + const std::string& msg) +{ + if (!part.empty()) + { + std::ostringstream ostr; + ostr << "[" << part << "] " << msg; + log_msg(level, ostr.str()); + } + else + { + log_msg(level, msg); + } } // eo log_part_msg(int,const std::string&,const std::string&) void _cleanup() { - close_syslog(); - //TODO other cleanups? + close_syslog(); + //TODO other cleanups? } // _cleanup class __Initializer { - public: - __Initializer() - { - std::atexit( _cleanup ); - } + public: + __Initializer() + { + std::atexit( _cleanup ); + } } __initialize; @@ -322,7 +322,7 @@ PartLogger::LogHelper::LogHelper(PartLogger& logger, int level, const SourceLoca , Level(level) , Location(loc) { - StreamPtr.reset(new std::ostringstream()); + StreamPtr.reset(new std::ostringstream()); } // eo PartLogger::LogHelper::LogHelper(PartLogger&,int) PartLogger::LogHelper::LogHelper(const LogHelper& helper) @@ -336,19 +336,19 @@ PartLogger::LogHelper::LogHelper(const LogHelper& helper) PartLogger::LogHelper::~LogHelper() { - if (StreamPtr.get()) - { - if (Location) - { - //*m_stream_ptr << " at " << m_loc.Line << " in " << m_loc.FunctionName; - *StreamPtr << " @" << Location.get_location_tag(); - } - std::string msg(StreamPtr->str()); - if (!msg.empty()) - { - Logger.log(Level,msg); - } - } + if (StreamPtr.get()) + { + if (Location) + { + //*m_stream_ptr << " at " << m_loc.Line << " in " << m_loc.FunctionName; + *StreamPtr << " @" << Location.get_location_tag(); + } + std::string msg(StreamPtr->str()); + if (!msg.empty()) + { + Logger.log(Level,msg); + } + } } // eo PartLogger::LogHelper::~LogHelper @@ -376,19 +376,19 @@ PartLogger::PartLogger(const std::string& part) */ PartLogger::PartLogger( const SourceLocation& loc ) { - if (loc.Line>0 && ! loc.File.empty()) - { - std::string str= basename(loc.File); - Part= remove_suffix(str,".cpp"); - if (Part == str) Part= remove_suffix(str,".cxx"); - if (Part == str) Part= remove_suffix(str,".c++"); - if (Part == str) Part= remove_suffix(str,".cc"); - if (Part == str) Part= remove_suffix(str,".C"); - } - else - { - Part="Unknown"; - } + if (loc.Line>0 && ! loc.File.empty()) + { + std::string str= basename(loc.File); + Part= remove_suffix(str,".cpp"); + if (Part == str) Part= remove_suffix(str,".cxx"); + if (Part == str) Part= remove_suffix(str,".c++"); + if (Part == str) Part= remove_suffix(str,".cc"); + if (Part == str) Part= remove_suffix(str,".C"); + } + else + { + Part="Unknown"; + } }// PartLogger::PartLogger(const SourceLocation&) @@ -404,107 +404,107 @@ PartLogger::~PartLogger() */ void PartLogger::log(int level, const std::string msg) { - if (level <= g_max_level) - { - log_part_msg(level, Part, msg); - } + if (level <= g_max_level) + { + log_part_msg(level, Part, msg); + } } // eo PartLogger::log(int,const std::string); void PartLogger::fatal(const std::string& msg) { - log(LOG_EMERG,msg); + log(LOG_EMERG,msg); } // eo PartLogger::fatal(const std::string&) void PartLogger::alert(const std::string& msg) { - log(LOG_ALERT,msg); + log(LOG_ALERT,msg); } // eo PartLogger::alert(const std::string&) void PartLogger::critical(const std::string& msg) { - log(LOG_CRIT,msg); + log(LOG_CRIT,msg); } // eo PartLogger::critical(const std::string&) void PartLogger::error(const std::string& msg) { - log(LOG_ERR, msg); + log(LOG_ERR, msg); } // eo PartLogger::error(const std::string&) void PartLogger::warning(const std::string& msg) { - log(LOG_WARNING, msg); + log(LOG_WARNING, msg); } // eo PartLogger::warning(const std::string&) void PartLogger::notice(const std::string& msg) { - log(LOG_NOTICE, msg); + log(LOG_NOTICE, msg); } // eo PartLogger::notice(const std::string&) void PartLogger::info(const std::string& msg) { - log(LOG_INFO, msg); + log(LOG_INFO, msg); } // eo PartLogger::info(const std::string&) void PartLogger::debug(const std::string& msg) { - log(LOG_DEBUG, msg); + log(LOG_DEBUG, msg); } // eo PartLogger::debug(const std::string&) PartLogger::LogHelper PartLogger::fatal(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_EMERG,loc); + return PartLogger::LogHelper(*this,LOG_EMERG,loc); } // eo PartLogger::fatal(const SourceLocation&) PartLogger::LogHelper PartLogger::alert(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_ALERT,loc); + return PartLogger::LogHelper(*this,LOG_ALERT,loc); } // eo PartLogger::alert(const SourceLocation&) PartLogger::LogHelper PartLogger::critical(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_CRIT,loc); + return PartLogger::LogHelper(*this,LOG_CRIT,loc); } // eo PartLogger::critical(const SourceLocation&) PartLogger::LogHelper PartLogger::error(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_ERR,loc); + return PartLogger::LogHelper(*this,LOG_ERR,loc); } // eo PartLogger::error(const SourceLocation&) PartLogger::LogHelper PartLogger::warning(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_WARNING,loc); + return PartLogger::LogHelper(*this,LOG_WARNING,loc); } // eo PartLogger::warning(const SourceLocation&) PartLogger::LogHelper PartLogger::notice(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_NOTICE,loc); + return PartLogger::LogHelper(*this,LOG_NOTICE,loc); } // eo PartLogger::notice(const SourceLocation&) PartLogger::LogHelper PartLogger::info(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_INFO,loc); + return PartLogger::LogHelper(*this,LOG_INFO,loc); } // eo PartLogger::info(const SourceLocation&) PartLogger::LogHelper PartLogger::debug(const SourceLocation& loc) { - return PartLogger::LogHelper(*this,LOG_DEBUG,loc); + return PartLogger::LogHelper(*this,LOG_DEBUG,loc); } // eo PartLogger::debug(const SourceLocation&) /* @@ -519,10 +519,10 @@ PartLogger::LogHelper PartLogger::debug(const SourceLocation& loc) */ void enable_syslog( const std::string& name, Facility facility ) { - close_syslog(); - g_ident= name; - g_facility= facility; - open_syslog(); + close_syslog(); + g_ident= name; + g_facility= facility; + open_syslog(); } // eo enable_syslog(const std::string,Facility) @@ -534,24 +534,24 @@ void enable_syslog( const std::string& name, Facility facility ) */ void enable_syslog( Facility facility ) { - if (g_ident.empty()) - { - // determine the program name: - std::string exe_path; - { - std::ostringstream ostr; - ostr << "/proc/" << ::getpid() << "/exe"; - exe_path= ostr.str(); - } - std::string binary_path= read_link(exe_path); - if (!binary_path.empty()) - { - g_ident= basename(binary_path); - } - } - close_syslog(); - g_facility = facility; - open_syslog(); + if (g_ident.empty()) + { + // determine the program name: + std::string exe_path; + { + std::ostringstream ostr; + ostr << "/proc/" << ::getpid() << "/exe"; + exe_path= ostr.str(); + } + std::string binary_path= read_link(exe_path); + if (!binary_path.empty()) + { + g_ident= basename(binary_path); + } + } + close_syslog(); + g_facility = facility; + open_syslog(); } // eo enable_syslog(Facility) @@ -561,17 +561,17 @@ void enable_syslog( Facility facility ) */ void enable_syslog( bool enable ) { - if (enable) - { - if (!g_syslog_opened) - { - enable_syslog( g_facility ); - } - } - else // ! enable - { - close_syslog(); - } + if (enable) + { + if (!g_syslog_opened) + { + enable_syslog( g_facility ); + } + } + else // ! enable + { + close_syslog(); + } } // eo enable_syslog(bool) @@ -581,7 +581,7 @@ void enable_syslog( bool enable ) */ void enable_stderr_log(bool enable) { - g_stderr_log= enable; + g_stderr_log= enable; } // eo enableStderr; @@ -594,10 +594,10 @@ void enable_stderr_log(bool enable) */ void enable_log_file( const std::string& name ) { - g_log_file_name= name; - g_log_stream_ptr.reset( new std::ofstream() ); - g_log_stream_ptr->open( name.c_str(), std::ios::out|std::ios::app ); - //std::cerr << "### opened \"" << name << "\"" << g_log_stream_ptr->good() << std::endl; + g_log_file_name= name; + g_log_stream_ptr.reset( new std::ofstream() ); + g_log_stream_ptr->open( name.c_str(), std::ios::out|std::ios::app ); + //std::cerr << "### opened \"" << name << "\"" << g_log_stream_ptr->good() << std::endl; } // eo enable_log_file(const std::string&) @@ -609,17 +609,17 @@ void enable_log_file( const std::string& name ) */ void enable_log_file( bool enable ) { - if (enable) - { - if (! g_log_file_name.empty()) - { - enable_log_file( g_log_file_name ); - } - } - else // ! enable - { - g_log_stream_ptr.reset(); - } + if (enable) + { + if (! g_log_file_name.empty()) + { + enable_log_file( g_log_file_name ); + } + } + else // ! enable + { + g_log_stream_ptr.reset(); + } } // eo enable_log_file(bool) @@ -644,9 +644,9 @@ void reopen() */ int set_log_level(int level) { - int result = g_max_level; - g_max_level = std::max( LOG_CRIT, level ); - return result; + int result = g_max_level; + g_max_level = std::max( LOG_CRIT, level ); + return result; } // eo set_log_level(int) @@ -656,7 +656,7 @@ int set_log_level(int level) */ int get_log_level() { - return g_max_level; + return g_max_level; } // eo get_log_level() @@ -668,7 +668,7 @@ int get_log_level() */ bool has_log_level(int level) { - return (g_max_level >= level); + return (g_max_level >= level); } // eo has_log_level(int) diff --git a/src/logfunc.hpp b/src/logfunc.hpp index 15110de..a6e0be4 100644 --- a/src/logfunc.hpp +++ b/src/logfunc.hpp @@ -44,29 +44,29 @@ namespace Logger struct Facility { - static const int AuthPriv; - static const int Cron; - static const int Daemon; - static const int Kern; - static const int Mail; - static const int News; - static const int Syslog; - static const int User; - static const int UUCP; - static const int Local0; - static const int Local1; - static const int Local2; - static const int Local3; - static const int Local4; - static const int Local5; - static const int Local6; - static const int Local7; - - int m_facility; - - Facility(int facility = Facility::User) : m_facility(facility) {} - - operator int() const { return m_facility; } + static const int AuthPriv; + static const int Cron; + static const int Daemon; + static const int Kern; + static const int Mail; + static const int News; + static const int Syslog; + static const int User; + static const int UUCP; + static const int Local0; + static const int Local1; + static const int Local2; + static const int Local3; + static const int Local4; + static const int Local5; + static const int Local6; + static const int Local7; + + int m_facility; + + Facility(int facility = Facility::User) : m_facility(facility) {} + + operator int() const { return m_facility; } }; // eo Facility @@ -75,23 +75,23 @@ struct Facility */ struct LogLevel { - enum { - Emergency = 0, Fatal= Emergency, - Alert, - Critical, - Error, - Warning, - Notice, - Info, - Debug, - _LogLevel_END - }; - - int m_level; - - LogLevel(int level= Warning) : m_level(level) {} - - operator int() const { return m_level; } + enum { + Emergency = 0, Fatal= Emergency, + Alert, + Critical, + Error, + Warning, + Notice, + Info, + Debug, + _LogLevel_END + }; + + int m_level; + + LogLevel(int level= Warning) : m_level(level) {} + + operator int() const { return m_level; } }; // eo struct LogLevel @@ -101,66 +101,66 @@ struct LogLevel */ class PartLogger { - public: - - class LogHelper - { - public: - virtual ~LogHelper(); - - template< typename T > - std::ostream& operator << (T v) - { - if (StreamPtr.get()) - { - return *StreamPtr << v; - } - throw std::logic_error("pointer vanished"); - } // eo operator <<(T) - - protected: - friend class PartLogger; - - LogHelper(PartLogger& logger, int level, const SourceLocation& loc); - LogHelper(const LogHelper& helper); - - protected: - - PartLogger& Logger; - int Level; - SourceLocation Location; - mutable std::auto_ptr< std::ostringstream > StreamPtr; - }; // eo class LogHelper - - - public: - PartLogger( const std::string& part ); - PartLogger( const SourceLocation& loc ); - virtual ~PartLogger(); - - void log(int level, const std::string msg); - - void fatal(const std::string& msg); - void alert(const std::string& msg); - void critical(const std::string& msg); - void error(const std::string& msg); - void warning(const std::string& msg); - void notice(const std::string& msg); - void info(const std::string& msg); - void debug(const std::string& msg); - - LogHelper fatal(const SourceLocation& loc= SourceLocation()); - LogHelper alert(const SourceLocation& loc= SourceLocation()); - LogHelper critical(const SourceLocation& loc= SourceLocation()); - LogHelper error(const SourceLocation& loc= SourceLocation()); - LogHelper warning(const SourceLocation& loc= SourceLocation()); - LogHelper notice(const SourceLocation& loc= SourceLocation()); - LogHelper info(const SourceLocation& loc= SourceLocation()); - LogHelper debug(const SourceLocation& loc= SourceLocation()); - - protected: - - std::string Part; + public: + + class LogHelper + { + public: + virtual ~LogHelper(); + + template< typename T > + std::ostream& operator << (T v) + { + if (StreamPtr.get()) + { + return *StreamPtr << v; + } + throw std::logic_error("pointer vanished"); + } // eo operator <<(T) + + protected: + friend class PartLogger; + + LogHelper(PartLogger& logger, int level, const SourceLocation& loc); + LogHelper(const LogHelper& helper); + + protected: + + PartLogger& Logger; + int Level; + SourceLocation Location; + mutable std::auto_ptr< std::ostringstream > StreamPtr; + }; // eo class LogHelper + + + public: + PartLogger( const std::string& part ); + PartLogger( const SourceLocation& loc ); + virtual ~PartLogger(); + + void log(int level, const std::string msg); + + void fatal(const std::string& msg); + void alert(const std::string& msg); + void critical(const std::string& msg); + void error(const std::string& msg); + void warning(const std::string& msg); + void notice(const std::string& msg); + void info(const std::string& msg); + void debug(const std::string& msg); + + LogHelper fatal(const SourceLocation& loc= SourceLocation()); + LogHelper alert(const SourceLocation& loc= SourceLocation()); + LogHelper critical(const SourceLocation& loc= SourceLocation()); + LogHelper error(const SourceLocation& loc= SourceLocation()); + LogHelper warning(const SourceLocation& loc= SourceLocation()); + LogHelper notice(const SourceLocation& loc= SourceLocation()); + LogHelper info(const SourceLocation& loc= SourceLocation()); + LogHelper debug(const SourceLocation& loc= SourceLocation()); + + protected: + + std::string Part; }; // eo class PartLogger @@ -200,7 +200,7 @@ bool has_log_level(int level); inline void enable_syslog( const char* name, Facility facility= Facility::User ) { - enable_syslog( std::string(name), facility); + enable_syslog( std::string(name), facility); } inline void enable_log_file( const char* name) { enable_log_file( std::string(name) ); } diff --git a/test/stringfunc.cpp b/test/stringfunc.cpp index 043c58f..50ae643 100644 --- a/test/stringfunc.cpp +++ b/test/stringfunc.cpp @@ -49,6 +49,8 @@ class stringfunc : public TestFixture CPPUNIT_TEST(PairSplit1); CPPUNIT_TEST(SplitString1); CPPUNIT_TEST(SplitString2); + CPPUNIT_TEST(SplitStringEmpty); + CPPUNIT_TEST(SplitStringDelimiterOnly); CPPUNIT_TEST(JoinString1); CPPUNIT_TEST(ConversionStringInt); @@ -378,6 +380,31 @@ class stringfunc : public TestFixture CPPUNIT_ASSERT_EQUAL( 7u, list1.size() ); } // eo SplitString2 + + + + void SplitStringEmpty() + { + std::string line(""); + + StringList list1; + + split_string(line, list1, " ", true, Whitespaces); + + CPPUNIT_ASSERT_EQUAL( 0u, list1.size() ); + } // eo SplitStringEmpty + + + void SplitStringDelimiterOnly() + { + std::string line(" "); + + StringList list1; + + split_string(line, list1, " ", true, Whitespaces); + + CPPUNIT_ASSERT_EQUAL( 0u, list1.size() ); + } // eo SplitStringDelimiterOnly