xbmc
log.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 // spdlog specific defines
12 // clang-format off
13 #include <string_view>
14 #define SPDLOG_LEVEL_NAMES \
15 { \
16  std::string_view{"TRACE"}, \
17  std::string_view{"DEBUG"}, \
18  std::string_view{"INFO"}, \
19  std::string_view{"WARNING"}, \
20  std::string_view{"ERROR"}, \
21  std::string_view{"FATAL"}, \
22  std::string_view{"OFF"} \
23 };
24 // clang-format on
25 
26 #include "commons/ilog.h"
27 #include "settings/lib/ISettingCallback.h"
28 #include "settings/lib/ISettingsHandler.h"
29 #include "settings/lib/SettingDefinitions.h"
30 #include "utils/IPlatformLog.h"
31 #include "utils/logtypes.h"
32 
33 #include <string>
34 #include <vector>
35 
36 #include <spdlog/spdlog.h>
37 
38 namespace spdlog
39 {
40 namespace sinks
41 {
42 class sink;
43 
44 template<typename Mutex>
45 class dist_sink;
46 } // namespace sinks
47 } // namespace spdlog
48 
49 #if FMT_VERSION >= 100000
50 using fmt::enums::format_as;
51 
52 namespace fmt
53 {
54 template<typename T, typename Char>
55 struct formatter<std::atomic<T>, Char> : formatter<T, Char>
56 {
57 };
58 } // namespace fmt
59 #endif
60 
61 class CLog : public ISettingsHandler, public ISettingCallback
62 {
63 public:
64  CLog();
65  ~CLog();
66 
67  // implementation of ISettingsHandler
68  void OnSettingsLoaded() override;
69 
70  // implementation of ISettingCallback
71  void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
72 
73  void Initialize(const std::string& path);
74  void UnregisterFromSettings();
75  void Deinitialize();
76 
77  void SetLogLevel(int level);
78  int GetLogLevel() { return m_logLevel; }
79  bool IsLogLevelLogged(int loglevel);
80 
81  bool CanLogComponent(uint32_t component) const;
82  static void SettingOptionsLoggingComponentsFiller(const std::shared_ptr<const CSetting>& setting,
83  std::vector<IntegerSettingOption>& list,
84  int& current,
85  void* data);
86 
87  Logger GetLogger(const std::string& loggerName);
88 
89  template<typename... Args>
90  static inline void Log(int level, const std::string_view& format, Args&&... args)
91  {
92  Log(MapLogLevel(level), format, std::forward<Args>(args)...);
93  }
94 
95  template<typename... Args>
96  static inline void Log(int level,
97  uint32_t component,
98  const std::string_view& format,
99  Args&&... args)
100  {
101  if (!GetInstance().CanLogComponent(component))
102  return;
103 
104  Log(level, format, std::forward<Args>(args)...);
105  }
106 
107  template<typename... Args>
108  static inline void Log(spdlog::level::level_enum level,
109  const std::string_view& format,
110  Args&&... args)
111  {
112  GetInstance().FormatAndLogInternal(level, format, std::forward<Args>(args)...);
113  }
114 
115  template<typename... Args>
116  static inline void Log(spdlog::level::level_enum level,
117  uint32_t component,
118  const std::string_view& format,
119  Args&&... args)
120  {
121  if (!GetInstance().CanLogComponent(component))
122  return;
123 
124  Log(level, format, std::forward<Args>(args)...);
125  }
126 
127 #define LogF(level, format, ...) Log((level), ("{}: " format), __FUNCTION__, ##__VA_ARGS__)
128 #define LogFC(level, component, format, ...) \
129  Log((level), (component), ("{}: " format), __FUNCTION__, ##__VA_ARGS__)
130 
131 private:
132  static CLog& GetInstance();
133 
134  static spdlog::level::level_enum MapLogLevel(int level);
135 
136  template<typename... Args>
137  inline void FormatAndLogInternal(spdlog::level::level_enum level,
138  const std::string_view& format,
139  Args&&... args)
140  {
141  auto message = fmt::format(format, std::forward<Args>(args)...);
142 
143  // fixup newline alignment, number of spaces should equal prefix length
144  FormatLineBreaks(message);
145 
146  m_defaultLogger->log(level, message);
147  }
148 
149  Logger CreateLogger(const std::string& loggerName);
150 
151  void SetComponentLogLevel(const std::vector<CVariant>& components);
152 
153  void FormatLineBreaks(std::string& message);
154 
155  std::unique_ptr<IPlatformLog> m_platform;
156  std::shared_ptr<spdlog::sinks::dist_sink<std::mutex>> m_sinks;
157  Logger m_defaultLogger;
158 
159  std::shared_ptr<spdlog::sinks::sink> m_fileSink;
160 
161  int m_logLevel;
162 
163  bool m_componentLogEnabled = false;
164  uint32_t m_componentLogLevels = 0;
165 };
Definition: ISettingCallback.h:16
Definition: IPlatformLog.h:21
Definition: log.h:61
Interface defining methods being called by the settings system if an action is performed on multiple/...
Definition: ISettingsHandler.h:16