]> git.stg.codes - stg.git/blob - libs/logger/logger.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / libs / logger / logger.cpp
1 #include "stg/logger.h"
2
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <syslog.h>
6
7 #ifdef STG_TIME
8 extern const volatile time_t stgTime;
9 #endif
10
11 using STG::Logger;
12 using STG::PluginLogger;
13
14 //-----------------------------------------------------------------------------
15 Logger& Logger::get()
16 {
17     static Logger logger;
18     return logger;
19 }
20 //-----------------------------------------------------------------------------
21 void Logger::setFileName(const std::string& fn)
22 {
23     std::lock_guard<std::mutex> lock(mutex);
24     fileName = fn;
25 }
26 //-----------------------------------------------------------------------------
27 void Logger::operator()(const char* fmt, ...) const
28 {
29     std::lock_guard<std::mutex> lock(mutex);
30
31     static char buff[2048];
32
33     va_list vl;
34     va_start(vl, fmt);
35     vsnprintf(buff, sizeof(buff), fmt, vl);
36     va_end(vl);
37
38     logString(buff);
39 }
40 //-----------------------------------------------------------------------------
41 const char* Logger::logDate(time_t t) const
42 {
43     static char s[32];
44
45     const auto tt = localtime(&t);
46
47     snprintf(s, 32, "%d-%s%d-%s%d %s%d:%s%d:%s%d",
48              tt->tm_year + 1900,
49              tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1,
50              tt->tm_mday    < 10 ? "0" : "", tt->tm_mday,
51              tt->tm_hour    < 10 ? "0" : "", tt->tm_hour,
52              tt->tm_min     < 10 ? "0" : "", tt->tm_min,
53              tt->tm_sec     < 10 ? "0" : "", tt->tm_sec);
54
55     return s;
56 }
57 //-----------------------------------------------------------------------------
58 void Logger::logString(const char* str) const
59 {
60     if (!fileName.empty())
61     {
62         auto f = fopen(fileName.c_str(), "at");
63         if (f)
64         {
65             #ifdef STG_TIME
66             fprintf(f, "%s", logDate(stgTime));
67             #else
68             fprintf(f, "%s", logDate(time(NULL)));
69             #endif
70             fprintf(f, " -- ");
71             fprintf(f, "%s", str);
72             fprintf(f, "\n");
73             fclose(f);
74         }
75         else
76         {
77             openlog("stg", LOG_NDELAY, LOG_USER);
78             syslog(LOG_CRIT, "%s", str);
79             closelog();
80         }
81     }
82     else
83     {
84         openlog("stg", LOG_NDELAY, LOG_USER);
85         syslog(LOG_CRIT, "%s", str);
86         closelog();
87     }
88 }
89 //-----------------------------------------------------------------------------
90 void PluginLogger::operator()(const char * fmt, ...) const
91 {
92     std::lock_guard<std::mutex> lock(m_mutex);
93
94     static char buff[2029];
95
96     va_list vl;
97     va_start(vl, fmt);
98     vsnprintf(buff, sizeof(buff), fmt, vl);
99     va_end(vl);
100
101     m_parent("[%s] %s", m_pluginName.c_str(), buff);
102 }
103 //-----------------------------------------------------------------------------
104 void PluginLogger::operator()(const std::string & line) const
105 {
106     m_parent("[%s] %s", m_pluginName.c_str(), line.c_str());
107 }