libcamera: include/libcamera/base/log.h Source File

libcamera v0.0.1 Supporting cameras in Linux since 2019

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 * log.h - Logging infrastructure
6 */
7
8#pragma once
9
10#include <chrono>
11#include <sstream>
12
13#include <libcamera/base/private.h>
14
15#include <libcamera/base/class.h>
16#include <libcamera/base/utils.h>
17
18namespace libcamera {
19
20 enum LogSeverity {
21 LogInvalid = -1,
22 LogDebug = 0,
23 LogInfo,
24 LogWarning,
25 LogError,
26 LogFatal,
27};
28
29 class LogCategory
30{
31public:
32 explicit LogCategory(const char *name);
33
34 const char *name() const { return name_; }
35 LogSeverity severity() const { return severity_; }
36 void setSeverity(LogSeverity severity);
37
38 static const LogCategory &defaultCategory();
39
40private:
41 const char *name_;
42 LogSeverity severity_;
43};
44
45#define LOG_DECLARE_CATEGORY(name) \
46extern const LogCategory &_LOG_CATEGORY(name)();
47
48#define LOG_DEFINE_CATEGORY(name) \
49const LogCategory &_LOG_CATEGORY(name)() \
50{ \
51 /* The instance will be deleted by the Logger destructor. */ \
52 static LogCategory *category = new LogCategory(#name); \
53 return *category; \
54}
55
56 class LogMessage
57{
58public:
59 LogMessage(const char *fileName, unsigned int line,
60 const LogCategory &category, LogSeverity severity,
61 const std::string &prefix = std::string());
62
63 LogMessage(LogMessage &&);
64 ~LogMessage();
65
66 std::ostream &stream() { return msgStream_; }
67
68 const utils::time_point &timestamp() const { return timestamp_; }
69 LogSeverity severity() const { return severity_; }
70 const LogCategory &category() const { return category_; }
71 const std::string &fileInfo() const { return fileInfo_; }
72 const std::string &prefix() const { return prefix_; }
73 const std::string msg() const { return msgStream_.str(); }
74
75private:
76 LIBCAMERA_DISABLE_COPY(LogMessage)
77
78 void init(const char *fileName, unsigned int line);
79
80 std::ostringstream msgStream_;
81 const LogCategory &category_;
82 LogSeverity severity_;
83 utils::time_point timestamp_;
84 std::string fileInfo_;
85 std::string prefix_;
86};
87
88 class Loggable
89{
90public:
91 virtual ~Loggable();
92
93protected:
94 virtual std::string logPrefix() const = 0;
95
96 LogMessage _log(const LogCategory *category, LogSeverity severity,
97 const char *fileName = __builtin_FILE(),
98 unsigned int line = __builtin_LINE()) const;
99};
100
101 LogMessage _log(const LogCategory *category, LogSeverity severity,
102 const char *fileName = __builtin_FILE(),
103 unsigned int line = __builtin_LINE());
104
105#ifndef __DOXYGEN__
106#define _LOG_CATEGORY(name) logCategory##name
107
108#define _LOG1(severity) \
109 _log(nullptr, Log##severity).stream()
110#define _LOG2(category, severity) \
111 _log(&_LOG_CATEGORY(category)(), Log##severity).stream()
112
113/*
114 * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of
115 * arguments.
116 */
117#define _LOG_MACRO(_1, _2, NAME, ...) NAME
118#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__)
119#else /* __DOXYGEN___ */
120#define LOG(category, severity)
121#endif /* __DOXYGEN__ */
122
123#ifndef NDEBUG
124#define ASSERT(condition) static_cast<void>(({ \
125 if (!(condition)) \
126 LOG(Fatal) << "assertion \"" #condition "\" failed in " \
127 << __func__ << "()"; \
128}))
129#else
130#define ASSERT(condition) static_cast<void>(false && (condition))
131#endif
132
133} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
libcamera::LogCategory
A category of log message.
Definition: log.h:30
libcamera::LogCategory::name
const char * name() const
Retrieve the log category name.
Definition: log.h:34
libcamera::LogCategory::severity
LogSeverity severity() const
Retrieve the severity of the log category.
Definition: log.h:35
libcamera::LogCategory::defaultCategory
static const LogCategory & defaultCategory()
Retrieve the default log category.
Definition: log.cpp:805
libcamera::LogCategory::LogCategory
LogCategory(const char *name)
Construct a log category.
Definition: log.cpp:767
libcamera::LogCategory::setSeverity
void setSeverity(LogSeverity severity)
Set the severity of the log category.
Definition: log.cpp:792
libcamera::LogMessage
Internal log message representation.
Definition: log.h:57
libcamera::LogMessage::category
const LogCategory & category() const
Retrieve the category of the log message.
Definition: log.h:70
libcamera::LogMessage::LogMessage
LogMessage(const char *fileName, unsigned int line, const LogCategory &category, LogSeverity severity, const std::string &prefix=std::string())
Construct a log message for a given category.
Definition: log.cpp:835
libcamera::LogMessage::msg
const std::string msg() const
Retrieve the message text of the log message.
Definition: log.h:73
libcamera::LogMessage::fileInfo
const std::string & fileInfo() const
Retrieve the file info of the log message.
Definition: log.h:71
libcamera::LogMessage::prefix
const std::string & prefix() const
Retrieve the prefix of the log message.
Definition: log.h:72
libcamera::LogMessage::timestamp
const utils::time_point & timestamp() const
Retrieve the timestamp of the log message.
Definition: log.h:68
libcamera::LogMessage::stream
std::ostream & stream()
Definition: log.h:66
libcamera::LogMessage::severity
LogSeverity severity() const
Retrieve the severity of the log message.
Definition: log.h:69
libcamera::Loggable
Base class to support log message extensions.
Definition: log.h:89
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:977
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:997
libcamera::LogSeverity
LogSeverity
Definition: log.h:20
libcamera::LogWarning
@ LogWarning
Definition: log.h:24
libcamera::LogFatal
@ LogFatal
Definition: log.h:26
libcamera::LogError
@ LogError
Definition: log.h:25
libcamera::LogDebug
@ LogDebug
Definition: log.h:22
libcamera::LogInfo
@ LogInfo
Definition: log.h:23
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:73