* @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
};
* 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"
};
*/
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()
*/
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()
*/
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)
*/
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)
*/
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
* 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;
, 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)
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
*/
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&)
*/
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&)
/*
*/
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)
*/
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)
*/
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)
*/
void enable_stderr_log(bool enable)
{
- g_stderr_log= enable;
+ g_stderr_log= enable;
} // eo enableStderr;
*/
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&)
*/
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)
*/
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)
*/
int get_log_level()
{
- return g_max_level;
+ return g_max_level;
} // eo get_log_level()
*/
bool has_log_level(int level)
{
- return (g_max_level >= level);
+ return (g_max_level >= level);
} // eo has_log_level(int)
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
*/
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
*/
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
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) ); }