forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.cpp
118 lines (101 loc) · 3.09 KB
/
global.cpp
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
/**
* Copyright (c) 2020-2021 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "plog/Appenders/ColorConsoleAppender.h"
#include "plog/Converters/UTF8Converter.h"
#include "plog/Formatters/FuncMessageFormatter.h"
#include "plog/Formatters/TxtFormatter.h"
#include "plog/Init.h"
#include "plog/Log.h"
#include "plog/Logger.h"
//
#include "global.hpp"
#include "impl/init.hpp"
#include <mutex>
namespace {
void plogInit(plog::Severity severity, plog::IAppender *appender) {
using Logger = plog::Logger<PLOG_DEFAULT_INSTANCE_ID>;
static Logger *logger = nullptr;
if (!logger) {
PLOG_DEBUG << "Initializing logger";
logger = new Logger(severity);
if (appender) {
logger->addAppender(appender);
} else {
using ConsoleAppender = plog::ColorConsoleAppender<plog::TxtFormatter>;
static ConsoleAppender *consoleAppender = new ConsoleAppender();
logger->addAppender(consoleAppender);
}
} else {
logger->setMaxSeverity(severity);
if (appender)
logger->addAppender(appender);
}
}
} // namespace
namespace rtc {
struct LogAppender : public plog::IAppender {
synchronized_callback<LogLevel, string> callback;
void write(const plog::Record &record) override {
const auto severity = record.getSeverity();
auto formatted = plog::FuncMessageFormatter::format(record);
formatted.pop_back(); // remove newline
const auto &converted =
plog::UTF8Converter::convert(formatted); // does nothing on non-Windows systems
if (!callback(static_cast<LogLevel>(severity), converted))
std::cout << plog::severityToString(severity) << " " << converted << std::endl;
}
};
void InitLogger(LogLevel level, LogCallback callback) {
const auto severity = static_cast<plog::Severity>(level);
static LogAppender *appender = nullptr;
static std::mutex mutex;
std::lock_guard lock(mutex);
if (appender) {
appender->callback = std::move(callback);
plogInit(severity, nullptr); // change the severity
} else if (callback) {
appender = new LogAppender();
appender->callback = std::move(callback);
plogInit(severity, appender);
} else {
plogInit(severity, nullptr); // log to cout
}
}
void InitLogger(plog::Severity severity, plog::IAppender *appender) {
plogInit(severity, appender);
}
void Preload() { impl::Init::Instance().preload(); }
std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
} // namespace rtc
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::LogLevel level) {
switch (level) {
case rtc::LogLevel::Fatal:
out << "fatal";
break;
case rtc::LogLevel::Error:
out << "error";
break;
case rtc::LogLevel::Warning:
out << "warning";
break;
case rtc::LogLevel::Info:
out << "info";
break;
case rtc::LogLevel::Debug:
out << "debug";
break;
case rtc::LogLevel::Verbose:
out << "verbose";
break;
default:
out << "none";
break;
}
return out;
}