2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief implementaton of logging functions.
23 * @copyright © Copyright 2007-2008 by Intra2net AG
25 * @note Don't call loggers from global constructed objects
26 * as this depends on the global object construction sequence.
29 #include "logfunc.hpp"
39 #include <boost/shared_ptr.hpp>
40 #include <boost/thread/recursive_mutex.hpp>
41 #include <boost/thread/thread.hpp>
43 #include <stringfunc.hxx>
44 #include <filefunc.hxx>
53 * @brief the global logger instance.
55 * This may be used in all cases wheer a specialized logger is not available
56 * or shouldn't be used for some reason.
58 PartLogger GlobalLogger("");
64 /// Global lock for the logger. Used during all syslog operations
65 /// and modification of our shared local variables.
66 static boost::recursive_mutex LoggerLock;
72 static std::string g_ident;
73 static Facility g_facility;
74 static bool g_syslog_opened = false;
75 static int g_max_level= LogLevel::Warning;
77 static bool g_stderr_log = false;
79 static std::string g_log_file_name;
80 static boost::shared_ptr< std::ofstream > g_log_stream_ptr;
83 * @brief lookup array for translating our log levels to syslog level.
85 static int loglevel_2_syslog_level[ LogLevel::_LogLevel_END ] = {
98 * @brief lookup array for translating our log levels to a short (4 character) tag.
100 * These tags are used when logs are written to stderr or into a log file.
102 static std::string loglevel_2_short_tag[ LogLevel::_LogLevel_END ] = {
115 * @brief a copy of the current identifier used for syslog.
117 * Keeping a copy of this ident is necessary since openlog doen't copy it's first
118 * argument but copies only the pointer! (what a **censored**!)
120 static char* syslog_ident= NULL;
128 * @brief close syslog.
132 boost::recursive_mutex::scoped_lock lock(LoggerLock);
142 g_syslog_opened= false;
144 } // eo close_syslog()
148 * @brief open syslog.
152 boost::recursive_mutex::scoped_lock lock(LoggerLock);
155 syslog_ident= strdup(g_ident.c_str());
156 openlog( syslog_ident, LOG_CONS|LOG_PID, g_facility);
157 g_syslog_opened= true;
158 } // eo open_syslog()
162 * @brief get syslog level from internal log level.
163 * @param level log level.
164 * @return syslog level
166 int get_syslog_level( int level )
170 if (level >=0 && level < LogLevel::_LogLevel_END)
172 return loglevel_2_syslog_level[level];
174 return (level<0) ? (LOG_EMERG) : (LOG_DEBUG);
175 } // eo get_syslog_level(int)
179 * @brief get a short tag for the log level (/ message type)
180 * @param level the log level
181 * @return a short tag for the level.
183 std::string get_level_tag( int level )
187 if (level >=0 && level < LogLevel::_LogLevel_END)
189 return loglevel_2_short_tag[level];
191 return (level<0) ? loglevel_2_short_tag[0] : loglevel_2_short_tag[ LogLevel::_LogLevel_END -1 ];
192 } // eo get_level_tag(int)
196 * @brief the "real" log function which logs a message at a given level.
197 * @param level the log level to log the message at.
198 * @param msg the message.
200 * Write the message to every enabled log channel.
202 * If syslog is enabled the message is passed unmodified to syslog.
204 * If a stream log is enabled (stderr or file) then the message is prepended with date, time
205 * and process information (like syslog does). The message is splitted at line ends and
206 * consecutive lines are indented.
208 void log_msg( int level, const std::string& msg)
210 boost::recursive_mutex::scoped_lock lock(LoggerLock);
212 if (not g_syslog_opened and not g_stderr_log and not g_log_stream_ptr)
214 // if nothing is opened for logging: we activate syslog!
220 ::syslog( get_syslog_level(level), "%s", msg.c_str());
222 // file(/stream) logging:
223 if (g_stderr_log or g_log_stream_ptr) // add more log "enabled" expressions here...
225 // here we need must do something more with the msg...
229 std::ostringstream ostr;
230 // add time stamp (syslog like: "Mon DD HH:MM:SS") :
232 time_t t = time(NULL);
234 std::strftime(buffer, sizeof(buffer),"%b %d %H:%M:%S ", std::localtime(&t));
237 ostr << get_level_tag(level) << " ";
238 ostr << g_ident << "[" << getpid() << "]: ";
243 std::string indent_string(prefix.size(), ' ');
244 std::list< std::string > parts;
245 split_string( chomp(msg,"\n"), parts, "\n");
246 std::ostringstream ostr;
248 for(std::list< std::string >::const_iterator it= parts.begin();
252 if (it != parts.begin())
254 ostr << indent_string;
256 ostr << *it << std::endl;
263 std::cerr << new_msg;
265 if (g_log_stream_ptr)
267 *g_log_stream_ptr << new_msg << std::flush;
274 * @brief "real" log function for part messages.
275 * @param level the log level.
276 * @param part the part(/module) name(/id)
277 * @param msg the log message.
279 * basically calls @a log(), but prepends the part (if not empty) in square brackets to the message.
283 const std::string& part,
284 const std::string& msg)
286 // Note: Locking is done in log_msg()
290 std::ostringstream ostr;
291 ostr << "[" << part << "] " << msg;
292 log_msg(level, ostr.str());
298 } // eo log_part_msg(int,const std::string&,const std::string&)
302 * @brief returns the name of the program (/binary)
303 * @return the program name if it could be determined; generated name else.
305 * Tries to determine the name of the binary.
307 * If no name could be determined, one is built.
309 std::string get_program_name()
314 // determine the program name:
316 // try to determine the name using the exe link:
317 std::string exe_path;
319 std::ostringstream ostr;
320 ostr << "/proc/" << ::getpid() << "/exe";
321 exe_path= ostr.str();
323 std::string binary_path= read_link(exe_path);
324 if (!binary_path.empty())
326 result= basename(binary_path);
331 // no program name found up to this point.
332 // make a name (as fallback solution):
333 std::ostringstream ostr;
334 ostr << "prg-" << ::getpid();
338 } // eo get_program_name
344 // Note: Locking is done in close_syslog();
346 //TODO other cleanups?
355 std::atexit( _cleanup );
360 } // eo namespace <anonymous>
365 ** implementation of Facility
369 const int Facility::AuthPriv= LOG_AUTH;
370 const int Facility::Cron = LOG_CRON;
371 const int Facility::Daemon = LOG_DAEMON;
372 const int Facility::Kern = LOG_KERN;
373 const int Facility::Mail = LOG_MAIL;
374 const int Facility::News = LOG_NEWS;
375 const int Facility::Syslog = LOG_SYSLOG;
376 const int Facility::User = LOG_USER;
377 const int Facility::UUCP = LOG_UUCP;
378 const int Facility::Local0 = LOG_LOCAL0;
379 const int Facility::Local1 = LOG_LOCAL1;
380 const int Facility::Local2 = LOG_LOCAL2;
381 const int Facility::Local3 = LOG_LOCAL3;
382 const int Facility::Local4 = LOG_LOCAL4;
383 const int Facility::Local5 = LOG_LOCAL5;
384 const int Facility::Local6 = LOG_LOCAL6;
385 const int Facility::Local7 = LOG_LOCAL7;
390 ** implementation of PartLogger::LogHelper
393 PartLogger::LogHelper::LogHelper(PartLogger& logger, int level, const SourceLocation& loc)
398 StreamPtr.reset(new std::ostringstream());
399 } // eo PartLogger::LogHelper::LogHelper(PartLogger&,int)
401 PartLogger::LogHelper::LogHelper(const LogHelper& helper)
402 : Logger(helper.Logger)
403 , Level(helper.Level)
404 , Location(helper.Location)
405 , StreamPtr(helper.StreamPtr)
407 } // eo PartLogger::LogHelper::LogHelper(const LogHelper&)
410 PartLogger::LogHelper::~LogHelper()
416 //*m_stream_ptr << " at " << m_loc.Line << " in " << m_loc.FunctionName;
417 *StreamPtr << " @" << Location.get_location_tag();
419 std::string msg(StreamPtr->str());
422 Logger.log(Level,msg);
425 } // eo PartLogger::LogHelper::~LogHelper
429 ** implementation of PartLogger
433 * constructor for a part logger.
434 * @param part name of the part (module name) using the logger instance.
436 PartLogger::PartLogger(const std::string& part)
439 } // eo PartLogger::PartLogger(const std.:string&)
443 * @brief constructor for a part logger at module level.
444 * @param loc the source location where the PartLogger is constructed.
446 * The part name is derived from the filename given with the source location by
447 * using the basename and cutting off the C++ file suffix (if it is a well known one;
448 * currently known extensions: cpp, cxx, c++, cc, C).
450 PartLogger::PartLogger( const SourceLocation& loc )
452 if (loc.Line>0 && ! loc.File.empty())
454 std::string str= basename(loc.File);
455 Part= remove_suffix(str,".cpp");
456 if (Part == str) Part= remove_suffix(str,".cxx");
457 if (Part == str) Part= remove_suffix(str,".c++");
458 if (Part == str) Part= remove_suffix(str,".cc");
459 if (Part == str) Part= remove_suffix(str,".C");
465 }// PartLogger::PartLogger(const SourceLocation&)
468 PartLogger::~PartLogger()
474 * generic log function.
475 * @param level the log level.
476 * @param msg the log message.
478 void PartLogger::log(int level, const std::string msg)
480 boost::recursive_mutex::scoped_lock lock(LoggerLock);
482 if (level <= g_max_level)
484 log_part_msg(level, Part, msg);
486 } // eo PartLogger::log(int,const std::string);
489 void PartLogger::fatal(const std::string& msg)
492 } // eo PartLogger::fatal(const std::string&)
495 void PartLogger::alert(const std::string& msg)
498 } // eo PartLogger::alert(const std::string&)
501 void PartLogger::critical(const std::string& msg)
504 } // eo PartLogger::critical(const std::string&)
507 void PartLogger::error(const std::string& msg)
510 } // eo PartLogger::error(const std::string&)
513 void PartLogger::warning(const std::string& msg)
515 log(LOG_WARNING, msg);
516 } // eo PartLogger::warning(const std::string&)
519 void PartLogger::notice(const std::string& msg)
521 log(LOG_NOTICE, msg);
522 } // eo PartLogger::notice(const std::string&)
525 void PartLogger::info(const std::string& msg)
528 } // eo PartLogger::info(const std::string&)
531 void PartLogger::debug(const std::string& msg)
534 } // eo PartLogger::debug(const std::string&)
538 PartLogger::LogHelper PartLogger::fatal(const SourceLocation& loc)
540 return PartLogger::LogHelper(*this,LOG_EMERG,loc);
541 } // eo PartLogger::fatal(const SourceLocation&)
544 PartLogger::LogHelper PartLogger::alert(const SourceLocation& loc)
546 return PartLogger::LogHelper(*this,LOG_ALERT,loc);
547 } // eo PartLogger::alert(const SourceLocation&)
550 PartLogger::LogHelper PartLogger::critical(const SourceLocation& loc)
552 return PartLogger::LogHelper(*this,LOG_CRIT,loc);
553 } // eo PartLogger::critical(const SourceLocation&)
556 PartLogger::LogHelper PartLogger::error(const SourceLocation& loc)
558 return PartLogger::LogHelper(*this,LOG_ERR,loc);
559 } // eo PartLogger::error(const SourceLocation&)
562 PartLogger::LogHelper PartLogger::warning(const SourceLocation& loc)
564 return PartLogger::LogHelper(*this,LOG_WARNING,loc);
565 } // eo PartLogger::warning(const SourceLocation&)
568 PartLogger::LogHelper PartLogger::notice(const SourceLocation& loc)
570 return PartLogger::LogHelper(*this,LOG_NOTICE,loc);
571 } // eo PartLogger::notice(const SourceLocation&)
574 PartLogger::LogHelper PartLogger::info(const SourceLocation& loc)
576 return PartLogger::LogHelper(*this,LOG_INFO,loc);
577 } // eo PartLogger::info(const SourceLocation&)
580 PartLogger::LogHelper PartLogger::debug(const SourceLocation& loc)
582 return PartLogger::LogHelper(*this,LOG_DEBUG,loc);
583 } // eo PartLogger::debug(const SourceLocation&)
591 * enable logging to syslog with a name and a facility.
592 * @param name the name used as ident.
593 * @param facility the facility which should be used.
595 void enable_syslog( const std::string& name, Facility facility )
597 boost::recursive_mutex::scoped_lock lock(LoggerLock);
601 g_facility= facility;
603 } // eo enable_syslog(const std::string,Facility)
607 * enable logging to syslog with a facility.
608 * The ident is used from a previous call or (if none was set) is
609 * determined by reading the program path from /proc/\<pid\>/exe.
610 * @param facility the facility which should be used.
612 void enable_syslog( Facility facility )
614 boost::recursive_mutex::scoped_lock lock(LoggerLock);
618 g_ident= get_program_name();
621 g_facility = facility;
623 } // eo enable_syslog(Facility)
627 * enable or disable logging to syslog.
628 * @param enable whether the logging to syslog should be enabled or not.
630 void enable_syslog( bool enable )
632 boost::recursive_mutex::scoped_lock lock(LoggerLock);
636 if (!g_syslog_opened)
638 enable_syslog( g_facility );
645 } // eo enable_syslog(bool)
649 * enable/ disable loggin to stderr.
650 * @param enable whether to enable or disable logging to stderr.
652 void enable_stderr_log(bool enable)
654 boost::recursive_mutex::scoped_lock lock(LoggerLock);
656 g_stderr_log= enable;
657 } // eo enableStderr;
662 * enable logging to a file.
663 * @param name path to the file.
665 * @note only one log file can be use at a time.
667 void enable_log_file( const std::string& name )
669 boost::recursive_mutex::scoped_lock lock(LoggerLock);
671 g_log_file_name= name;
672 g_log_stream_ptr.reset( new std::ofstream() );
673 g_log_stream_ptr->open( name.c_str(), std::ios::out|std::ios::app );
674 //std::cerr << "### opened \"" << name << "\"" << g_log_stream_ptr->good() << std::endl;
675 } // eo enable_log_file(const std::string&)
679 * enable or disable loggin to a file.
680 * if a logfile was already set by a previous call to enable_log_file(const std::string&)
681 * that one is used; else it logs to <tt>/var/log/</tt><em>program name</em><tt>.log</tt>.
682 * @param enable whether to enable or disable logging to a file.
684 void enable_log_file( bool enable )
686 boost::recursive_mutex::scoped_lock lock(LoggerLock);
690 if (g_log_file_name.empty())
692 std::ostringstream ostr;
693 ostr << "/var/log/" << get_program_name() << ".log";
694 enable_log_file( ostr.str() );
698 enable_log_file( g_log_file_name );
703 g_log_stream_ptr.reset();
705 } // eo enable_log_file(bool)
709 * @brief returns if loging to file is enabled and active.
710 * @return @a true if logfile is enabled and opened.
712 bool is_logging_to_file()
714 boost::recursive_mutex::scoped_lock lock(LoggerLock);
716 return g_log_stream_ptr and g_log_stream_ptr->good();
717 } // eo is_logging_to_file()
722 * @brief returns the current name of the log file.
723 * @return the name of the log file; empty if none was given.
725 * This function returns the last used log file name; even
726 * when logging to that file is currently disabled.
728 std::string get_log_file_name()
730 boost::recursive_mutex::scoped_lock lock(LoggerLock);
732 return g_log_file_name;
733 } // eo get_log_file_name()
737 * @brief re-opens the logfiles (if applicable).
741 boost::recursive_mutex::scoped_lock lock(LoggerLock);
743 if (g_log_stream_ptr)
745 enable_log_file(false); // closes log, but holds the name.
746 enable_log_file(true); // opens the log file again.
752 * set a new log level.
753 * @param level the new log level.
754 * @return the previous log level.
756 int set_log_level(int level)
758 boost::recursive_mutex::scoped_lock lock(LoggerLock);
760 int previous = g_max_level;
763 if (level < LogLevel::Emergency)
764 level = LogLevel::Emergency;
765 else if (level > LogLevel::Debug)
766 level = LogLevel::Debug;
771 } // eo set_log_level(int)
775 * returns the current log level.
776 * @return the current log level.
780 boost::recursive_mutex::scoped_lock lock(LoggerLock);
783 } // eo get_log_level()
787 * returns if the current log level covers the given level.
788 * This is a convenience function for optimization of log output (especially debug output).
789 * @param level the level which should be tested for.
790 * @return @a true iff a message with the level would be written out.
792 bool has_log_level(int level)
794 boost::recursive_mutex::scoped_lock lock(LoggerLock);
796 return (g_max_level >= level);
797 } // eo has_log_level(int)
801 } // eo namespace Logger
802 } // eo namespace I2n