1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "log.h"
#include "CompileInfo.h"
#include "ServiceBroker.h"
#include "filesystem/File.h"
#include "guilib/LocalizeStrings.h"
#include "settings/SettingUtils.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/Setting.h"
#include "settings/lib/SettingsManager.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include <cstring>
#include <set>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/dist_sink.h>
#include <spdlog/sinks/dup_filter_sink.h>
namespace
{
static constexpr unsigned char Utf8Bom[3] = {0xEF, 0xBB, 0xBF};
static const std::string LogFileExtension = ".log";
static const std::string LogPattern = "%Y-%m-%d %T.%e T:%-5t %7l <%n>: %v";
} // namespace
CLog::CLog()
: m_platform(IPlatformLog::CreatePlatformLog()),
m_sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()),
m_defaultLogger(CreateLogger("general")),
m_logLevel(LOG_LEVEL_DEBUG),
m_componentLogEnabled(false),
m_componentLogLevels(0)
{
// add platform-specific debug sinks
m_platform->AddSinks(m_sinks);
// register the default logger with spdlog
spdlog::set_default_logger(m_defaultLogger);
// set the formatting pattern globally
spdlog::set_pattern(LogPattern);
// flush on debug logs
spdlog::flush_on(spdlog::level::debug);
// set the log level
SetLogLevel(m_logLevel);
}
CLog::~CLog()
{
spdlog::drop("general");
}
void CLog::OnSettingsLoaded()
{
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
m_componentLogEnabled = settings->GetBool(CSettings::SETTING_DEBUG_EXTRALOGGING);
SetComponentLogLevel(settings->GetList(CSettings::SETTING_DEBUG_SETEXTRALOGLEVEL));
}
void CLog::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
{
if (setting == NULL)
return;
const std::string& settingId = setting->GetId();
if (settingId == CSettings::SETTING_DEBUG_EXTRALOGGING)
m_componentLogEnabled = std::static_pointer_cast<const CSettingBool>(setting)->GetValue();
else if (settingId == CSettings::SETTING_DEBUG_SETEXTRALOGLEVEL)
SetComponentLogLevel(
CSettingUtils::GetList(std::static_pointer_cast<const CSettingList>(setting)));
}
void CLog::Initialize(const std::string& path)
{
if (m_fileSink != nullptr)
return;
// register setting callbacks
auto settingsManager =
CServiceBroker::GetSettingsComponent()->GetSettings()->GetSettingsManager();
settingsManager->RegisterSettingOptionsFiller("loggingcomponents",
SettingOptionsLoggingComponentsFiller);
settingsManager->RegisterSettingsHandler(this);
std::set<std::string> settingSet;
settingSet.insert(CSettings::SETTING_DEBUG_EXTRALOGGING);
settingSet.insert(CSettings::SETTING_DEBUG_SETEXTRALOGLEVEL);
settingsManager->RegisterCallback(this, settingSet);
if (path.empty())
return;
// put together the path to the log file(s)
std::string appName = CCompileInfo::GetAppName();
StringUtils::ToLower(appName);
const std::string filePathBase = URIUtils::AddFileToFolder(path, appName);
const std::string filePath = filePathBase + LogFileExtension;
const std::string oldFilePath = filePathBase + ".old" + LogFileExtension;
// handle old.log by deleting an existing old.log and renaming the last log to old.log
XFILE::CFile::Delete(oldFilePath);
XFILE::CFile::Rename(filePath, oldFilePath);
// write UTF-8 BOM
{
XFILE::CFile file;
if (file.OpenForWrite(filePath, true))
file.Write(Utf8Bom, sizeof(Utf8Bom));
}
// create the file sink within a duplicate filter sink
auto duplicateFilterSink =
std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(10));
auto basicFileSink = std::make_shared<spdlog::sinks::basic_file_sink_st>(
m_platform->GetLogFilename(filePath), false);
basicFileSink->set_pattern(LogPattern);
duplicateFilterSink->add_sink(basicFileSink);
m_fileSink = duplicateFilterSink;
// add it to the existing sinks
m_sinks->add_sink(m_fileSink);
}
void CLog::UnregisterFromSettings()
{
// unregister setting callbacks
auto settingsManager =
CServiceBroker::GetSettingsComponent()->GetSettings()->GetSettingsManager();
settingsManager->UnregisterSettingOptionsFiller("loggingcomponents");
settingsManager->UnregisterSettingsHandler(this);
settingsManager->UnregisterCallback(this);
}
void CLog::Deinitialize()
{
if (m_fileSink == nullptr)
return;
// flush all loggers
spdlog::apply_all([](const std::shared_ptr<spdlog::logger>& logger) { logger->flush(); });
// flush the file sink
m_fileSink->flush();
// remove and destroy the file sink
m_sinks->remove_sink(m_fileSink);
m_fileSink.reset();
}
void CLog::SetLogLevel(int level)
{
if (level < LOG_LEVEL_NONE || level > LOG_LEVEL_MAX)
return;
m_logLevel = level;
auto spdLevel = spdlog::level::info;
if (level <= LOG_LEVEL_NONE)
spdLevel = spdlog::level::off;
else if (level >= LOG_LEVEL_DEBUG)
spdLevel = spdlog::level::trace;
if (m_defaultLogger != nullptr && m_defaultLogger->level() == spdLevel)
return;
spdlog::set_level(spdLevel);
FormatAndLogInternal(spdlog::level::info, "Log level changed to \"{}\"",
spdlog::level::to_string_view(spdLevel));
}
bool CLog::IsLogLevelLogged(int loglevel)
{
if (m_logLevel >= LOG_LEVEL_DEBUG)
return true;
if (m_logLevel <= LOG_LEVEL_NONE)
return false;
return (loglevel & LOGMASK) >= LOGINFO;
}
bool CLog::CanLogComponent(uint32_t component) const
{
if (!m_componentLogEnabled || component == 0)
return false;
return ((m_componentLogLevels & component) == component);
}
void CLog::SettingOptionsLoggingComponentsFiller(const SettingConstPtr& setting,
std::vector<IntegerSettingOption>& list,
int& current,
void* data)
{
list.emplace_back(g_localizeStrings.Get(669), LOGSAMBA);
list.emplace_back(g_localizeStrings.Get(670), LOGCURL);
list.emplace_back(g_localizeStrings.Get(672), LOGFFMPEG);
list.emplace_back(g_localizeStrings.Get(675), LOGJSONRPC);
list.emplace_back(g_localizeStrings.Get(676), LOGAUDIO);
list.emplace_back(g_localizeStrings.Get(680), LOGVIDEO);
list.emplace_back(g_localizeStrings.Get(683), LOGAVTIMING);
list.emplace_back(g_localizeStrings.Get(684), LOGWINDOWING);
list.emplace_back(g_localizeStrings.Get(685), LOGPVR);
list.emplace_back(g_localizeStrings.Get(686), LOGEPG);
list.emplace_back(g_localizeStrings.Get(39117), LOGANNOUNCE);
#ifdef HAS_DBUS
list.emplace_back(g_localizeStrings.Get(674), LOGDBUS);
#endif
#ifdef HAS_WEB_SERVER
list.emplace_back(g_localizeStrings.Get(681), LOGWEBSERVER);
#endif
#ifdef HAS_AIRTUNES
list.emplace_back(g_localizeStrings.Get(677), LOGAIRTUNES);
#endif
#ifdef HAS_UPNP
list.emplace_back(g_localizeStrings.Get(678), LOGUPNP);
#endif
#ifdef HAVE_LIBCEC
list.emplace_back(g_localizeStrings.Get(679), LOGCEC);
#endif
list.emplace_back(g_localizeStrings.Get(682), LOGDATABASE);
#if defined(HAS_FILESYSTEM_SMB)
list.emplace_back(g_localizeStrings.Get(37050), LOGWSDISCOVERY);
#endif
}
Logger CLog::GetLogger(const std::string& loggerName)
{
auto logger = spdlog::get(loggerName);
if (logger == nullptr)
logger = CreateLogger(loggerName);
return logger;
}
CLog& CLog::GetInstance()
{
return CServiceBroker::GetLogging();
}
spdlog::level::level_enum CLog::MapLogLevel(int level)
{
switch (level)
{
case LOGDEBUG:
return spdlog::level::debug;
case LOGINFO:
return spdlog::level::info;
case LOGWARNING:
return spdlog::level::warn;
case LOGERROR:
return spdlog::level::err;
case LOGFATAL:
return spdlog::level::critical;
case LOGNONE:
return spdlog::level::off;
default:
break;
}
return spdlog::level::info;
}
Logger CLog::CreateLogger(const std::string& loggerName)
{
// create the logger
auto logger = std::make_shared<spdlog::logger>(loggerName, m_sinks);
// initialize the logger
spdlog::initialize_logger(logger);
return logger;
}
void CLog::SetComponentLogLevel(const std::vector<CVariant>& components)
{
m_componentLogLevels = 0;
for (const auto& component : components)
{
if (!component.isInteger())
continue;
m_componentLogLevels |= static_cast<uint32_t>(component.asInteger());
}
}
void CLog::FormatLineBreaks(std::string& message)
{
StringUtils::Replace(message, "\n", "\n ");
}
|