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.
199 * @param keep_unsafe_chars don't replace characters that are considered unsafe to log, e.g. control characters.
201 * Write the message to every enabled log channel.
203 * If syslog is enabled the message is passed unmodified to syslog.
205 * If a stream log is enabled (stderr or file) then the message is prepended with date, time
206 * and process information (like syslog does). The message is splitted at line ends and
207 * consecutive lines are indented.
209 void log_msg( int level, const std::string& msg, bool keep_unsafe_chars)
211 boost::recursive_mutex::scoped_lock lock(LoggerLock);
213 if (not g_syslog_opened and not g_stderr_log and not g_log_stream_ptr)
215 // if nothing is opened for logging: we activate syslog!
221 std::string sane_msg;
222 if (keep_unsafe_chars)
225 sane_msg=sanitize_for_logging(msg);
227 ::syslog( get_syslog_level(level), "%s", sane_msg.c_str());
229 // file(/stream) logging:
230 if (g_stderr_log or g_log_stream_ptr) // add more log "enabled" expressions here...
232 // here we need must do something more with the msg...
236 std::ostringstream ostr;
237 // add time stamp (syslog like: "Mon DD HH:MM:SS") :
239 time_t t = time(NULL);
242 if (localtime_r(&t, &ta) == NULL)
243 memset(&ta, 0, sizeof(struct tm));
245 std::strftime(buffer, sizeof(buffer),"%b %d %H:%M:%S ", &ta);
248 ostr << get_level_tag(level) << " ";
249 ostr << g_ident << "[" << getpid() << "]: ";
254 std::string indent_string(prefix.size(), ' ');
255 std::list< std::string > parts;
256 split_string( chomp(msg,"\n"), parts, "\n");
257 std::ostringstream ostr;
259 for(std::list< std::string >::const_iterator it= parts.begin();
263 if (it != parts.begin())
265 ostr << indent_string;
268 if (keep_unsafe_chars)
269 ostr << *it << std::endl;
271 ostr << sanitize_for_logging(*it) << std::endl;
278 std::cerr << new_msg;
280 if (g_log_stream_ptr)
282 *g_log_stream_ptr << new_msg << std::flush;
289 * @brief "real" log function for part messages.
290 * @param level the log level.
291 * @param part the part(/module) name(/id)
292 * @param msg the log message.
293 * @param keep_unsafe_chars don't replace characters that are considered unsafe to log, e.g. control characters.
295 * basically calls @a log(), but prepends the part (if not empty) in square brackets to the message.
299 const std::string& part,
300 const std::string& msg,
301 bool keep_unsafe_chars)
303 // Note: Locking is done in log_msg()
307 std::ostringstream ostr;
308 ostr << "[" << part << "] " << msg;
309 log_msg(level, ostr.str(), keep_unsafe_chars);
313 log_msg(level, msg, keep_unsafe_chars);
315 } // eo log_part_msg(int,const std::string&,const std::string&)
319 * @brief returns the name of the program (/binary)
320 * @return the program name if it could be determined; generated name else.
322 * Tries to determine the name of the binary.
324 * If no name could be determined, one is built.
326 std::string get_program_name()
331 // determine the program name:
333 // try to determine the name using the exe link:
334 std::string exe_path;
336 std::ostringstream ostr;
337 ostr << "/proc/" << ::getpid() << "/exe";
338 exe_path= ostr.str();
340 std::string binary_path= read_link(exe_path);
341 if (!binary_path.empty())
343 result= basename(binary_path);
348 // no program name found up to this point.
349 // make a name (as fallback solution):
350 std::ostringstream ostr;
351 ostr << "prg-" << ::getpid();
355 } // eo get_program_name
361 // Note: Locking is done in close_syslog();
363 //TODO other cleanups?
372 std::atexit( _cleanup );
377 } // eo namespace <anonymous>
382 ** implementation of Facility
386 const int Facility::AuthPriv= LOG_AUTH;
387 const int Facility::Cron = LOG_CRON;
388 const int Facility::Daemon = LOG_DAEMON;
389 const int Facility::Kern = LOG_KERN;
390 const int Facility::Mail = LOG_MAIL;
391 const int Facility::News = LOG_NEWS;
392 const int Facility::Syslog = LOG_SYSLOG;
393 const int Facility::User = LOG_USER;
394 const int Facility::UUCP = LOG_UUCP;
395 const int Facility::Local0 = LOG_LOCAL0;
396 const int Facility::Local1 = LOG_LOCAL1;
397 const int Facility::Local2 = LOG_LOCAL2;
398 const int Facility::Local3 = LOG_LOCAL3;
399 const int Facility::Local4 = LOG_LOCAL4;
400 const int Facility::Local5 = LOG_LOCAL5;
401 const int Facility::Local6 = LOG_LOCAL6;
402 const int Facility::Local7 = LOG_LOCAL7;
407 ** implementation of PartLogger::LogHelper
410 PartLogger::LogHelper::LogHelper(PartLogger& logger, int level, const SourceLocation& loc)
415 StreamPtr.reset(new std::ostringstream());
416 } // eo PartLogger::LogHelper::LogHelper(PartLogger&,int)
418 PartLogger::LogHelper::LogHelper(const LogHelper& helper)
419 : Logger(helper.Logger)
420 , Level(helper.Level)
421 , Location(helper.Location)
422 , StreamPtr(helper.StreamPtr)
424 } // eo PartLogger::LogHelper::LogHelper(const LogHelper&)
427 PartLogger::LogHelper::~LogHelper()
433 //*m_stream_ptr << " at " << m_loc.Line << " in " << m_loc.FunctionName;
434 *StreamPtr << " @" << Location.get_location_tag();
436 std::string msg(StreamPtr->str());
439 Logger.log(Level,msg);
442 } // eo PartLogger::LogHelper::~LogHelper
446 ** implementation of PartLogger
450 * constructor for a part logger.
451 * @param part name of the part (module name) using the logger instance.
453 PartLogger::PartLogger(const std::string& part)
454 : Part(part), keep_unsafe_chars(false)
456 } // eo PartLogger::PartLogger(const std.:string&)
460 * @brief constructor for a part logger at module level.
461 * @param loc the source location where the PartLogger is constructed.
463 * The part name is derived from the filename given with the source location by
464 * using the basename and cutting off the C++ file suffix (if it is a well known one;
465 * currently known extensions: cpp, cxx, c++, cc, C).
467 PartLogger::PartLogger( const SourceLocation& loc )
468 : keep_unsafe_chars(false)
470 if (loc.Line>0 && ! loc.File.empty())
472 std::string str= basename(loc.File);
473 Part= remove_suffix(str,".cpp");
474 if (Part == str) Part= remove_suffix(str,".cxx");
475 if (Part == str) Part= remove_suffix(str,".c++");
476 if (Part == str) Part= remove_suffix(str,".cc");
477 if (Part == str) Part= remove_suffix(str,".C");
483 }// PartLogger::PartLogger(const SourceLocation&)
486 PartLogger::~PartLogger()
492 * generic log function.
493 * @param level the log level.
494 * @param msg the log message.
496 void PartLogger::log(int level, const std::string &msg, bool keep_unsafe_chars)
498 boost::recursive_mutex::scoped_lock lock(LoggerLock);
500 if (level <= g_max_level)
502 log_part_msg(level, Part, msg, keep_unsafe_chars);
504 } // eo PartLogger::log(int,const std::string);
507 void PartLogger::fatal(const std::string& msg)
509 log(LOG_EMERG,msg,keep_unsafe_chars);
510 } // eo PartLogger::fatal(const std::string&)
513 void PartLogger::alert(const std::string& msg)
515 log(LOG_ALERT,msg,keep_unsafe_chars);
516 } // eo PartLogger::alert(const std::string&)
519 void PartLogger::critical(const std::string& msg)
521 log(LOG_CRIT,msg,keep_unsafe_chars);
522 } // eo PartLogger::critical(const std::string&)
525 void PartLogger::error(const std::string& msg)
527 log(LOG_ERR, msg,keep_unsafe_chars);
528 } // eo PartLogger::error(const std::string&)
531 void PartLogger::warning(const std::string& msg)
533 log(LOG_WARNING, msg,keep_unsafe_chars);
534 } // eo PartLogger::warning(const std::string&)
537 void PartLogger::notice(const std::string& msg)
539 log(LOG_NOTICE, msg,keep_unsafe_chars);
540 } // eo PartLogger::notice(const std::string&)
543 void PartLogger::info(const std::string& msg)
545 log(LOG_INFO, msg,keep_unsafe_chars);
546 } // eo PartLogger::info(const std::string&)
549 void PartLogger::debug(const std::string& msg)
551 log(LOG_DEBUG, msg,keep_unsafe_chars);
552 } // eo PartLogger::debug(const std::string&)
554 void PartLogger::set_keep_unsafe_chars(bool _keep_unsafe_chars)
556 keep_unsafe_chars=_keep_unsafe_chars;
559 bool PartLogger::get_keep_unsafe_chars()
561 return keep_unsafe_chars;
564 PartLogger::LogHelper PartLogger::fatal(const SourceLocation& loc)
566 return PartLogger::LogHelper(*this,LOG_EMERG,loc);
567 } // eo PartLogger::fatal(const SourceLocation&)
570 PartLogger::LogHelper PartLogger::alert(const SourceLocation& loc)
572 return PartLogger::LogHelper(*this,LOG_ALERT,loc);
573 } // eo PartLogger::alert(const SourceLocation&)
576 PartLogger::LogHelper PartLogger::critical(const SourceLocation& loc)
578 return PartLogger::LogHelper(*this,LOG_CRIT,loc);
579 } // eo PartLogger::critical(const SourceLocation&)
582 PartLogger::LogHelper PartLogger::error(const SourceLocation& loc)
584 return PartLogger::LogHelper(*this,LOG_ERR,loc);
585 } // eo PartLogger::error(const SourceLocation&)
588 PartLogger::LogHelper PartLogger::warning(const SourceLocation& loc)
590 return PartLogger::LogHelper(*this,LOG_WARNING,loc);
591 } // eo PartLogger::warning(const SourceLocation&)
594 PartLogger::LogHelper PartLogger::notice(const SourceLocation& loc)
596 return PartLogger::LogHelper(*this,LOG_NOTICE,loc);
597 } // eo PartLogger::notice(const SourceLocation&)
600 PartLogger::LogHelper PartLogger::info(const SourceLocation& loc)
602 return PartLogger::LogHelper(*this,LOG_INFO,loc);
603 } // eo PartLogger::info(const SourceLocation&)
606 PartLogger::LogHelper PartLogger::debug(const SourceLocation& loc)
608 return PartLogger::LogHelper(*this,LOG_DEBUG,loc);
609 } // eo PartLogger::debug(const SourceLocation&)
617 * enable logging to syslog with a name and a facility.
618 * @param name the name used as ident.
619 * @param facility the facility which should be used.
621 void enable_syslog( const std::string& name, Facility facility )
623 boost::recursive_mutex::scoped_lock lock(LoggerLock);
627 g_facility= facility;
629 } // eo enable_syslog(const std::string,Facility)
633 * enable logging to syslog with a facility.
634 * The ident is used from a previous call or (if none was set) is
635 * determined by reading the program path from /proc/\<pid\>/exe.
636 * @param facility the facility which should be used.
638 void enable_syslog( Facility facility )
640 boost::recursive_mutex::scoped_lock lock(LoggerLock);
644 g_ident= get_program_name();
647 g_facility = facility;
649 } // eo enable_syslog(Facility)
653 * enable or disable logging to syslog.
654 * @param enable whether the logging to syslog should be enabled or not.
656 void enable_syslog( bool enable )
658 boost::recursive_mutex::scoped_lock lock(LoggerLock);
662 if (!g_syslog_opened)
664 enable_syslog( g_facility );
671 } // eo enable_syslog(bool)
675 * enable/ disable loggin to stderr.
676 * @param enable whether to enable or disable logging to stderr.
678 void enable_stderr_log(bool enable)
680 boost::recursive_mutex::scoped_lock lock(LoggerLock);
682 g_stderr_log= enable;
683 } // eo enableStderr;
688 * enable logging to a file.
689 * @param name path to the file.
691 * @note only one log file can be use at a time.
693 void enable_log_file( const std::string& name )
695 boost::recursive_mutex::scoped_lock lock(LoggerLock);
697 g_log_file_name= name;
698 g_log_stream_ptr.reset( new std::ofstream() );
699 g_log_stream_ptr->open( name.c_str(), std::ios::out|std::ios::app );
700 //std::cerr << "### opened \"" << name << "\"" << g_log_stream_ptr->good() << std::endl;
701 } // eo enable_log_file(const std::string&)
705 * enable or disable loggin to a file.
706 * if a logfile was already set by a previous call to enable_log_file(const std::string&)
707 * that one is used; else it logs to <tt>/var/log/</tt><em>program name</em><tt>.log</tt>.
708 * @param enable whether to enable or disable logging to a file.
710 void enable_log_file( bool enable )
712 boost::recursive_mutex::scoped_lock lock(LoggerLock);
716 if (g_log_file_name.empty())
718 std::ostringstream ostr;
719 ostr << "/var/log/" << get_program_name() << ".log";
720 enable_log_file( ostr.str() );
724 enable_log_file( g_log_file_name );
729 g_log_stream_ptr.reset();
731 } // eo enable_log_file(bool)
735 * @brief returns if loging to file is enabled and active.
736 * @return @a true if logfile is enabled and opened.
738 bool is_logging_to_file()
740 boost::recursive_mutex::scoped_lock lock(LoggerLock);
742 return g_log_stream_ptr and g_log_stream_ptr->good();
743 } // eo is_logging_to_file()
748 * @brief returns the current name of the log file.
749 * @return the name of the log file; empty if none was given.
751 * This function returns the last used log file name; even
752 * when logging to that file is currently disabled.
754 std::string get_log_file_name()
756 boost::recursive_mutex::scoped_lock lock(LoggerLock);
758 return g_log_file_name;
759 } // eo get_log_file_name()
763 * @brief re-opens the logfiles (if applicable).
767 boost::recursive_mutex::scoped_lock lock(LoggerLock);
769 if (g_log_stream_ptr)
771 enable_log_file(false); // closes log, but holds the name.
772 enable_log_file(true); // opens the log file again.
778 * set a new log level.
779 * @param level the new log level.
780 * @return the previous log level.
782 int set_log_level(int level)
784 boost::recursive_mutex::scoped_lock lock(LoggerLock);
786 int previous = g_max_level;
789 if (level < LogLevel::Emergency)
790 level = LogLevel::Emergency;
791 else if (level > LogLevel::Debug)
792 level = LogLevel::Debug;
797 } // eo set_log_level(int)
801 * returns the current log level.
802 * @return the current log level.
806 boost::recursive_mutex::scoped_lock lock(LoggerLock);
809 } // eo get_log_level()
813 * returns if the current log level covers the given level.
814 * This is a convenience function for optimization of log output (especially debug output).
815 * @param level the level which should be tested for.
816 * @return @a true iff a message with the level would be written out.
818 bool has_log_level(int level)
820 boost::recursive_mutex::scoped_lock lock(LoggerLock);
822 return (g_max_level >= level);
823 } // eo has_log_level(int)
828 * returns a string representation of the current log level.
829 * @return a string corresponding to get_log_level
831 std::string get_log_level_string()
833 int level = get_log_level();
837 else if (level >= LogLevel::_LogLevel_END)
843 case LogLevel::Debug: return "Debug"; break;
844 case LogLevel::Info: return "Info"; break;
845 case LogLevel::Notice: return "Notice"; break;
846 case LogLevel::Warning: return "Warning"; break;
847 case LogLevel::Error: return "Error"; break;
848 case LogLevel::Critical: return "Critical"; break;
849 case LogLevel::Alert: return "Alert"; break;
850 case LogLevel::Emergency: return "Emergency"; break;
851 default: return "Non-standard"; break;
854 } // eo get_log_level_string
857 } // eo namespace Logger
858 } // eo namespace I2n