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, ...)
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)
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 //-----------------------------------------------------------------------------