cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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
35
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;
59 return level_enum::critical;
60 }
61 return level_enum::trace;
62}
63
67class SpdlogLogSink final : public ILogSink {
68public:
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
95private:
97 std::shared_ptr<spdlog::logger> logger_;
98};
99
107 const std::shared_ptr<spdlog::logger>& logger) {
108 logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%^%l%$] %v (%s:%#, %!)");
109}
110
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
Class of exceptions in cpp-msgpack-rpc library.
Class of locations in source codes.
constexpr std::uint32_t line() const noexcept
Get the line number.
constexpr std::string_view file_path() const noexcept
Get the file path.
constexpr std::string_view function() const noexcept
Get the function name.
std::shared_ptr< spdlog::logger > logger_
Logger in spdlog library.
void write(SourceLocationView location, LogLevel level, std::string_view body) override
Write a log.
SpdlogLogSink(std::shared_ptr< spdlog::logger > logger)
Constructor.
Definition of ILogSink class.
Definition of LogLevel enumeration.
Definition of MsgpackRPCException class.
Namespace of internal implementation of logging using spdlog library.
spdlog::level::level_enum convert_log_level(LogLevel level)
Convert a log level in this library to a log level in spdlog library.
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.
LogLevel
Enumeration of log levels.
Definition log_level.h:29
@ TRACE
Trace. (Internal operations to send and receive messages.)
Definition log_level.h:31
@ WARN
Warnings. (Unexpected conditions which don't stop operations.)
Definition log_level.h:40
@ INFO
Information. (Not used in this library.)
Definition log_level.h:37
@ CRITICAL
Critical. (Unexpected conditions which can stop all operations in communication.)
Definition log_level.h:46
@ ERROR
Error. (Unexpected conditions which stop some operations in communication.)
Definition log_level.h:43
@ DEBUG
Debug. (Log of messages, initialization and finalization of clients and servers.)
Definition log_level.h:34
STL namespace.
Definition of SourceLocationView class.
Definition of StatusCode enumeration.