libcamera  v0.0.0
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 #ifndef __LIBCAMERA_INTERNAL_LOG_H__
8 #define __LIBCAMERA_INTERNAL_LOG_H__
9 
10 #include <chrono>
11 #include <sstream>
12 
14 
15 namespace libcamera {
16 
18  LogInvalid = -1,
19  LogDebug = 0,
24 };
25 
27 {
28 public:
29  explicit LogCategory(const char *name);
30  ~LogCategory();
31 
32  const char *name() const { return name_; }
33  LogSeverity severity() const { return severity_; }
35 
36  static const LogCategory &defaultCategory();
37 
38 private:
39  const char *name_;
40  LogSeverity severity_;
41 };
42 
43 #define LOG_DECLARE_CATEGORY(name) \
44 extern const LogCategory &_LOG_CATEGORY(name)();
45 
46 #define LOG_DEFINE_CATEGORY(name) \
47 const LogCategory &_LOG_CATEGORY(name)() \
48 { \
49  static LogCategory category(#name); \
50  return category; \
51 }
52 
54 {
55 public:
56  LogMessage(const char *fileName, unsigned int line,
58  LogMessage(const char *fileName, unsigned int line,
60  LogMessage(const LogMessage &) = delete;
62  ~LogMessage();
63 
64  std::ostream &stream() { return msgStream_; }
65 
66  const utils::time_point &timestamp() const { return timestamp_; }
67  LogSeverity severity() const { return severity_; }
68  const LogCategory &category() const { return category_; }
69  const std::string &fileInfo() const { return fileInfo_; }
70  const std::string msg() const { return msgStream_.str(); }
71 
72 private:
73  void init(const char *fileName, unsigned int line);
74 
75  std::ostringstream msgStream_;
76  const LogCategory &category_;
77  LogSeverity severity_;
78  utils::time_point timestamp_;
79  std::string fileInfo_;
80 };
81 
82 class Loggable
83 {
84 public:
85  virtual ~Loggable();
86 
87 protected:
88  virtual std::string logPrefix() const = 0;
89 
90  LogMessage _log(const char *file, unsigned int line,
91  LogSeverity severity) const;
92  LogMessage _log(const char *file, unsigned int line,
93  const LogCategory &category,
94  LogSeverity severity) const;
95 };
96 
97 LogMessage _log(const char *file, unsigned int line, LogSeverity severity);
98 LogMessage _log(const char *file, unsigned int line,
99  const LogCategory &category, LogSeverity severity);
100 
101 #ifndef __DOXYGEN__
102 #define _LOG_CATEGORY(name) logCategory##name
103 
104 #define _LOG1(severity) \
105  _log(__FILE__, __LINE__, Log##severity).stream()
106 #define _LOG2(category, severity) \
107  _log(__FILE__, __LINE__, _LOG_CATEGORY(category)(), Log##severity).stream()
108 
109 /*
110  * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of
111  * arguments.
112  */
113 #define _LOG_MACRO(_1, _2, NAME, ...) NAME
114 #define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__)
115 #else /* __DOXYGEN___ */
116 #define LOG(category, severity)
117 #endif /* __DOXYGEN__ */
118 
119 #ifndef NDEBUG
120 #define ASSERT(condition) static_cast<void>(({ \
121  if (!(condition)) \
122  LOG(Fatal) << "assertion \"" #condition "\" failed"; \
123 }))
124 #else
125 #define ASSERT(condition) static_cast<void>(false && (condition))
126 #endif
127 
128 } /* namespace libcamera */
129 
130 #endif /* __LIBCAMERA_INTERNAL_LOG_H__ */
A category of log message.
Definition: log.h:27
const char * name() const
Retrieve the log category name.
Definition: log.h:32
LogSeverity severity() const
Retrieve the severity of the log category.
Definition: log.h:33
static const LogCategory & defaultCategory()
Retrieve the default log category.
Definition: log.cpp:750
LogCategory(const char *name)
Construct a log category.
Definition: log.cpp:707
void setSeverity(LogSeverity severity)
Set the severity of the log category.
Definition: log.cpp:737
Internal log message representation.
Definition: log.h:54
const LogCategory & category() const
Retrieve the category of the log message.
Definition: log.h:68
LogMessage(const char *fileName, unsigned int line, LogSeverity severity)
Construct a log message for the default category.
Definition: log.cpp:776
const std::string msg() const
Retrieve the message text of the log message.
Definition: log.h:70
const std::string & fileInfo() const
Retrieve the file info of the log message.
Definition: log.h:69
const utils::time_point & timestamp() const
Retrieve the timestamp of the log message.
Definition: log.h:66
std::ostream & stream()
Definition: log.h:64
LogSeverity severity() const
Retrieve the severity of the log message.
Definition: log.h:67
Base class to support log message extensions.
Definition: log.h:83
virtual std::string logPrefix() const =0
Retrieve a string to be prefixed to the log message.
LogMessage _log(const char *file, unsigned int line, LogSeverity severity) const
Create a temporary LogMessage object to log a message.
Definition: log.cpp:926
LogSeverity
Definition: log.h:17
@ LogWarning
Definition: log.h:21
@ LogFatal
Definition: log.h:23
@ LogError
Definition: log.h:22
@ LogDebug
Definition: log.h:19
@ LogInfo
Definition: log.h:20
LogMessage _log(const char *file, unsigned int line, LogSeverity severity)
Create a temporary LogMessage object to log a message.
Definition: log.cpp:968
Miscellaneous utility functions.
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition: utils.h:67