X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a9c940996bac5c61168f06ef61bc20225fb7a65e..980332313bffde590173f76fd006837e0c8f3bed:/stglibs/logger.lib/logger.cpp diff --git a/stglibs/logger.lib/logger.cpp b/stglibs/logger.lib/logger.cpp index 215c6392..4a455257 100644 --- a/stglibs/logger.lib/logger.cpp +++ b/stglibs/logger.lib/logger.cpp @@ -2,7 +2,7 @@ #include #include -#include "logger.h" +#include "stg/logger.h" #ifdef STG_TIME extern const volatile time_t stgTime; @@ -32,7 +32,7 @@ STG_LOGGER_LOCKER lock(&mutex); fileName = fn; } //----------------------------------------------------------------------------- -void STG_LOGGER::operator()(const char * fmt, ...) +void STG_LOGGER::operator()(const char * fmt, ...) const { STG_LOGGER_LOCKER lock(&mutex); @@ -43,10 +43,33 @@ va_start(vl, fmt); vsnprintf(buff, sizeof(buff), fmt, vl); va_end(vl); -FILE * f; +LogString(buff); +} +//----------------------------------------------------------------------------- +const char * STG_LOGGER::LogDate(time_t t) const +{ +static char s[32]; +if (t == 0) + t = time(NULL); + +struct tm * 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); + +return s; +} +//----------------------------------------------------------------------------- +void STG_LOGGER::LogString(const char * str) const +{ if (!fileName.empty()) { - f = fopen(fileName.c_str(), "at"); + FILE * f = fopen(fileName.c_str(), "at"); if (f) { #ifdef STG_TIME @@ -55,41 +78,50 @@ if (!fileName.empty()) fprintf(f, "%s", LogDate(time(NULL))); #endif fprintf(f, " -- "); - fprintf(f, "%s", buff); + fprintf(f, "%s", str); fprintf(f, "\n"); fclose(f); } else { openlog("stg", LOG_NDELAY, LOG_USER); - syslog(LOG_CRIT, "%s", buff); + syslog(LOG_CRIT, "%s", str); closelog(); } } else { openlog("stg", LOG_NDELAY, LOG_USER); - syslog(LOG_CRIT, "%s", buff); + syslog(LOG_CRIT, "%s", str); closelog(); } } //----------------------------------------------------------------------------- -const char * STG_LOGGER::LogDate(time_t t) +PLUGIN_LOGGER::PLUGIN_LOGGER(const STG_LOGGER& logger, const std::string& pn) + : m_parent(logger), + m_pluginName(pn) { -static char s[32]; -if (t == 0) - t = time(NULL); - -struct tm * tt = localtime(&t); +} +//----------------------------------------------------------------------------- +void PLUGIN_LOGGER::operator()(const char * fmt, ...) const +{ +char buff[2029]; -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); +va_list vl; +va_start(vl, fmt); +vsnprintf(buff, sizeof(buff), fmt, vl); +va_end(vl); -return s; +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()); +} +//----------------------------------------------------------------------------- +PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::string & pluginName) +{ +return PLUGIN_LOGGER(logger, pluginName); } //-----------------------------------------------------------------------------