Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
/* 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 &timestamp() 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(LogCategory::defaultCategory(), LogFatal).stream() \
<< "assertion \"" #condition "\" failed in " \
<< __func__ << "()"; \
}))
#else
#define ASSERT(condition) static_cast<void>(false && (condition))
#endif
} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY_AND_MOVE
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
libcamera::LogCategory
A category of log message.
Definition log.h:31
libcamera::LogCategory::create
static LogCategory * create(std::string_view name)
Create a new LogCategory or return an existing one.
Definition log.cpp:775
libcamera::LogCategory::defaultCategory
static const LogCategory & defaultCategory()
Retrieve the default log category.
Definition log.cpp:818
libcamera::LogCategory::name
const std::string & name() const
Retrieve the log category name.
Definition log.h:35
libcamera::LogCategory::setSeverity
void setSeverity(LogSeverity severity)
Set the severity of the log category.
Definition log.h:37
libcamera::LogCategory::severity
LogSeverity severity() const
Retrieve the severity of the log category.
Definition log.h:36
libcamera::LogMessage
Internal log message representation.
Definition log.h:64
libcamera::LogMessage::timestamp
const utils::time_point & timestamp() const
Retrieve the timestamp of the log message.
Definition log.h:73
libcamera::LogMessage::category
const LogCategory & category() const
Retrieve the category of the log message.
Definition log.h:75
libcamera::LogMessage::severity
LogSeverity severity() const
Retrieve the severity of the log message.
Definition log.h:74
libcamera::LogMessage::prefix
const std::string & prefix() const
Retrieve the prefix of the log message.
Definition log.h:77
libcamera::LogMessage::msg
std::string msg() const
Retrieve the message text of the log message.
Definition log.h:78
libcamera::LogMessage::LogMessage
LogMessage(const char *fileName, unsigned int line, const LogCategory &category, LogSeverity severity, std::string prefix={})
Construct a log message for a given category.
Definition log.cpp:848
libcamera::LogMessage::fileInfo
const std::string & fileInfo() const
Retrieve the file info of the log message.
Definition log.h:76
libcamera::LogMessage::stream
std::ostream & stream()
Definition log.h:71
libcamera::Loggable
Base class to support log message extensions.
Definition log.h:92
libcamera::Loggable::logPrefix
virtual std::string logPrefix() const =0
Retrieve a string to be prefixed to the log message.
libcamera::Loggable::_log
LogMessage _log(const LogCategory &category, LogSeverity severity, const char *fileName=__builtin_FILE(), unsigned int line=__builtin_LINE()) const
Create a temporary LogMessage object to log a message.
Definition log.cpp:958
libcamera::Logger
Message logger.
Definition log.cpp:299
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::_log
LogMessage _log(const LogCategory &category, LogSeverity severity, const char *fileName=__builtin_FILE(), unsigned int line=__builtin_LINE())
Create a temporary LogMessage object to log a message.
Definition log.cpp:976
libcamera::LogSeverity
LogSeverity
Definition log.h:21
libcamera::LogDebug
@ LogDebug
Definition log.h:23
libcamera::LogInfo
@ LogInfo
Definition log.h:24
libcamera::LogError
@ LogError
Definition log.h:26
libcamera::LogWarning
@ LogWarning
Definition log.h:25
libcamera::LogFatal
@ LogFatal
Definition log.h:27
utils.h
Miscellaneous utility functions.
libcamera::utils::time_point
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition utils.h:75