libcamera: log: Use compiler builtins to retrieve file and line number

Replace the __FILE__ and __LINE__ values passed to the _log() function
with default parameters, taking their values from the __builtin_FILE()
and __builtin_LINE() functions. This moves handling of the file and line
from the preprocessor to the compiler, which is generally preferred as
it increases type safety.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Tested-by: Sebastian Fricke <sebastian.fricke@posteo.net>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2021-04-14 00:26:39 +03:00
parent 93be96431a
commit df1d955d24
2 changed files with 16 additions and 16 deletions

View file

@ -89,21 +89,22 @@ public:
protected: protected:
virtual std::string logPrefix() const = 0; virtual std::string logPrefix() const = 0;
LogMessage _log(const char *file, unsigned int line, LogMessage _log(const LogCategory *category, LogSeverity severity,
const LogCategory *category, const char *fileName = __builtin_FILE(),
LogSeverity severity) const; unsigned int line = __builtin_LINE()) const;
}; };
LogMessage _log(const char *file, unsigned int line, LogMessage _log(const LogCategory *category, LogSeverity severity,
const LogCategory *category, LogSeverity severity); const char *fileName = __builtin_FILE(),
unsigned int line = __builtin_LINE());
#ifndef __DOXYGEN__ #ifndef __DOXYGEN__
#define _LOG_CATEGORY(name) logCategory##name #define _LOG_CATEGORY(name) logCategory##name
#define _LOG1(severity) \ #define _LOG1(severity) \
_log(__FILE__, __LINE__, nullptr, Log##severity).stream() _log(nullptr, Log##severity).stream()
#define _LOG2(category, severity) \ #define _LOG2(category, severity) \
_log(__FILE__, __LINE__, &_LOG_CATEGORY(category)(), Log##severity).stream() _log(&_LOG_CATEGORY(category)(), Log##severity).stream()
/* /*
* Expand the LOG() macro to _LOG1() or _LOG2() based on the number of * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of

View file

@ -897,19 +897,18 @@ Loggable::~Loggable()
/** /**
* \brief Create a temporary LogMessage object to log a message * \brief Create a temporary LogMessage object to log a message
* \param[in] fileName The file name where the message is logged from
* \param[in] line The line number where the message is logged from
* \param[in] category The log message category * \param[in] category The log message category
* \param[in] severity The log message severity * \param[in] severity The log message severity
* \param[in] fileName The file name where the message is logged from
* \param[in] line The line number where the message is logged from
* *
* This method is used as a backeng by the LOG() macro to create a log message * This method is used as a backeng by the LOG() macro to create a log message
* for locations inheriting from the Loggable class. * for locations inheriting from the Loggable class.
* *
* \return A log message * \return A log message
*/ */
LogMessage Loggable::_log(const char *fileName, unsigned int line, LogMessage Loggable::_log(const LogCategory *category, LogSeverity severity,
const LogCategory *category, const char *fileName, unsigned int line) const
LogSeverity severity) const
{ {
LogMessage msg(fileName, line, LogMessage msg(fileName, line,
category ? *category : LogCategory::defaultCategory(), category ? *category : LogCategory::defaultCategory(),
@ -921,18 +920,18 @@ LogMessage Loggable::_log(const char *fileName, unsigned int line,
/** /**
* \brief Create a temporary LogMessage object to log a message * \brief Create a temporary LogMessage object to log a message
* \param[in] fileName The file name where the message is logged from
* \param[in] line The line number where the message is logged from
* \param[in] category The log message category * \param[in] category The log message category
* \param[in] severity The log message severity * \param[in] severity The log message severity
* \param[in] fileName The file name where the message is logged from
* \param[in] line The line number where the message is logged from
* *
* This function is used as a backeng by the LOG() macro to create a log * This function is used as a backeng by the LOG() macro to create a log
* message for locations not inheriting from the Loggable class. * message for locations not inheriting from the Loggable class.
* *
* \return A log message * \return A log message
*/ */
LogMessage _log(const char *fileName, unsigned int line, LogMessage _log(const LogCategory *category, LogSeverity severity,
const LogCategory *category, LogSeverity severity) const char *fileName, unsigned int line)
{ {
return LogMessage(fileName, line, return LogMessage(fileName, line,
category ? *category : LogCategory::defaultCategory(), category ? *category : LogCategory::defaultCategory(),