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