diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-02-20 20:01:10 +0100 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2018-02-27 07:11:11 +0000 |
commit | 3da1f8352e337fb032bf7a58c2909d3ba918e237 (patch) | |
tree | 33efd20443a363cd783da4dea99255210237097e /CommonLibs | |
parent | 5ea1817dc299611808532eda489981f09f87cba3 (diff) |
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
Diffstat (limited to 'CommonLibs')
-rw-r--r-- | CommonLibs/Logger.cpp | 73 | ||||
-rw-r--r-- | CommonLibs/Logger.h | 66 | ||||
-rw-r--r-- | CommonLibs/Makefile.am | 2 |
3 files changed, 29 insertions, 112 deletions
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 @@ using namespace std; 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(&buf[(300-4)],"..."); } - return std::string(buf); -} - -const std::string timestr() -{ - struct timeval tv; - struct tm tm; - gettimeofday(&tv,NULL); - localtime_r(&tv.tv_sec,&tm); - unsigned tenths = tv.tv_usec / 100000; // 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 @@ std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) 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<numLevels); - mStream << levelNames[mPriority] << ' '; return mStream; } - - -void gLogInit(const char* level, char *fn) -{ - // Set the level if one has been specified. - if (level) - config_log_level = levelStringToInt(level); -} - // vim: ts=4 sw=4 diff --git a/CommonLibs/Logger.h b/CommonLibs/Logger.h index a8fe44d..00efcd7 100644 --- a/CommonLibs/Logger.h +++ b/CommonLibs/Logger.h @@ -23,12 +23,6 @@ */ -// (pat) WARNING is stupidly defined in /usr/local/include/osipparser2/osip_const.h. -// This must be outside the #ifndef LOGGER_H to fix it as long as Logger.h included after the above file. -#ifdef WARNING -#undef WARNING -#endif - #ifndef LOGGER_H #define LOGGER_H @@ -37,31 +31,28 @@ #include <sstream> #include <string> -extern int config_log_level; - -#define LOG_EMERG 0 /* system is unusable */ -#define LOG_ALERT 1 /* action must be taken immediately */ -#define LOG_CRIT 2 /* critical conditions */ -#define LOG_ERR 3 /* error conditions */ -#define LOG_WARNING 4 /* warning conditions */ -#define LOG_NOTICE 5 /* normal but significant condition */ -#define LOG_INFO 6 /* informational */ -#define LOG_DEBUG 7 /* debug-level messages */ - -#define _LOG(level) \ - Log(LOG_##level).get() << pthread_self() \ - << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " - -#define IS_LOG_LEVEL(wLevel) (config_log_level>=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) +extern "C" { +#include <osmocom/core/logging.h> +#include "debug.h" +} + +/* 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) \ + Log(DMAIN, LOGL_##level).get() << "[tid=" << pthread_self() << "] " + +#define LOGC(category, level) \ + Log(category, LOGL_##level).get() << "[tid=" << pthread_self() << "] " + /** A C++ stream-based thread-safe logger. This object is NOT the global logger; @@ -73,13 +64,14 @@ class Log { protected: - std::ostringstream mStream; ///< This is where we buffer up the log entry. - int mPriority; ///< Priority of current report. + std::ostringstream mStream; ///< This is where we buffer up the log entry. + int mCategory; ///< Priority of current report. + int mPriority; ///< Category of current report. public: - Log(int wPriority) - :mPriority(wPriority) + Log(int wCategory, int wPriority) + : mCategory(wCategory), mPriority(wPriority) { } // Most of the work is in the destructor. @@ -89,16 +81,8 @@ class Log { std::ostringstream& get(); }; -const std::string timestr(); // A timestamp to print in messages. std::ostream& operator<<(std::ostream& os, std::ostringstream& ss); -/**@ Global control and initialization of the logging system. */ -//@{ -/** Initialize the global logging system. */ -void gLogInit(const char* level=NULL, char* fn=NULL); -//@} - - #endif // vim: ts=4 sw=4 diff --git a/CommonLibs/Makefile.am b/CommonLibs/Makefile.am index 843bc38..5d116f9 100644 --- a/CommonLibs/Makefile.am +++ b/CommonLibs/Makefile.am @@ -22,7 +22,7 @@ include $(top_srcdir)/Makefile.common AM_CPPFLAGS = $(STD_DEFINES_AND_INCLUDES) -AM_CXXFLAGS = -Wall -O3 -g -lpthread +AM_CXXFLAGS = -Wall -O3 -g -lpthread $(LIBOSMOCORE_CFLAGS) $(LIBOSMOCTRL_CFLAGS) $(LIBOSMOVTY_CFLAGS) AM_CFLAGS = $(LIBOSMOCORE_CFLAGS) $(LIBOSMOCTRL_CFLAGS) $(LIBOSMOVTY_CFLAGS) EXTRA_DIST = \ |