libcamera v0.7.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2018, Google Inc.
4 *
5 * Logging infrastructure
6 */
7
8#pragma once
9
10#include <atomic>
11#include <sstream>
12#include <string_view>
13
14#include <libcamera/base/private.h>
15
16#include <libcamera/base/class.h>
17#include <libcamera/base/utils.h>
18
19 namespace libcamera {
20
21 enum LogSeverity {
22 LogInvalid = -1,
23 LogDebug = 0,
24 LogInfo,
25 LogWarning,
26 LogError,
27 LogFatal,
28};
29
30 class LogCategory
31{
32 public:
33 static LogCategory *create(std::string_view name);
34
35 const std::string &name() const { return name_; }
36 LogSeverity severity() const { return severity_.load(std::memory_order_relaxed); }
37 void setSeverity(LogSeverity severity) { severity_.store(severity, std::memory_order_relaxed); }
38
39 static const LogCategory &defaultCategory();
40
41 private:
42 friend class Logger;
43 explicit LogCategory(std::string_view name);
44
45 const std::string name_;
46
47 std::atomic<LogSeverity> severity_;
48 static_assert(decltype(severity_)::is_always_lock_free);
49};
50
51#define LOG_DECLARE_CATEGORY(name) \
52 extern const LogCategory &_LOG_CATEGORY(name)();
53
54#define LOG_DEFINE_CATEGORY(name) \
55 LOG_DECLARE_CATEGORY(name) \
56 const LogCategory &_LOG_CATEGORY(name)() \
57{ \
58 /* The instance will be deleted by the Logger destructor. */ \
59 static LogCategory *category = LogCategory::create(#name); \
60 return *category; \
61}
62
63 class LogMessage
64{
65 public:
66 LogMessage(const char *fileName, unsigned int line,
67 const LogCategory &category, LogSeverity severity,
68 std::string prefix = {});
69 ~LogMessage();
70
71 std::ostream &stream() { return msgStream_; }
72
73 const utils::time_point &timestamp() const { return timestamp_; }
74 LogSeverity severity() const { return severity_; }
75 const LogCategory &category() const { return category_; }
76 const std::string &fileInfo() const { return fileInfo_; }
77 const std::string &prefix() const { return prefix_; }
78 std::string msg() const { return msgStream_.str(); }
79
80 private:
81 LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessage)
82
83 std::ostringstream msgStream_;
84 const LogCategory &category_;
85 LogSeverity severity_;
86 utils::time_point timestamp_;
87 std::string fileInfo_;
88 std::string prefix_;
89};
90
91#ifndef __DOXYGEN__
92 template<LogSeverity>
93 struct LogMessageAbortGuard {
94};
95
96 template<>
97 struct LogMessageAbortGuard<LogFatal> {
98 LogMessageAbortGuard() = default;
99 LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessageAbortGuard)
100 [[noreturn]] ~LogMessageAbortGuard();
101};
102#endif
103
104 class Loggable
105{
106 public:
107 virtual ~Loggable();
108
109 protected:
110 virtual std::string logPrefix() const = 0;
111
112 LogMessage _log(const LogCategory &category, LogSeverity severity,
113 const char *fileName = __builtin_FILE(),
114 unsigned int line = __builtin_LINE()) const;
115};
116
117 LogMessage _log(const LogCategory &category, LogSeverity severity,
118 const char *fileName = __builtin_FILE(),
119 unsigned int line = __builtin_LINE());
120
121#ifndef __DOXYGEN__
122#define _LOG_CATEGORY(name) logCategory##name
123
124/* Returns `int` to avoid `-Wswitch-bool` below. */
125 template<LogSeverity Severity>
126 constexpr int isLogSeverityEnabled(const LogCategory &category)
127{
128 static_assert(LogDebug <= Severity && Severity <= LogFatal);
129
130 if constexpr (Severity < LogFatal)
131 return static_cast<unsigned int>(category.severity()) <= Severity;
132 else
133 return true;
134}
135
136#define _LOG(cat, sev) \
137 switch (const auto &_logCategory = (cat); \
138 isLogSeverityEnabled<Log##sev>(_logCategory)) \
139 case 1: \
140 (LogMessageAbortGuard<Log##sev>(), \
141 _log(_logCategory, Log##sev).stream())
142
143#define _LOG1(severity) \
144 _LOG(LogCategory::defaultCategory(), severity)
145#define _LOG2(category, severity) \
146 _LOG(_LOG_CATEGORY(category)(), severity)
147
148/*
149 * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of
150 * arguments.
151 */
152#define _LOG_MACRO(_1, _2, NAME, ...) NAME
153#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__)
154#else /* __DOXYGEN___ */
155#define LOG(category, severity)
156#endif /* __DOXYGEN__ */
157
158#ifndef NDEBUG
159#define ASSERT(condition) static_cast<void>(({ \
160 if (!(condition)) \
161 LOG(Fatal) \
162 << "assertion \"" #condition "\" failed in " \
163 << __func__ << "()"; \
164}))
165#else
166#define ASSERT(condition) static_cast<void>(false && (condition))
167#endif
168
169} /* 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:780
libcamera::LogCategory::defaultCategory
static const LogCategory & defaultCategory()
Retrieve the default log category.
Definition log.cpp:823
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:853
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:105
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:969
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:987
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:84