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