libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
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 
13 #include <libcamera/class.h>
14 
16 
17 namespace libcamera {
18 
20  LogInvalid = -1,
21  LogDebug = 0,
26 };
27 
29 {
30 public:
31  explicit LogCategory(const char *name);
32  ~LogCategory();
33 
34  const char *name() const { return name_; }
35  LogSeverity severity() const { return severity_; }
37 
38  static const LogCategory &defaultCategory();
39 
40 private:
41  const char *name_;
42  LogSeverity severity_;
43 };
44 
45 #define LOG_DECLARE_CATEGORY(name) \
46 extern const LogCategory &_LOG_CATEGORY(name)();
47 
48 #define LOG_DEFINE_CATEGORY(name) \
49 const LogCategory &_LOG_CATEGORY(name)() \
50 { \
51  static LogCategory category(#name); \
52  return category; \
53 }
54 
56 {
57 public:
58  LogMessage(const char *fileName, unsigned int line,
59  const LogCategory &category, LogSeverity severity);
60 
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:
74 
75  void init(const char *fileName, unsigned int line);
76 
77  std::ostringstream msgStream_;
78  const LogCategory &category_;
79  LogSeverity severity_;
80  utils::time_point timestamp_;
81  std::string fileInfo_;
82 };
83 
84 class Loggable
85 {
86 public:
87  virtual ~Loggable();
88 
89 protected:
90  virtual std::string logPrefix() const = 0;
91 
93  const char *fileName = __builtin_FILE(),
94  unsigned int line = __builtin_LINE()) const;
95 };
96 
98  const char *fileName = __builtin_FILE(),
99  unsigned int line = __builtin_LINE());
100 
101 #ifndef __DOXYGEN__
102 #define _LOG_CATEGORY(name) logCategory##name
103 
104 #define _LOG1(severity) \
105  _log(nullptr, Log##severity).stream()
106 #define _LOG2(category, severity) \
107  _log(&_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 in " \
123  << __func__ << "()"; \
124 }))
125 #else
126 #define ASSERT(condition) static_cast<void>(false && (condition))
127 #endif
128 
129 } /* namespace libcamera */
130 
131 #endif /* __LIBCAMERA_INTERNAL_LOG_H__ */
Utilities to help constructing class interfaces.
Definition: log.h:22
const char * name() const
Retrieve the log category name.
Definition: log.h:34
Base class to support log message extensions.
Definition: log.h:84
LogSeverity
Definition: log.h:19
Definition: log.h:25
Top-level libcamera namespace.
Definition: bound_method.h:15
Definition: log.h:23
Internal log message representation.
Definition: log.h:55
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition: utils.h:68
Miscellaneous utility functions.
Definition: log.h:21
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:933
LogSeverity severity() const
Retrieve the severity of the log message.
Definition: log.h:67
#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:69
const LogCategory & category() const
Retrieve the category of the log message.
Definition: log.h:68
LogCategory(const char *name)
Construct a log category.
Definition: log.cpp:708
const std::string msg() const
Retrieve the message text of the log message.
Definition: log.h:70
static const LogCategory & defaultCategory()
Retrieve the default log category.
Definition: log.cpp:751
Definition: log.h:24
A category of log message.
Definition: log.h:28
const utils::time_point & timestamp() const
Retrieve the timestamp of the log message.
Definition: log.h:66
std::ostream & stream()
Definition: log.h:64
void setSeverity(LogSeverity severity)
Set the severity of the log category.
Definition: log.cpp:738