cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
log_sinks.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2023 MusicScience37 (Kenta Kabashima)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
20
#include "
msgpack_rpc/logging/log_sinks.h
"
21
22
#include <cstddef>
23
#include <memory>
24
#include <string>
25
#include <string_view>
26
#include <utility>
27
28
#include <spdlog/common.h>
29
#include <spdlog/sinks/rotating_file_sink.h>
30
#include <spdlog/sinks/stdout_color_sinks.h>
31
32
#include "
msgpack_rpc/logging/i_log_sink.h
"
33
#include "
msgpack_rpc/logging/impl/spdlog_log_sink.h
"
34
35
namespace
msgpack_rpc::logging
{
36
42
std::shared_ptr<ILogSink>
create_stdout_log_sink_impl
() {
43
auto
spdlog_logger = spdlog::stdout_color_mt(
"stdout"
);
44
impl::spdlog_backend::configure_spdlog_logger_format_for_consoles
(
45
spdlog_logger);
46
return
std::make_shared<impl::spdlog_backend::SpdlogLogSink>(
47
std::move(spdlog_logger));
48
}
49
50
std::shared_ptr<ILogSink>
create_stdout_log_sink
() {
51
static
const
auto
log_sink =
create_stdout_log_sink_impl
();
52
return
log_sink;
53
}
54
55
std::shared_ptr<ILogSink>
create_rotating_file_log_sink
(
56
std::string_view file_path, std::size_t max_file_size,
57
std::size_t max_files) {
58
auto
spdlog_logger = spdlog::rotating_logger_mt(std::string(file_path),
59
spdlog::filename_t(file_path), max_file_size, max_files,
true
);
60
impl::spdlog_backend::configure_spdlog_logger_format_for_files
(
61
spdlog_logger);
62
return
std::make_shared<impl::spdlog_backend::SpdlogLogSink>(
63
std::move(spdlog_logger));
64
}
65
66
std::shared_ptr<ILogSink>
create_log_sink_from_config
(
67
const
config::LoggingConfig
&
config
) {
68
if
(
config
.file_path().empty()) {
69
return
create_stdout_log_sink
();
70
}
71
return
create_rotating_file_log_sink
(
72
config
.file_path(),
config
.max_file_size(),
config
.max_files());
73
}
74
75
}
// namespace msgpack_rpc::logging
msgpack_rpc::config::LoggingConfig
Class of Logging configuration.
Definition
logging_config.h:34
i_log_sink.h
Definition of ILogSink class.
log_sinks.h
Declaration of functions to create log sinks.
msgpack_rpc::config
Namespace of configurations.
Definition
client_config.h:32
msgpack_rpc::logging::impl::spdlog_backend::configure_spdlog_logger_format_for_consoles
void configure_spdlog_logger_format_for_consoles(const std::shared_ptr< spdlog::logger > &logger)
Configure the log format of a logger in spdlog library for output to consoles.
Definition
spdlog_log_sink.h:106
msgpack_rpc::logging::impl::spdlog_backend::configure_spdlog_logger_format_for_files
void configure_spdlog_logger_format_for_files(const std::shared_ptr< spdlog::logger > &logger)
Configure the log format of a logger in spdlog library for output to files.
Definition
spdlog_log_sink.h:117
msgpack_rpc::logging
Namespace of logging.
Definition
i_log_sink.h:27
msgpack_rpc::logging::create_rotating_file_log_sink
std::shared_ptr< ILogSink > create_rotating_file_log_sink(std::string_view file_path, std::size_t max_file_size, std::size_t max_files)
Create a log sink to write to a file with rotation.
Definition
log_sinks.cpp:55
msgpack_rpc::logging::create_stdout_log_sink
std::shared_ptr< ILogSink > create_stdout_log_sink()
Create a log sink to write to standard output.
Definition
log_sinks.cpp:50
msgpack_rpc::logging::create_log_sink_from_config
std::shared_ptr< ILogSink > create_log_sink_from_config(const config::LoggingConfig &config)
Create a log sink from a configuration.
Definition
log_sinks.cpp:66
msgpack_rpc::logging::create_stdout_log_sink_impl
std::shared_ptr< ILogSink > create_stdout_log_sink_impl()
Create a log sink to write to standard output.
Definition
log_sinks.cpp:42
spdlog_log_sink.h
Definition of SpdlogLogSink class.
src
msgpack_rpc
logging
log_sinks.cpp
Generated on
for cpp-msgpack-rpc by
1.18.0