At the moment every `LOG()` macro invocation results in a `LogMessage` being
created, the message serialized into an `std::stringstream`. Only in the
destructor is it actually checked whether the given `LogCategory` enables
the given log level.
This is not too efficient, it would be better to skip the log message
construction and all the `operator<<()` invocations if the message will
just be discarded.
This could be easily done if the `LOG()` macro accepted its arguments like a
traditional function as in that case an appropriate `if` statement can be
injected in a do-while loop. However, that is not the case, the `LOG()` macro
should effectively "return" a stream.
It is not possible inject an `if` statement directly as that would
lead to issues:
if (...)
LOG(...)
else
...
The `else` would bind the to the `if` in the `LOG()` macro. This is
diagnosed by `-Wdangling-else`.
An alternative approach would be to use a `for` loop and force a single
iteration using a boolean flag or similar. This is entirely doable but
I think the implemented approach is easier to understand.
This change implements the early log level checking using a `switch` statement
as this avoids the dangling else related issues. One small issue arises
because having a boolean controlling expression is diagnosed by clang
(`-Wswitch-bool`); the result is cast to `int` to avoid the warning.
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
143 lines
3.7 KiB
C++
143 lines
3.7 KiB
C++
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
/*
|
|
* Copyright (C) 2018, Google Inc.
|
|
*
|
|
* Logging infrastructure
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <sstream>
|
|
#include <string_view>
|
|
|
|
#include <libcamera/base/private.h>
|
|
|
|
#include <libcamera/base/class.h>
|
|
#include <libcamera/base/utils.h>
|
|
|
|
namespace libcamera {
|
|
|
|
enum LogSeverity {
|
|
LogInvalid = -1,
|
|
LogDebug = 0,
|
|
LogInfo,
|
|
LogWarning,
|
|
LogError,
|
|
LogFatal,
|
|
};
|
|
|
|
class LogCategory
|
|
{
|
|
public:
|
|
static LogCategory *create(std::string_view name);
|
|
|
|
const std::string &name() const { return name_; }
|
|
LogSeverity severity() const { return severity_.load(std::memory_order_relaxed); }
|
|
void setSeverity(LogSeverity severity) { severity_.store(severity, std::memory_order_relaxed); }
|
|
|
|
static const LogCategory &defaultCategory();
|
|
|
|
private:
|
|
friend class Logger;
|
|
explicit LogCategory(std::string_view name);
|
|
|
|
const std::string name_;
|
|
|
|
std::atomic<LogSeverity> severity_;
|
|
static_assert(decltype(severity_)::is_always_lock_free);
|
|
};
|
|
|
|
#define LOG_DECLARE_CATEGORY(name) \
|
|
extern const LogCategory &_LOG_CATEGORY(name)();
|
|
|
|
#define LOG_DEFINE_CATEGORY(name) \
|
|
LOG_DECLARE_CATEGORY(name) \
|
|
const LogCategory &_LOG_CATEGORY(name)() \
|
|
{ \
|
|
/* The instance will be deleted by the Logger destructor. */ \
|
|
static LogCategory *category = LogCategory::create(#name); \
|
|
return *category; \
|
|
}
|
|
|
|
class LogMessage
|
|
{
|
|
public:
|
|
LogMessage(const char *fileName, unsigned int line,
|
|
const LogCategory &category, LogSeverity severity,
|
|
std::string prefix = {});
|
|
~LogMessage();
|
|
|
|
std::ostream &stream() { return msgStream_; }
|
|
|
|
const utils::time_point ×tamp() const { return timestamp_; }
|
|
LogSeverity severity() const { return severity_; }
|
|
const LogCategory &category() const { return category_; }
|
|
const std::string &fileInfo() const { return fileInfo_; }
|
|
const std::string &prefix() const { return prefix_; }
|
|
std::string msg() const { return msgStream_.str(); }
|
|
|
|
private:
|
|
LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessage)
|
|
|
|
std::ostringstream msgStream_;
|
|
const LogCategory &category_;
|
|
LogSeverity severity_;
|
|
utils::time_point timestamp_;
|
|
std::string fileInfo_;
|
|
std::string prefix_;
|
|
};
|
|
|
|
class Loggable
|
|
{
|
|
public:
|
|
virtual ~Loggable();
|
|
|
|
protected:
|
|
virtual std::string logPrefix() const = 0;
|
|
|
|
LogMessage _log(const LogCategory &category, LogSeverity severity,
|
|
const char *fileName = __builtin_FILE(),
|
|
unsigned int line = __builtin_LINE()) const;
|
|
};
|
|
|
|
LogMessage _log(const LogCategory &category, LogSeverity severity,
|
|
const char *fileName = __builtin_FILE(),
|
|
unsigned int line = __builtin_LINE());
|
|
|
|
#ifndef __DOXYGEN__
|
|
#define _LOG_CATEGORY(name) logCategory##name
|
|
|
|
#define _LOG(cat, sev) \
|
|
switch (const auto &_logCategory = (cat); \
|
|
static_cast<int>(_logCategory.severity() <= Log##sev)) \
|
|
case 1: \
|
|
_log(_logCategory, Log##sev).stream()
|
|
|
|
#define _LOG1(severity) \
|
|
_LOG(LogCategory::defaultCategory(), severity)
|
|
#define _LOG2(category, severity) \
|
|
_LOG(_LOG_CATEGORY(category)(), severity)
|
|
|
|
/*
|
|
* Expand the LOG() macro to _LOG1() or _LOG2() based on the number of
|
|
* arguments.
|
|
*/
|
|
#define _LOG_MACRO(_1, _2, NAME, ...) NAME
|
|
#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__)
|
|
#else /* __DOXYGEN___ */
|
|
#define LOG(category, severity)
|
|
#endif /* __DOXYGEN__ */
|
|
|
|
#ifndef NDEBUG
|
|
#define ASSERT(condition) static_cast<void>(({ \
|
|
if (!(condition)) \
|
|
LOG(Fatal) << "assertion \"" #condition "\" failed in " \
|
|
<< __func__ << "()"; \
|
|
}))
|
|
#else
|
|
#define ASSERT(condition) static_cast<void>(false && (condition))
|
|
#endif
|
|
|
|
} /* namespace libcamera */
|