X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/46b0747592074017ff0ea4b33d4a7194235886e5..8bfaedfaf22377ff1beadebff699613bf19a1ce2:/libs/logger/logger.cpp?ds=sidebyside diff --git a/libs/logger/logger.cpp b/libs/logger/logger.cpp index 20aa133a..e592c6d1 100644 --- a/libs/logger/logger.cpp +++ b/libs/logger/logger.cpp @@ -7,121 +7,101 @@ #ifdef STG_TIME extern const volatile time_t stgTime; #endif + +using STG::Logger; +using STG::PluginLogger; + //----------------------------------------------------------------------------- -STG_LOGGER & GetStgLogger() -{ -static STG_LOGGER logger; -return logger; -} -//----------------------------------------------------------------------------- -STG_LOGGER::STG_LOGGER() - : fileName(), - mutex() -{ -pthread_mutex_init(&mutex, NULL); -} -//----------------------------------------------------------------------------- -STG_LOGGER::~STG_LOGGER() +Logger& Logger::get() { -pthread_mutex_destroy(&mutex); + static Logger logger; + return logger; } //----------------------------------------------------------------------------- -void STG_LOGGER::SetLogFileName(const std::string & fn) +void Logger::setFileName(const std::string& fn) { -STG_LOGGER_LOCKER lock(&mutex); -fileName = fn; + std::lock_guard lock(mutex); + fileName = fn; } //----------------------------------------------------------------------------- -void STG_LOGGER::operator()(const char * fmt, ...) const +void Logger::operator()(const char* fmt, ...) const { -STG_LOGGER_LOCKER lock(&mutex); + std::lock_guard lock(mutex); -char buff[2048]; + static char buff[2048]; -va_list vl; -va_start(vl, fmt); -vsnprintf(buff, sizeof(buff), fmt, vl); -va_end(vl); + va_list vl; + va_start(vl, fmt); + vsnprintf(buff, sizeof(buff), fmt, vl); + va_end(vl); -LogString(buff); + logString(buff); } //----------------------------------------------------------------------------- -const char * STG_LOGGER::LogDate(time_t t) const +const char* Logger::logDate(time_t t) const { -static char s[32]; -if (t == 0) - t = time(NULL); + static char s[32]; -struct tm * tt = localtime(&t); + const auto tt = localtime(&t); -snprintf(s, 32, "%d-%s%d-%s%d %s%d:%s%d:%s%d", - tt->tm_year + 1900, - tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1, - tt->tm_mday < 10 ? "0" : "", tt->tm_mday, - tt->tm_hour < 10 ? "0" : "", tt->tm_hour, - tt->tm_min < 10 ? "0" : "", tt->tm_min, - tt->tm_sec < 10 ? "0" : "", tt->tm_sec); + snprintf(s, 32, "%d-%s%d-%s%d %s%d:%s%d:%s%d", + tt->tm_year + 1900, + tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1, + tt->tm_mday < 10 ? "0" : "", tt->tm_mday, + tt->tm_hour < 10 ? "0" : "", tt->tm_hour, + tt->tm_min < 10 ? "0" : "", tt->tm_min, + tt->tm_sec < 10 ? "0" : "", tt->tm_sec); -return s; + return s; } //----------------------------------------------------------------------------- -void STG_LOGGER::LogString(const char * str) const +void Logger::logString(const char* str) const { -if (!fileName.empty()) + if (!fileName.empty()) { - FILE * f = fopen(fileName.c_str(), "at"); - if (f) + auto f = fopen(fileName.c_str(), "at"); + if (f) { - #ifdef STG_TIME - fprintf(f, "%s", LogDate(stgTime)); - #else - fprintf(f, "%s", LogDate(time(NULL))); - #endif - fprintf(f, " -- "); - fprintf(f, "%s", str); - fprintf(f, "\n"); - fclose(f); + #ifdef STG_TIME + fprintf(f, "%s", logDate(stgTime)); + #else + fprintf(f, "%s", logDate(time(NULL))); + #endif + fprintf(f, " -- "); + fprintf(f, "%s", str); + fprintf(f, "\n"); + fclose(f); } - else + else { - openlog("stg", LOG_NDELAY, LOG_USER); - syslog(LOG_CRIT, "%s", str); - closelog(); + openlog("stg", LOG_NDELAY, LOG_USER); + syslog(LOG_CRIT, "%s", str); + closelog(); } } -else + else { - openlog("stg", LOG_NDELAY, LOG_USER); - syslog(LOG_CRIT, "%s", str); - closelog(); + openlog("stg", LOG_NDELAY, LOG_USER); + syslog(LOG_CRIT, "%s", str); + closelog(); } } //----------------------------------------------------------------------------- -PLUGIN_LOGGER::PLUGIN_LOGGER(const STG_LOGGER& logger, const std::string& pn) - : m_parent(logger), - m_pluginName(pn) -{ -} -//----------------------------------------------------------------------------- -void PLUGIN_LOGGER::operator()(const char * fmt, ...) const +void PluginLogger::operator()(const char * fmt, ...) const { -char buff[2029]; + std::lock_guard lock(m_mutex); -va_list vl; -va_start(vl, fmt); -vsnprintf(buff, sizeof(buff), fmt, vl); -va_end(vl); + static char buff[2029]; -m_parent("[%s] %s", m_pluginName.c_str(), buff); -} -//----------------------------------------------------------------------------- -void PLUGIN_LOGGER::operator()(const std::string & line) const -{ -m_parent("[%s] %s", m_pluginName.c_str(), line.c_str()); + va_list vl; + va_start(vl, fmt); + vsnprintf(buff, sizeof(buff), fmt, vl); + va_end(vl); + + m_parent("[%s] %s", m_pluginName.c_str(), buff); } //----------------------------------------------------------------------------- -PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::string & pluginName) +void PluginLogger::operator()(const std::string & line) const { -return PLUGIN_LOGGER(logger, pluginName); + m_parent("[%s] %s", m_pluginName.c_str(), line.c_str()); } -//-----------------------------------------------------------------------------