numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
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 <string_view>
23#include <utility>
24
25#include "num_collect/impl/num_collect_export.h"
29
31
39class NUM_COLLECT_EXPORT log_sink {
40public:
49 using write_function_type = void (*)(void* /*user_data*/,
50 time_stamp /*time*/, std::string_view /*tag*/, log_level /*level*/,
51 util::source_info_view /*source*/, std::string_view /*body*/) noexcept;
52
58 using finalizer_type = void (*)(void* /*user_data*/) noexcept;
59
67 log_sink(void* user_data, write_function_type write_function,
68 finalizer_type finalizer);
69
75 log_sink(const log_sink& obj) noexcept;
76
82 log_sink(log_sink&& obj) noexcept;
83
90 auto operator=(const log_sink& obj) noexcept -> log_sink&;
91
98 auto operator=(log_sink&& obj) noexcept -> log_sink&;
99
104
116 void write(time_stamp time, std::string_view tag, log_level level,
117 util::source_info_view source, std::string_view body) const noexcept;
118
119private:
121 struct reference_count;
122
125
128
131
133 reference_count* reference_count_;
134};
135
145template <typename T, typename... Args>
146[[nodiscard]] auto create_log_sink(Args&&... args) -> log_sink {
147 T* ptr = new T(std::forward<Args>(args)...);
148 return log_sink(
149 ptr,
150 [](void* ptr, time_stamp time, std::string_view tag, log_level level,
151 util::source_info_view source, std::string_view body) noexcept {
152 static_cast<T*>(ptr)->write(time, tag, level, source, body);
153 },
154 [](void* ptr) noexcept { delete static_cast<T*>(ptr); });
155}
156
157} // namespace num_collect::logging::sinks
log_sink(const log_sink &obj) noexcept
Copy constructor.
reference_count * reference_count_
Reference count.
Definition log_sink.h:133
void(*)(void *, time_stamp, std::string_view, log_level, util::source_info_view, std::string_view) noexcept write_function_type
Type of functions to write logs.
Definition log_sink.h:49
log_sink(log_sink &&obj) noexcept
Move constructor.
void write(time_stamp time, std::string_view tag, log_level level, util::source_info_view source, std::string_view body) const noexcept
Write a log.
auto operator=(log_sink &&obj) noexcept -> log_sink &
Move assignment operator.
auto operator=(const log_sink &obj) noexcept -> log_sink &
Copy assignment operator.
log_sink(void *user_data, write_function_type write_function, finalizer_type finalizer)
Constructor.
write_function_type write_function_
Function to write logs.
Definition log_sink.h:127
finalizer_type finalizer_
Function to finalize the log sink.
Definition log_sink.h:130
void(*)(void *) noexcept finalizer_type
Type of functions to finalize the lgo sink.
Definition log_sink.h:58
Class of time stamps.
Definition time_stamp.h:38
Class to hold information of source codes.
Definition of log_level enumeration.
Namespace of log sinks.
auto create_log_sink(Args &&... args) -> log_sink
Create a log sink.
Definition log_sink.h:146
log_level
Enumeration of log levels.
Definition log_level.h:47
Definition of source_info_view class.
Definition of time_stamp class.