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 class CLog : public ISettingsHandler, public ISettingCallback
50 {
51 public:
52  CLog();
53  ~CLog();
54 
55  // implementation of ISettingsHandler
56  void OnSettingsLoaded() override;
57 
58  // implementation of ISettingCallback
59  void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
60 
61  void Initialize(const std::string& path);
62  void UnregisterFromSettings();
63  void Deinitialize();
64 
65  void SetLogLevel(int level);
66  int GetLogLevel() { return m_logLevel; }
67  bool IsLogLevelLogged(int loglevel);
68 
69  bool CanLogComponent(uint32_t component) const;
70  static void SettingOptionsLoggingComponentsFiller(const std::shared_ptr<const CSetting>& setting,
71  std::vector<IntegerSettingOption>& list,
72  int& current,
73  void* data);
74 
75  Logger GetLogger(const std::string& loggerName);
76 
77  template<typename... Args>
78  static inline void Log(int level, const std::string_view& format, Args&&... args)
79  {
80  Log(MapLogLevel(level), format, std::forward<Args>(args)...);
81  }
82 
83  template<typename... Args>
84  static inline void Log(int level,
85  uint32_t component,
86  const std::string_view& format,
87  Args&&... args)
88  {
89  if (!GetInstance().CanLogComponent(component))
90  return;
91 
92  Log(level, format, std::forward<Args>(args)...);
93  }
94 
95  template<typename... Args>
96  static inline void Log(spdlog::level::level_enum level,
97  const std::string_view& format,
98  Args&&... args)
99  {
100  GetInstance().FormatAndLogInternal(level, format, std::forward<Args>(args)...);
101  }
102 
103  template<typename... Args>
104  static inline void Log(spdlog::level::level_enum level,
105  uint32_t component,
106  const std::string_view& format,
107  Args&&... args)
108  {
109  if (!GetInstance().CanLogComponent(component))
110  return;
111 
112  Log(level, format, std::forward<Args>(args)...);
113  }
114 
115 #define LogF(level, format, ...) Log((level), ("{}: " format), __FUNCTION__, ##__VA_ARGS__)
116 #define LogFC(level, component, format, ...) \
117  Log((level), (component), ("{}: " format), __FUNCTION__, ##__VA_ARGS__)
118 
119 private:
120  static CLog& GetInstance();
121 
122  static spdlog::level::level_enum MapLogLevel(int level);
123 
124  template<typename... Args>
125  inline void FormatAndLogInternal(spdlog::level::level_enum level,
126  const std::string_view& format,
127  Args&&... args)
128  {
129  auto message = fmt::format(format, std::forward<Args>(args)...);
130 
131  // fixup newline alignment, number of spaces should equal prefix length
132  FormatLineBreaks(message);
133 
134  m_defaultLogger->log(level, message);
135  }
136 
137  Logger CreateLogger(const std::string& loggerName);
138 
139  void SetComponentLogLevel(const std::vector<CVariant>& components);
140 
141  void FormatLineBreaks(std::string& message);
142 
143  std::unique_ptr<IPlatformLog> m_platform;
144  std::shared_ptr<spdlog::sinks::dist_sink<std::mutex>> m_sinks;
145  Logger m_defaultLogger;
146 
147  std::shared_ptr<spdlog::sinks::sink> m_fileSink;
148 
149  int m_logLevel;
150 
151  bool m_componentLogEnabled;
152  uint32_t m_componentLogLevels;
153 };
Definition: ISettingCallback.h:16
Definition: IPlatformLog.h:21
Definition: log.h:49
Interface defining methods being called by the settings system if an action is performed on multiple/...
Definition: ISettingsHandler.h:16