cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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
40
41namespace msgpack_rpc::logging {
42
54class Logger {
55public:
62 [[nodiscard]] static std::shared_ptr<Logger> create(
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,
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)
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
138private:
140 std::shared_ptr<ILogSink> sink_;
141
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__)
Class of Logging configuration.
void write(SourceLocationView location, LogLevel level, fmt::format_string< Args... > format, Args &&... args)
Write a log with formatting.
Definition logger.h:118
LogLevel output_log_level_
Log level.
Definition logger.h:143
LogLevel output_log_level() const noexcept
Get the log level to write logs.
Definition logger.h:134
static std::shared_ptr< Logger > create(const config::LoggingConfig &config=config::LoggingConfig())
Create a logger.
Definition logger.h:62
static std::shared_ptr< Logger > create(std::shared_ptr< ILogSink > sink, LogLevel output_log_level=LogLevel::INFO)
Create a logger.
Definition logger.h:75
std::shared_ptr< ILogSink > sink_
Log sink.
Definition logger.h:140
Logger(std::shared_ptr< ILogSink > sink, LogLevel output_log_level)
Constructor.
Definition logger.h:87
void write(SourceLocationView location, LogLevel level, Body &&body)
Write a log.
Definition logger.h:100
Class of locations in source codes.
Definition of ILogSink class.
Definition of LogLevel enumeration.
Declaration of functions to create log sinks.
Definition of LoggingConfig class.
Namespace of configurations.
Namespace of logging.
Definition i_log_sink.h:27
LogLevel
Enumeration of log levels.
Definition log_level.h:29
@ INFO
Information. (Not used in this library.)
Definition log_level.h:37
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
STL namespace.
Definition of SourceLocationView class.