📄 logger.h
字号:
/************************************************************ * Threads Library Extras * ---------------------- * * Logger Class - this class will help on logging messages * * Last Update: Oct 15th 2001 * * Author: Cass Surek <cass@surek.com.br> * and * Orn E. Hansen <oe.hansen@gamma.telenordia.se> * ************************************************************/#include <thread.h>#include "exception.h"#include "linked_list.h"extern "C" {#include <syslog.h>#include <unistd.h>#include <stdarg.h>};using namespace cpp_threads;/** * The Logger class provides an easy way to log messages from inside your application. * <BR>At startup, it starts a new thread which does not block the main execution thread. * <BR>It currently uses syslog to log messages. * <BR>Here's an example of its use: <BR><BR>int main()<BR>{<BR><BR> Logger *log;<BR><BR> Creating Log facility<BR> log = new Logger(3);<BR> log->run();<BR><BR> //lots of code here<BR> <BR> log->write(4,"integer: \%d",(void*)10);<BR> <BR> //lots of code here too<BR><BR> // Stopping the Logger<BR> log->stop();<BR><BR> return 1;<BR>} */class LoggerMsg : public Node { private: Logger::llevel _level; std::string _message; public: /** * If no log level is informed, it will assum an "info" message. * @param level The log level that this message should have */ LoggerMsg(Logger::llevel level=info, const std::string message="") { _level = level; _message = message; } /** * Sets the log level of this message * @param level The log level that this message should have */ void setLevel(Logger::llevel level) { _level = level; } /** * Sets the message * @param message What should be logged */ void setMessage(const std::string message&) { _message = message; } /** * Gets the log level of this message * @return The log level of this message */ Logger::llevel level getLevel() { return level; } /** * Gets the message * @return The text of this message */ std::string message& getMessage() { return std::string message&; }};class Logger : public pthread { private: int debug_level; public: /** * Options supported by the linux kernel */ enum loptions { cons = LOG_CONS, ndelay = LOG_NDELAY, perror = LOG_PERROR, pid = LOG_PID } /** * Facilities supported by the linux kernel */ enum lfacility { authpriv = LOG_AUTHPRIV, cron = LOG_CRON, daemon = LOG_DAEMON, kern = LOG_KERNEL, local0 = LOG_LOCAL0, local1 = LOG_LOCAL1, local2 = LOG_LOCAL2, local3 = LOG_LOCAL3, local4 = LOG_LOCAL4, local5 = LOG_LOCAL5, local6 = LOG_LOCAL6, local7 = LOG_LOCAL7, lpr = LOG_LPR, mail = LOG_MAIL, news = LOG_NEWS, user = LOG_USER, } /** * Levels supported by the linux kernel */ enum llevel { emerg = LOG_EMERG, alert = LOG_ALERT, crit = LOG_CRIT, err = LOG_ERR, warn = LOG_WARNING, info = LOG_INFO, debug = LOG_DEBUG } /** * Here you must inform the debug level that should be logged. * If none is informed, all messages up to warning messages are going to be logged. @param debug_level The maximum debug level */ Logger(int debug_level=LOG_WARN) { this->debug_level = debug_level; } /** * Use this method to write the message to the log \param level The debug level of this message \param msg The message to be written \param arg* Arguments to the syslog function in a printf style */ void write(int level, char *msg, void *arg1=0, void *arg2=0, void *arg3=0) { if(level>=debug_level) syslog(LOG_INFO,msg,arg1,arg2,arg3); } int thread(void *) { return 0; } /* Operators */ /** * Use this method to log something with INFO level. @param std::string& Message */ Logger& Logger::operator <<(const std::string&) { syslog(LOG_INFO,msg.c_str()); } /* Syslog Logging */ /** * This method will open a connection to the system logger. @param ident The ident is added to each message (typically the program name) */ void OpenLog(const std::string ident&, Logger::loption opt, Logger::facility fac) { openlog(ident.c_str(), opt, fac); } /** * This method will close the descriptor used to write to the system logger */ void CloseLog() { closelog(void); } /* Shortcuts */ /** * This method is a shortcut to log an emergency message * @param msg The message to be logged */ void Emergency(const std::string& msg) { syslog(LOG_EMERG,msg.c_str()); } /** * This method is a shortcut to log an alert message * @param msg The message to be logged */ void Alert(const std::string& msg) { syslog(LOG_ALERT,msg.c_str()); } /** * This method is a shortcut to log a crit message * @param msg The message to be logged */ void Critical(const std::string& msg) { syslog(LOG_CRIT,msg.c_str()); } /** * This method is a shortcut to log an error message * @param msg The message to be logged */ void Error(const std::string& msg) { syslog(LOG_INFO,msg.c_str()); } /** * This method is a shortcut to log a warning message * @param msg The message to be logged */ void Warning(const std::string& msg) { syslog(LOG_WARN,msg.c_str()); } /** * This method is a shortcut to log a notice message * @param msg The message to be logged */ void Notice(const std::string& msg) { syslog(LOG_NOTICE,msg.c_str()); } /** * This method is a shortcut to log an info message * @param msg The message to be logged */ void Info(const std::string& msg) { syslog(LOG_INFO,msg.c_str()); } /** * This method is a shortcut to log a debug message * @param msg The message to be logged */ void Debug(const std::string& msg) { syslog(LOG_DEBUG,msg.c_str()); } /** * This method will log an exception */ void LogException(cpp_threads::exception::tType severity, int err_num, const std::string msg&) { syslog(LOG_ERR, "ERROR: %d",err_num); syslog(LOG_ERR,msg.c_str()); } /* Kernel logging */ /** * This method will open the log through the kernel system call interface. <BR>This will only work for a root process. @param msg The msg to be sent when opening the syslog @return true if successful, false if unsuccessful *//* bool kOpenLog(char *msg) { if(syslog(1,msg,0)!=-1) return true; } */ };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -