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);
48 //-----------------------------------------------------------------------------
49 const char * STG_LOGGER::LogDate(time_t t) const
55 struct tm * tt = localtime(&t);
57 snprintf(s, 32, "%d-%s%d-%s%d %s%d:%s%d:%s%d",
59 tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1,
60 tt->tm_mday < 10 ? "0" : "", tt->tm_mday,
61 tt->tm_hour < 10 ? "0" : "", tt->tm_hour,
62 tt->tm_min < 10 ? "0" : "", tt->tm_min,
63 tt->tm_sec < 10 ? "0" : "", tt->tm_sec);
67 //-----------------------------------------------------------------------------
68 void STG_LOGGER::LogString(const char * str) const
70 if (!fileName.empty())
72 FILE * f = fopen(fileName.c_str(), "at");
76 fprintf(f, "%s", LogDate(stgTime));
78 fprintf(f, "%s", LogDate(time(NULL)));
81 fprintf(f, "%s", str);
87 openlog("stg", LOG_NDELAY, LOG_USER);
88 syslog(LOG_CRIT, "%s", str);
94 openlog("stg", LOG_NDELAY, LOG_USER);
95 syslog(LOG_CRIT, "%s", str);
99 //-----------------------------------------------------------------------------
100 PLUGIN_LOGGER::PLUGIN_LOGGER(const STG_LOGGER & logger, const std::string & pn)
104 SetLogFileName(logger.fileName);
106 //-----------------------------------------------------------------------------
107 PLUGIN_LOGGER::PLUGIN_LOGGER(const PLUGIN_LOGGER & rhs)
109 pluginName(rhs.pluginName)
111 SetLogFileName(fileName);
113 //-----------------------------------------------------------------------------
114 void PLUGIN_LOGGER::operator()(const char * fmt, ...) const
120 vsnprintf(buff, sizeof(buff), fmt, vl);
123 STG_LOGGER::operator()("[%s] %s", pluginName.c_str(), buff);
125 //-----------------------------------------------------------------------------
126 void PLUGIN_LOGGER::operator()(const std::string & line) const
128 STG_LOGGER::operator()("[%s] %s", pluginName.c_str(), line.c_str());
130 //-----------------------------------------------------------------------------
131 PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::string & pluginName)
133 return PLUGIN_LOGGER(logger, pluginName);
135 //-----------------------------------------------------------------------------