cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
spdlog_log_sink.h
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
#pragma once
21
22
#include <memory>
23
#include <string>
24
#include <string_view>
25
#include <utility>
26
27
#include <spdlog/common.h>
28
#include <spdlog/logger.h>
29
30
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
31
#include "
msgpack_rpc/common/status_code.h
"
32
#include "
msgpack_rpc/logging/i_log_sink.h
"
33
#include "
msgpack_rpc/logging/log_level.h
"
34
#include "
msgpack_rpc/logging/source_location_view.h
"
35
36
namespace
msgpack_rpc::logging::impl::spdlog_backend
{
37
44
[[nodiscard]]
inline
spdlog::level::level_enum
convert_log_level
(
45
LogLevel
level) {
46
using
spdlog::level::level_enum;
47
switch
(level) {
48
case
LogLevel::TRACE
:
49
return
level_enum::trace;
50
case
LogLevel::DEBUG
:
51
return
level_enum::debug;
52
case
LogLevel::INFO
:
53
return
level_enum::info;
54
case
LogLevel::WARN
:
55
return
level_enum::warn;
56
case
LogLevel::ERROR
:
57
return
level_enum::err;
58
case
LogLevel::CRITICAL
:
59
return
level_enum::critical;
60
}
61
return
level_enum::trace;
62
}
63
67
class
SpdlogLogSink
final :
public
ILogSink {
68
public
:
74
explicit
SpdlogLogSink
(std::shared_ptr<spdlog::logger> logger)
75
:
logger_
(
std
::move(logger)) {
76
if
(!
logger_
) {
77
throw
MsgpackRPCException
(StatusCode::INVALID_ARGUMENT,
78
"Null logger was given to SpdlogLogSink class."
);
79
}
80
81
// Log level is checked in Logger class.
82
logger_
->set_level(spdlog::level::trace);
83
}
84
86
void
write
(
SourceLocationView
location,
LogLevel
level,
87
std::string_view body)
override
{
88
logger_
->log(
89
spdlog::source_loc(location.
file_path
().data(),
90
static_cast<
int
>
(location.
line
()), location.
function
().data()),
91
convert_log_level
(level), body);
92
logger_
->flush();
// TODO configuration
93
}
94
95
private
:
97
std::shared_ptr<spdlog::logger>
logger_
;
98
};
99
106
inline
void
configure_spdlog_logger_format_for_consoles
(
107
const
std::shared_ptr<spdlog::logger>& logger) {
108
logger->set_pattern(
"[%Y-%m-%d %H:%M:%S.%f] [%^%l%$] %v (%s:%#, %!)"
);
109
}
110
117
inline
void
configure_spdlog_logger_format_for_files
(
118
const
std::shared_ptr<spdlog::logger>& logger) {
119
logger->set_pattern(
"[%Y-%m-%d %H:%M:%S.%f] [%l] %v (%@, %!)"
);
120
}
121
122
}
// namespace msgpack_rpc::logging::impl::spdlog_backend
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::logging::SourceLocationView
Class of locations in source codes.
Definition
source_location_view.h:30
msgpack_rpc::logging::SourceLocationView::line
constexpr std::uint32_t line() const noexcept
Get the line number.
Definition
source_location_view.h:59
msgpack_rpc::logging::SourceLocationView::file_path
constexpr std::string_view file_path() const noexcept
Get the file path.
Definition
source_location_view.h:50
msgpack_rpc::logging::SourceLocationView::function
constexpr std::string_view function() const noexcept
Get the function name.
Definition
source_location_view.h:68
msgpack_rpc::logging::impl::spdlog_backend::SpdlogLogSink::logger_
std::shared_ptr< spdlog::logger > logger_
Logger in spdlog library.
Definition
spdlog_log_sink.h:97
msgpack_rpc::logging::impl::spdlog_backend::SpdlogLogSink::write
void write(SourceLocationView location, LogLevel level, std::string_view body) override
Write a log.
Definition
spdlog_log_sink.h:86
msgpack_rpc::logging::impl::spdlog_backend::SpdlogLogSink::SpdlogLogSink
SpdlogLogSink(std::shared_ptr< spdlog::logger > logger)
Constructor.
Definition
spdlog_log_sink.h:74
i_log_sink.h
Definition of ILogSink class.
log_level.h
Definition of LogLevel enumeration.
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::logging::impl::spdlog_backend
Namespace of internal implementation of logging using spdlog library.
Definition
spdlog_log_sink.h:36
msgpack_rpc::logging::impl::spdlog_backend::convert_log_level
spdlog::level::level_enum convert_log_level(LogLevel level)
Convert a log level in this library to a log level in spdlog library.
Definition
spdlog_log_sink.h:44
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::LogLevel
LogLevel
Enumeration of log levels.
Definition
log_level.h:29
msgpack_rpc::logging::LogLevel::TRACE
@ TRACE
Trace. (Internal operations to send and receive messages.).
Definition
log_level.h:31
msgpack_rpc::logging::LogLevel::WARN
@ WARN
Warnings. (Unexpected conditions which don't stop operations.).
Definition
log_level.h:40
msgpack_rpc::logging::LogLevel::INFO
@ INFO
Information. (Not used in this library.).
Definition
log_level.h:37
msgpack_rpc::logging::LogLevel::CRITICAL
@ CRITICAL
Critical. (Unexpected conditions which can stop all operations in communication.).
Definition
log_level.h:46
msgpack_rpc::logging::LogLevel::ERROR
@ ERROR
Error. (Unexpected conditions which stop some operations in communication.).
Definition
log_level.h:43
msgpack_rpc::logging::LogLevel::DEBUG
@ DEBUG
Debug. (Log of messages, initialization and finalization of clients and servers.).
Definition
log_level.h:34
std
STL namespace.
source_location_view.h
Definition of SourceLocationView class.
status_code.h
Definition of StatusCode enumeration.
src
msgpack_rpc
logging
impl
spdlog_log_sink.h
Generated on
for cpp-msgpack-rpc by
1.18.0