Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#include "sese/Config.h"
27#include "sese/record/Event.h"
29#include "sese/thread/Thread.h"
30
31#include <memory>
32#include <vector>
33#include <source_location>
34
35#ifdef _WIN32
36#pragma warning(disable : 4251)
37#pragma warning(disable : 4544)
38#endif
39
40namespace sese::record {
41
43class LoggerInitiateTask final : public InitiateTask {
44public:
45 LoggerInitiateTask() : InitiateTask(__FUNCTION__) {}
46
47 int32_t init() noexcept override;
48 int32_t destroy() noexcept override;
49};
50
51class ConsoleAppender;
52
56class Logger {
57public:
58 typedef std::shared_ptr<Logger> Ptr;
59
60 Logger() noexcept;
61
62 virtual ~Logger() noexcept = default;
63
68 void addAppender(const AbstractAppender::Ptr &appender) noexcept;
69
74 void removeAppender(const AbstractAppender::Ptr &appender) noexcept;
75
80 virtual void log(const Event::Ptr &event) noexcept;
81
87 virtual void dump(const void *buffer, size_t length) noexcept;
88
90 std::string_view pattern;
91 std::source_location location;
92
93 PatternAndLocation(const char *pattern, const std::source_location &location = std::source_location::current())
94 : pattern(pattern), location(location) {
95 }
96 };
97
98 template<typename... ARGS>
99 static void debug(PatternAndLocation pattern_and_location, ARGS &&...args);
100
101 static void debug(PatternAndLocation pattern_and_location);
102
103 template<typename... ARGS>
104 static void info(PatternAndLocation pattern_and_location, ARGS &&...args);
105
106 static void info(PatternAndLocation pattern_and_location);
107
108 template<typename... ARGS>
109 static void warn(PatternAndLocation pattern_and_location, ARGS &&...args);
110
111 static void warn(PatternAndLocation pattern_and_location);
112
113 template<typename... ARGS>
114 static void error(PatternAndLocation pattern_and_location, ARGS &&...args);
115
116 static void error(PatternAndLocation pattern_and_location);
117
120 static void addGlobalLoggerAppender(const AbstractAppender::Ptr &appender) noexcept;
121
124 static void removeGlobalLoggerAppender(const AbstractAppender::Ptr &appender) noexcept;
125
126protected:
127 std::shared_ptr<AbstractFormatter> formatter;
128 std::shared_ptr<ConsoleAppender> builtInAppender;
129 std::vector<AbstractAppender::Ptr> appenderVector;
130
131private:
132 static void prelog(PatternAndLocation &pattern_and_location, Level level, const std::string &string) noexcept;
133};
134
139extern Logger *getLogger() noexcept;
140
141template<typename... ARGS>
142void Logger::debug(PatternAndLocation pattern_and_location, ARGS &&...args) {
143 auto message = text::fmt(pattern_and_location.pattern, std::forward<ARGS>(args)...);
144 prelog(pattern_and_location, Level::DEBUG, message);
145}
146
147template<typename... ARGS>
148void Logger::info(PatternAndLocation pattern_and_location, ARGS &&...args) {
149 auto message = text::fmt(pattern_and_location.pattern, std::forward<ARGS>(args)...);
150 prelog(pattern_and_location, Level::INFO, message);
151}
152
153template<typename... ARGS>
154void Logger::warn(PatternAndLocation pattern_and_location, ARGS &&...args) {
155 auto message = text::fmt(pattern_and_location.pattern, std::forward<ARGS>(args)...);
156 prelog(pattern_and_location, Level::WARN, message);
157}
158
159template<typename... ARGS>
160void Logger::error(PatternAndLocation pattern_and_location, ARGS &&...args) {
161 auto message = text::fmt(pattern_and_location.pattern, std::forward<ARGS>(args)...);
162 prelog(pattern_and_location, Level::ERR, message);
163}
164
165} // namespace sese::record