cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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 */
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
34
35namespace msgpack_rpc::logging {
36
42std::shared_ptr<ILogSink> create_stdout_log_sink_impl() {
43 auto spdlog_logger = spdlog::stdout_color_mt("stdout");
45 spdlog_logger);
46 return std::make_shared<impl::spdlog_backend::SpdlogLogSink>(
47 std::move(spdlog_logger));
48}
49
50std::shared_ptr<ILogSink> create_stdout_log_sink() {
51 static const auto log_sink = create_stdout_log_sink_impl();
52 return log_sink;
53}
54
55std::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);
61 spdlog_logger);
62 return std::make_shared<impl::spdlog_backend::SpdlogLogSink>(
63 std::move(spdlog_logger));
64}
65
66std::shared_ptr<ILogSink> create_log_sink_from_config(
68 if (config.file_path().empty()) {
70 }
72 config.file_path(), config.max_file_size(), config.max_files());
73}
74
75} // namespace msgpack_rpc::logging
Class of Logging configuration.
Definition of ILogSink class.
Declaration of functions to create log sinks.
Namespace of configurations.
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.
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.
Namespace of logging.
Definition i_log_sink.h:27
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
std::shared_ptr< ILogSink > create_stdout_log_sink()
Create a log sink to write to standard output.
Definition log_sinks.cpp:50
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
std::shared_ptr< ILogSink > create_stdout_log_sink_impl()
Create a log sink to write to standard output.
Definition log_sinks.cpp:42
Definition of SpdlogLogSink class.