5 #include "stg/logger.h"
8 extern const volatile time_t stgTime;
10 //-----------------------------------------------------------------------------
11 STG_LOGGER & GetStgLogger()
13 static STG_LOGGER logger;
16 //-----------------------------------------------------------------------------
17 STG_LOGGER::STG_LOGGER()
21 pthread_mutex_init(&mutex, NULL);
23 //-----------------------------------------------------------------------------
24 STG_LOGGER::~STG_LOGGER()
26 pthread_mutex_destroy(&mutex);
28 //-----------------------------------------------------------------------------
29 void STG_LOGGER::SetLogFileName(const std::string & fn)
31 STG_LOGGER_LOCKER lock(&mutex);
34 //-----------------------------------------------------------------------------
35 void STG_LOGGER::operator()(const char * fmt, ...) const
37 STG_LOGGER_LOCKER lock(&mutex);
43 vsnprintf(buff, sizeof(buff), fmt, vl);
47 if (!fileName.empty())
49 f = fopen(fileName.c_str(), "at");
53 fprintf(f, "%s", LogDate(stgTime));
55 fprintf(f, "%s", LogDate(time(NULL)));
58 fprintf(f, "%s", buff);
64 openlog("stg", LOG_NDELAY, LOG_USER);
65 syslog(LOG_CRIT, "%s", buff);
71 openlog("stg", LOG_NDELAY, LOG_USER);
72 syslog(LOG_CRIT, "%s", buff);
76 //-----------------------------------------------------------------------------
77 const char * STG_LOGGER::LogDate(time_t t) const
83 struct tm * tt = localtime(&t);
85 snprintf(s, 32, "%d-%s%d-%s%d %s%d:%s%d:%s%d",
87 tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1,
88 tt->tm_mday < 10 ? "0" : "", tt->tm_mday,
89 tt->tm_hour < 10 ? "0" : "", tt->tm_hour,
90 tt->tm_min < 10 ? "0" : "", tt->tm_min,
91 tt->tm_sec < 10 ? "0" : "", tt->tm_sec);
95 //-----------------------------------------------------------------------------
96 PLUGIN_LOGGER::PLUGIN_LOGGER(const STG_LOGGER & logger, const std::string & pn)
100 SetLogFileName(logger.fileName);
102 //-----------------------------------------------------------------------------
103 PLUGIN_LOGGER::PLUGIN_LOGGER(const PLUGIN_LOGGER & rhs)
105 pluginName(rhs.pluginName)
107 SetLogFileName(fileName);
109 //-----------------------------------------------------------------------------
110 void PLUGIN_LOGGER::operator()(const char * fmt, ...) const
116 vsnprintf(buff, sizeof(buff), fmt, vl);
119 STG_LOGGER::operator()("[%s] %s", pluginName.c_str(), buff);
121 //-----------------------------------------------------------------------------
122 PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::string & pluginName)
124 return PLUGIN_LOGGER(logger, pluginName);
126 //-----------------------------------------------------------------------------