cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
logger.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
// IWYU pragma: no_include <asio/ip/basic_endpoint.hpp>
23
// IWYU pragma: no_include <asio/local/basic_endpoint.hpp>
24
// IWYU pragma: no_include <asio/ip/detail/impl/endpoint.ipp>
25
// IWYU pragma: no_include <catch2/catch_message.hpp>
26
27
#include <iterator>
28
#include <memory>
29
#include <string_view>
30
#include <utility>
31
32
#include <fmt/base.h>
33
#include <fmt/format.h>
34
35
#include "
msgpack_rpc/config/logging_config.h
"
36
#include "
msgpack_rpc/logging/i_log_sink.h
"
37
#include "
msgpack_rpc/logging/log_level.h
"
38
#include "
msgpack_rpc/logging/log_sinks.h
"
39
#include "
msgpack_rpc/logging/source_location_view.h
"
40
41
namespace
msgpack_rpc::logging
{
42
54
class
Logger
{
55
public
:
62
[[nodiscard]]
static
std::shared_ptr<Logger>
create
(
63
const
config::LoggingConfig
&
config
=
config::LoggingConfig
()) {
64
return
create
(
65
create_log_sink_from_config
(
config
),
config
.output_log_level());
66
}
67
75
[[nodiscard]]
static
std::shared_ptr<Logger>
create
(
76
std::shared_ptr<ILogSink> sink,
77
LogLevel
output_log_level
=
LogLevel::INFO
) {
78
return
std::make_shared<Logger>(std::move(sink),
output_log_level
);
79
}
80
87
Logger
(std::shared_ptr<ILogSink> sink,
LogLevel
output_log_level
)
88
:
sink_
(
std
::move(sink)),
output_log_level_
(
output_log_level
) {}
89
99
template
<
typename
Body>
100
void
write
(
SourceLocationView
location,
LogLevel
level, Body&& body) {
101
if
(
sink_
) {
102
sink_
->write(location, level,
103
// NOLINTNEXTLINE
104
std::string_view(std::forward<Body>(body)));
105
}
106
}
107
117
template
<
typename
... Args>
118
void
write
(
SourceLocationView
location,
LogLevel
level,
119
fmt::format_string<Args...> format, Args&&... args) {
120
if
(
sink_
) {
121
fmt::memory_buffer buffer;
122
fmt::format_to(std::back_inserter(buffer), format,
123
std::forward<Args>(args)...);
124
sink_
->write(location, level,
125
std::string_view(buffer.data(), buffer.size()));
126
}
127
}
128
134
[[nodiscard]]
LogLevel
output_log_level
() const noexcept {
135
return
output_log_level_
;
136
}
137
138
private
:
140
std::shared_ptr<ILogSink>
sink_
;
141
143
LogLevel
output_log_level_
;
144
};
145
146
}
// namespace msgpack_rpc::logging
147
157
#define MSGPACK_RPC_LOG(LOGGER_PTR, LEVEL, ...) \
158
do { \
159
if (LEVEL >= (LOGGER_PTR)->output_log_level()) { \
160
(LOGGER_PTR) \
161
->write(MSGPACK_RPC_CURRENT_SOURCE_LOCATION(), LEVEL, \
162
__VA_ARGS__); \
163
} \
164
} while (false)
165
174
#define MSGPACK_RPC_TRACE(LOGGER_PTR, ...) \
175
MSGPACK_RPC_LOG( \
176
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::TRACE, __VA_ARGS__)
177
186
#define MSGPACK_RPC_DEBUG(LOGGER_PTR, ...) \
187
MSGPACK_RPC_LOG( \
188
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::DEBUG, __VA_ARGS__)
189
198
#define MSGPACK_RPC_INFO(LOGGER_PTR, ...) \
199
MSGPACK_RPC_LOG( \
200
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::INFO, __VA_ARGS__)
201
210
#define MSGPACK_RPC_WARN(LOGGER_PTR, ...) \
211
MSGPACK_RPC_LOG( \
212
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::WARN, __VA_ARGS__)
213
222
#define MSGPACK_RPC_ERROR(LOGGER_PTR, ...) \
223
MSGPACK_RPC_LOG( \
224
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::ERROR, __VA_ARGS__)
225
234
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR, ...) \
235
MSGPACK_RPC_LOG( \
236
LOGGER_PTR, ::msgpack_rpc::logging::LogLevel::CRITICAL, __VA_ARGS__)
msgpack_rpc::config::LoggingConfig
Class of Logging configuration.
Definition
logging_config.h:34
msgpack_rpc::logging::Logger::write
void write(SourceLocationView location, LogLevel level, fmt::format_string< Args... > format, Args &&... args)
Write a log with formatting.
Definition
logger.h:118
msgpack_rpc::logging::Logger::output_log_level_
LogLevel output_log_level_
Log level.
Definition
logger.h:143
msgpack_rpc::logging::Logger::output_log_level
LogLevel output_log_level() const noexcept
Get the log level to write logs.
Definition
logger.h:134
msgpack_rpc::logging::Logger::create
static std::shared_ptr< Logger > create(const config::LoggingConfig &config=config::LoggingConfig())
Create a logger.
Definition
logger.h:62
msgpack_rpc::logging::Logger::create
static std::shared_ptr< Logger > create(std::shared_ptr< ILogSink > sink, LogLevel output_log_level=LogLevel::INFO)
Create a logger.
Definition
logger.h:75
msgpack_rpc::logging::Logger::sink_
std::shared_ptr< ILogSink > sink_
Log sink.
Definition
logger.h:140
msgpack_rpc::logging::Logger::Logger
Logger(std::shared_ptr< ILogSink > sink, LogLevel output_log_level)
Constructor.
Definition
logger.h:87
msgpack_rpc::logging::Logger::write
void write(SourceLocationView location, LogLevel level, Body &&body)
Write a log.
Definition
logger.h:100
msgpack_rpc::logging::SourceLocationView
Class of locations in source codes.
Definition
source_location_view.h:30
i_log_sink.h
Definition of ILogSink class.
log_level.h
Definition of LogLevel enumeration.
log_sinks.h
Declaration of functions to create log sinks.
logging_config.h
Definition of LoggingConfig class.
msgpack_rpc::config
Namespace of configurations.
Definition
client_config.h:32
msgpack_rpc::logging
Namespace of logging.
Definition
i_log_sink.h:27
msgpack_rpc::logging::LogLevel
LogLevel
Enumeration of log levels.
Definition
log_level.h:29
msgpack_rpc::logging::LogLevel::INFO
@ INFO
Information. (Not used in this library.).
Definition
log_level.h:37
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
std
STL namespace.
source_location_view.h
Definition of SourceLocationView class.
include
msgpack_rpc
logging
logger.h
Generated on
for cpp-msgpack-rpc by
1.18.0