⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logger.cpp

📁 遗传算法的一个库
💻 CPP
字号:
#include <GradSoft/Logger.h>#include <sys/types.h>#include <time.h>#ifdef HAVE_SYSLOG#include <syslog.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endif#ifndef STREAMBUF_HAVE_PUBSEEKPOS#define pubseekpos seekpos#endifusing namespace std;namespace GradSoft {void Logger::setCallback(LogEventType evTag,                          void (*callback)(const char* msg)){ MutexLocker ml(mutex_); callbacks_[evTag]=callback;}void  Logger::put_event(LogEventType evType, const char* value,                         bool dup_to_stderr){ char tmbuff[26]; time_t t = time(NULL);#ifdef HAVE_CTIME_R#ifdef CTIME_R_HAVE_3_ARGS ctime_r(&t,tmbuff,26);#else ctime_r(&t,tmbuff);#endif#elif defined(HAVE_CTIME) {  MutexLocker ml(mutex_);   char *staticLine=ctime(&t);  memcpy(tmbuff,staticLine,26*sizeof(char)); }#else#error neither HAVE_CTIME_R nor HAVE_CTIME is defined#endif  tmbuff[strlen(tmbuff)-1]='\0'; // change \n to \0 const char* szEvType = getStringEventType(evType);  {  // serialize output  MutexLocker ml(mutex_);  if (dup_to_stderr) {    std::cerr << szEvType << ":" << tmbuff << ":" << value << std::flush;  }  if (ofs_) {    ofs_ << szEvType << ":" << tmbuff << ":" << value << std::flush;  } } if (callbacks_[evType]!=NULL) {   callbacks_[evType](value); }#ifdef HAVE_SYSLOG if (syslogEnabled_) {  int syslogType;  switch(evType) {    case Debug:              syslogType = LOG_DEBUG;                        break;    case Info:              syslogType = LOG_INFO;                        break;    case Warning:              syslogType = LOG_WARNING;                        break;    case Error:              syslogType = LOG_ERR;                        break;    case Fatal:              syslogType = LOG_ALERT;                        break;    default:              // internal error  (i.e. near impossible)              syslogType = LOG_CRIT;                       break;  }  syslog(syslogType,"%s",value); }#endif}void Logger::setOutputFile(const char* fname)                                      throw (Logger::IOException){ MutexLocker ml(mutex_);#ifdef HAVE_IOSBASE_FAILURE try {#endif  if (ofs_.is_open()) {    ofs_.close();    if (ofs_.bad()) {      ostrstream ostr;      ostr << "Can't close file " << fname << ":" << strerror(errno) << '\0';      ostr.rdbuf()->freeze(0);      throw IOException(ostr.str());    }  }  ofs_.open(fname);  if (ofs_.bad()) {     ostrstream ostr;     ostr << "Can't open file " << fname << ":" << strerror(errno) << '\0';     ostr.rdbuf()->freeze(0);     throw IOException(ostr.str());  }#ifdef HAVE_IOSBASE_FAILURE }catch(const iosbase_failure& ex){   throw IOException(ex.what()); }#endif}const char* Logger::getStringEventType(LogEventType evType){ switch(evType) {   case Debug:          return "Debug";   case Info:          return "Info";   case Warning:          return "Warning";   case Error:          return "Error";   case Fatal:          return "Fatal";   default:          return "UnknownEventType"; }}const LogEventType DebugLogEventStruct::tag=Debug;const LogEventType InfoLogEventStruct::tag=Info;const LogEventType WarningLogEventStruct::tag=Warning;const LogEventType ErrorLogEventStruct::tag=Error;const LogEventType FatalLogEventStruct::tag=Fatal;LogStreamBuff::LogStreamBuff(Logger& logger, LogEventType tag,                             bool enabled, bool duppedToStderr)      :logger_(logger),       tag_(tag),       enabled_(enabled),       duppedToStderr_(duppedToStderr),       ostr_(),       touched_(false){ setp(0,0); setg(0,0,0);} streamsize LogStreamBuff::xsputn(const char* str, streamsize size)                                 { touched_ = true; if (!enabled_) return size; ostr_.write(str,size); return size;} int LogStreamBuff::overflow(int c){ touched_ = true; if (!enabled_) return c; if (c!=EOF) ostr_ << (char)c; return c;}int LogStreamBuff::sync(){ if (!touched_) {     return 0; } if (!enabled_) {    return 0; } ostr_ << '\0'; char* str = ostr_.rdbuf()->str(); logger_.put_event(tag_,str,isDuppedToStderr()); ostr_.rdbuf()->freeze(0); ostr_.rdbuf()->pubseekpos(0); touched_ = false; return 0;}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -