[PATCH] osmo-trx[master]: Logger: Use libosmocore logging system
Hello Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/6620 to look at the new patch set (#5). Logger: Use libosmocore logging system We still need an intermediate class Logger due to osmo-trx being multi-threaded and requiring to have a lock to use libosmocore, which is not thread safe. Change-Id: I30baac89f53e927f8699d0586b43cccf88ecd493 --- M CommonLibs/Logger.cpp M CommonLibs/Logger.h M CommonLibs/Makefile.am M Transceiver52M/osmo-trx.cpp M tests/CommonLibs/LogTest.cpp M tests/CommonLibs/LogTest.err M tests/CommonLibs/LogTest.ok M tests/CommonLibs/Makefile.am 8 files changed, 65 insertions(+), 190 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/20/6620/5 diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp index 4c2a2d3..ac3de42 100644 --- a/CommonLibs/Logger.cpp +++ b/CommonLibs/Logger.cpp @@ -1,7 +1,5 @@ /* -* Copyright 2009, 2010 Free Software Foundation, Inc. -* Copyright 2010 Kestrel Signal Processing, Inc. -* Copyright 2011, 2012 Range Networks, Inc. +* Copyright (C) 2018 sysmocom - s.f.m.c. GmbH * * * This software is distributed under the terms of the GNU Affero Public License. @@ -39,55 +37,6 @@ Mutex gLogToLock; -// Global log level threshold: -int config_log_level; - -/** Names of the logging levels. */ -const char *levelNames[] = { - "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" -}; -int numLevels = 8; - - -int levelStringToInt(const string& name) -{ - // Reverse search, since the numerically larger levels are more common. - for (int i=numLevels-1; i>=0; i--) { - if (name == levelNames[i]) return i; - } - - // Common substitutions. - if (name=="INFORMATION") return 6; - if (name=="WARN") return 4; - if (name=="ERROR") return 3; - if (name=="CRITICAL") return 2; - if (name=="EMERGENCY") return 0; - - // Unknown level. - return -1; -} - -static std::string format(const char *fmt, ...) -{ - va_list ap; - char buf[300]; - va_start(ap,fmt); - int n = vsnprintf(buf,300,fmt,ap); - va_end(ap); - if (n >= (300-4)) { strcpy([(300-4)],"..."); } - return std::string(buf); -} - -const std::string timestr() -{ - struct timeval tv; - struct tm tm; - gettimeofday(,NULL); - localtime_r(_sec,); - unsigned tenths = tv.tv_usec / 10; // Rounding down is ok. - return format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); -} - std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) { return os << ss.str(); @@ -95,34 +44,18 @@ Log::~Log() { - // Anything at or above LOG_CRIT is an "alarm". - if (mPriority <= LOG_ERR) { - cerr << mStream.str() << endl; - } - int mlen = mStream.str().size(); int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); + const char *fmt = neednl ? "%s\n" : "%s"; ScopedLock lock(gLogToLock); // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, // so just use std::cout. - std::cout << mStream.str(); - if (neednl) std::cout<<"\n"; + LOGP(mCategory, mPriority, fmt, mStream.str().c_str()); } ostringstream& Log::get() { - assert(mPriority=LOG_##wLevel) - -#ifdef NDEBUG -#define
[PATCH] osmo-trx[master]: Logger: Use libosmocore logging system
Hello Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/6620 to look at the new patch set (#4). Logger: Use libosmocore logging system We still need an intermediate class Logger due to osmo-trx being multi-threaded and requiring to have a lock to use libosmocore, which is not thread safe. Change-Id: I30baac89f53e927f8699d0586b43cccf88ecd493 --- M CommonLibs/Logger.cpp M CommonLibs/Logger.h M CommonLibs/Makefile.am M Transceiver52M/osmo-trx.cpp M tests/CommonLibs/LogTest.cpp M tests/CommonLibs/LogTest.err M tests/CommonLibs/LogTest.ok M tests/CommonLibs/Makefile.am 8 files changed, 64 insertions(+), 187 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/20/6620/4 diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp index 4c2a2d3..d6125a7 100644 --- a/CommonLibs/Logger.cpp +++ b/CommonLibs/Logger.cpp @@ -39,55 +39,6 @@ Mutex gLogToLock; -// Global log level threshold: -int config_log_level; - -/** Names of the logging levels. */ -const char *levelNames[] = { - "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" -}; -int numLevels = 8; - - -int levelStringToInt(const string& name) -{ - // Reverse search, since the numerically larger levels are more common. - for (int i=numLevels-1; i>=0; i--) { - if (name == levelNames[i]) return i; - } - - // Common substitutions. - if (name=="INFORMATION") return 6; - if (name=="WARN") return 4; - if (name=="ERROR") return 3; - if (name=="CRITICAL") return 2; - if (name=="EMERGENCY") return 0; - - // Unknown level. - return -1; -} - -static std::string format(const char *fmt, ...) -{ - va_list ap; - char buf[300]; - va_start(ap,fmt); - int n = vsnprintf(buf,300,fmt,ap); - va_end(ap); - if (n >= (300-4)) { strcpy([(300-4)],"..."); } - return std::string(buf); -} - -const std::string timestr() -{ - struct timeval tv; - struct tm tm; - gettimeofday(,NULL); - localtime_r(_sec,); - unsigned tenths = tv.tv_usec / 10; // Rounding down is ok. - return format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); -} - std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) { return os << ss.str(); @@ -95,34 +46,18 @@ Log::~Log() { - // Anything at or above LOG_CRIT is an "alarm". - if (mPriority <= LOG_ERR) { - cerr << mStream.str() << endl; - } - int mlen = mStream.str().size(); int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); + const char *fmt = neednl ? "%s\n" : "%s"; ScopedLock lock(gLogToLock); // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, // so just use std::cout. - std::cout << mStream.str(); - if (neednl) std::cout<<"\n"; + LOGP(mCategory, mPriority, fmt, mStream.str().c_str()); } ostringstream& Log::get() { - assert(mPriority=LOG_##wLevel) - -#ifdef NDEBUG -#define LOG(wLevel) \ - if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel) -#else -#define LOG(wLevel) \ - if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel) +/* Translation for old log statements */ +#ifndef LOGL_ALERT +#define LOGL_ALERT LOGL_FATAL #endif +#ifndef LOGL_ERR +#define LOGL_ERR
[PATCH] osmo-trx[master]: Logger: Use libosmocore logging system
Review at https://gerrit.osmocom.org/6620 Logger: Use libosmocore logging system We still need an intermediate class Logger due to osmo-trx being multi-threaded and requiring to have a lock to use libosmocore, which is not thread safe. Change-Id: I30baac89f53e927f8699d0586b43cccf88ecd493 --- M CommonLibs/Logger.cpp M CommonLibs/Logger.h M CommonLibs/Makefile.am M Transceiver52M/osmo-trx.cpp M tests/CommonLibs/LogTest.cpp M tests/CommonLibs/LogTest.err M tests/CommonLibs/LogTest.ok M tests/CommonLibs/Makefile.am 8 files changed, 64 insertions(+), 187 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/20/6620/1 diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp index 868f795..d6125a7 100644 --- a/CommonLibs/Logger.cpp +++ b/CommonLibs/Logger.cpp @@ -39,55 +39,6 @@ Mutex gLogToLock; -// Global log level threshold: -int config_log_level; - -/** Names of the logging levels. */ -const char *levelNames[] = { - "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" -}; -int numLevels = 8; - - -int levelStringToInt(const string& name) -{ - // Reverse search, since the numerically larger levels are more common. - for (int i=numLevels-1; i>=0; i--) { - if (name == levelNames[i]) return i; - } - - // Common substitutions. - if (name=="INFORMATION") return 6; - if (name=="WARN") return 4; - if (name=="ERROR") return 3; - if (name=="CRITICAL") return 2; - if (name=="EMERGENCY") return 0; - - // Unknown level. - return -1; -} - -static std::string format(const char *fmt, ...) -{ - va_list ap; - char buf[300]; - va_start(ap,fmt); - int n = vsnprintf(buf,300,fmt,ap); - va_end(ap); - if (n >= (300-4)) { strcpy([(300-4)],"..."); } - return std::string(buf); -} - -const std::string timestr() -{ - struct timeval tv; - struct tm tm; - gettimeofday(,NULL); - localtime_r(_sec,); - unsigned tenths = tv.tv_usec / 10; // Rounding down is ok. - return format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); -} - std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) { return os << ss.str(); @@ -95,34 +46,18 @@ Log::~Log() { - // Anything at or above LOG_CRIT is an "alarm". - if (mPriority <= LOG_ERR) { - cerr << mStream.str() << endl; - } - int mlen = mStream.str().size(); int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); + const char *fmt = neednl ? "%s\n" : "%s"; ScopedLock lock(gLogToLock); // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, // so just use std::cout. - std::cout << mStream.str(); - if (neednl) std::cout<<"\n"; + LOGP(mCategory, mPriority, fmt, mStream.str().c_str()); } ostringstream& Log::get() { - assert(mPriority=LOG_##wLevel) - -#ifdef NDEBUG -#define LOG(wLevel) \ - if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel) -#else -#define LOG(wLevel) \ - if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel) +/* Translation for old log statements */ +#ifndef LOGL_ALERT +#define LOGL_ALERT LOGL_FATAL #endif +#ifndef LOGL_ERR +#define LOGL_ERR LOGL_ERROR +#endif +#ifndef LOGL_WARNING +#define LOGL_WARNING LOGL_NOTICE +#endif + +#define LOG(level) \ +