Logger: Print correct source file and line number

Before this commit, always Logger.cpp:53 was being printed.

Change-Id: Ie5c64b4961c7c41d23484784a93eda5e08331f08
This commit is contained in:
Pau Espin 2018-04-25 16:43:02 +02:00
parent f3d7f443a0
commit 3b78cbfdc1
3 changed files with 13 additions and 10 deletions

View File

@ -50,7 +50,7 @@ Log::~Log()
ScopedLock lock(gLogToLock); ScopedLock lock(gLogToLock);
// The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers,
// so just use std::cout. // so just use std::cout.
LOGP(mCategory, mPriority, fmt, mStream.str().c_str()); LOGPSRC(mCategory, mPriority, filename, line, fmt, mStream.str().c_str());
} }
ostringstream& Log::get() ostringstream& Log::get()

View File

@ -48,10 +48,10 @@ extern "C" {
#endif #endif
#define LOG(level) \ #define LOG(level) \
Log(DMAIN, LOGL_##level).get() << "[tid=" << pthread_self() << "] " Log(DMAIN, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] "
#define LOGC(category, level) \ #define LOGC(category, level) \
Log(category, LOGL_##level).get() << "[tid=" << pthread_self() << "] " Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] "
/** /**
A C++ stream-based thread-safe logger. A C++ stream-based thread-safe logger.
@ -67,11 +67,14 @@ class Log {
std::ostringstream mStream; ///< This is where we buffer up the log entry. std::ostringstream mStream; ///< This is where we buffer up the log entry.
int mCategory; ///< Priority of current report. int mCategory; ///< Priority of current report.
int mPriority; ///< Category of current report. int mPriority; ///< Category of current report.
const char *filename; ///< Source File Name of current report.
int line; ///< Line number in source file of current report.
public: public:
Log(int wCategory, int wPriority) Log(int wCategory, int wPriority, const char* filename, int line)
: mCategory(wCategory), mPriority(wPriority) : mCategory(wCategory), mPriority(wPriority),
filename(filename), line(line)
{ } { }
// Most of the work is in the destructor. // Most of the work is in the destructor.

View File

@ -61,9 +61,9 @@ int main(int argc, char *argv[])
log_set_print_filename(osmo_stderr_target, 0); log_set_print_filename(osmo_stderr_target, 0);
log_set_print_level(osmo_stderr_target, 1); log_set_print_level(osmo_stderr_target, 1);
Log(MYCAT, LOGL_FATAL).get() << "testing the logger."; Log(MYCAT, LOGL_FATAL, __BASE_FILE__, __LINE__).get() << "testing the logger.";
Log(MYCAT, LOGL_ERROR).get() << "testing the logger."; Log(MYCAT, LOGL_ERROR, __BASE_FILE__, __LINE__).get() << "testing the logger.";
Log(MYCAT, LOGL_NOTICE).get() << "testing the logger."; Log(MYCAT, LOGL_NOTICE, __BASE_FILE__, __LINE__).get() << "testing the logger.";
Log(MYCAT, LOGL_INFO).get() << "testing the logger."; Log(MYCAT, LOGL_INFO, __BASE_FILE__, __LINE__).get() << "testing the logger.";
Log(MYCAT, LOGL_DEBUG).get() << "testing the logger."; Log(MYCAT, LOGL_DEBUG, __BASE_FILE__, __LINE__).get() << "testing the logger.";
} }