[MERGED] osmo-trx[master]: Logger: Print correct source file and line number

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Thu Apr 26 09:21:19 UTC 2018


Pau Espin Pedrol has submitted this change and it was merged.

Change subject: Logger: Print correct source file and line number
......................................................................


Logger: Print correct source file and line number

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

Change-Id: Ie5c64b4961c7c41d23484784a93eda5e08331f08
---
M CommonLibs/Logger.cpp
M CommonLibs/Logger.h
M tests/CommonLibs/LogTest.cpp
3 files changed, 13 insertions(+), 10 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp
index ac3de42..393d882 100644
--- a/CommonLibs/Logger.cpp
+++ b/CommonLibs/Logger.cpp
@@ -50,7 +50,7 @@
 	ScopedLock lock(gLogToLock);
 	// The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers,
 	// 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()
diff --git a/CommonLibs/Logger.h b/CommonLibs/Logger.h
index 00efcd7..5b0b05c 100644
--- a/CommonLibs/Logger.h
+++ b/CommonLibs/Logger.h
@@ -48,10 +48,10 @@
 #endif
 
 #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) \
-	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.
@@ -67,11 +67,14 @@
 	std::ostringstream mStream;	///< This is where we buffer up the log entry.
 	int mCategory;			///< Priority 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:
 
-	Log(int wCategory, int wPriority)
-		: mCategory(wCategory), mPriority(wPriority)
+	Log(int wCategory, int wPriority, const char* filename, int line)
+		: mCategory(wCategory), mPriority(wPriority),
+		  filename(filename), line(line)
 	{ }
 
 	// Most of the work is in the destructor.
diff --git a/tests/CommonLibs/LogTest.cpp b/tests/CommonLibs/LogTest.cpp
index 5167a62..5e51ce7 100644
--- a/tests/CommonLibs/LogTest.cpp
+++ b/tests/CommonLibs/LogTest.cpp
@@ -61,9 +61,9 @@
 	log_set_print_filename(osmo_stderr_target, 0);
 	log_set_print_level(osmo_stderr_target, 1);
 
-	Log(MYCAT, LOGL_FATAL).get() << "testing the logger.";
-	Log(MYCAT, LOGL_ERROR).get() << "testing the logger.";
-	Log(MYCAT, LOGL_NOTICE).get() << "testing the logger.";
-	Log(MYCAT, LOGL_INFO).get() << "testing the logger.";
-	Log(MYCAT, LOGL_DEBUG).get() << "testing the logger.";
+	Log(MYCAT, LOGL_FATAL, __BASE_FILE__, __LINE__).get() << "testing the logger.";
+	Log(MYCAT, LOGL_ERROR, __BASE_FILE__, __LINE__).get() << "testing the logger.";
+	Log(MYCAT, LOGL_NOTICE, __BASE_FILE__, __LINE__).get() << "testing the logger.";
+	Log(MYCAT, LOGL_INFO, __BASE_FILE__, __LINE__).get() << "testing the logger.";
+	Log(MYCAT, LOGL_DEBUG, __BASE_FILE__, __LINE__).get() << "testing the logger.";
 }

-- 
To view, visit https://gerrit.osmocom.org/7929
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie5c64b4961c7c41d23484784a93eda5e08331f08
Gerrit-PatchSet: 1
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list