-
Notifications
You must be signed in to change notification settings - Fork 23
/
Logger.cpp
61 lines (50 loc) · 1.78 KB
/
Logger.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
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
#include <lagrange/Logger.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <sstream>
namespace lagrange {
namespace {
// Custom logger instance defined by the user, if any
std::shared_ptr<spdlog::logger> s_logger;
} // namespace
// Retrieve current logger
spdlog::logger& logger()
{
if (s_logger) {
return *s_logger;
} else {
// When using factory methods provided by spdlog (_st and _mt functions),
// names must be unique, since the logger is registered globally.
// Otherwise, you will need to create the logger manually. See
// https://github.com/gabime/spdlog/wiki/2.-Creating-loggers
static auto default_logger = spdlog::stdout_color_mt("lagrange");
return *default_logger;
}
}
// Use a custom logger
void set_logger(std::shared_ptr<spdlog::logger> x)
{
s_logger = std::move(x);
}
ScopedLogLevel::ScopedLogLevel(spdlog::level::level_enum lvl, spdlog::logger& logger_)
{
m_logger = &logger_;
m_prev_level = m_logger->level();
m_logger->set_level(lvl);
}
ScopedLogLevel::~ScopedLogLevel()
{
m_logger->set_level(m_prev_level);
}
} // namespace lagrange