Simo 0.0.1
Loading...
Searching...
No Matches
Log.h
1/*
2 * Copyright 2026 Matteo Fusi and Contributors
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 */
16
17#ifndef SIMO_LOG_HH
18#define SIMO_LOG_HH
19#include <Simo/compiler/Compiler.h>
20
21#include <filesystem>
22#include <format>
23#include <fstream>
24#include <ostream>
25#include <vector>
26
27#include "InitializationStatus.h"
28
29namespace Simo::Log {
30
31enum SIMO_PUBLIC LogLevel : std::uint8_t {
32 VERBOSE = 0,
33 DEBUG,
34 INFO,
35 WARNING,
36};
37
38class SIMO_PUBLIC Logger {
39 public:
41 InitializationStatus initialize(const std::filesystem::path& sink_path_v);
42
44 Logger& add_log_level(size_t level, std::string_view name);
45
48
50 Logger& log_level(LogLevel level);
51 Logger& log_level(uint8_t level);
52
54 Logger& log_level(std::string_view level_name);
55
56 [[nodiscard]] size_t log_level() const;
57
59 Logger& enabled(bool new_enabled_value);
60
61 bool enabled() const;
62
63 template <typename Callable>
64 Logger& log_callable(const size_t level, const bool print_level,
65 Callable&& c) {
66 if (!enabled() || level < log_level()) {
67 return *this;
68 }
69 if (print_level) {
70 *sink << '[' << level_map[level] << "] ";
71 }
72 *sink << std::format("{}", c());
73 return *this;
74 }
75
76 template <typename... Args>
77 Logger& log_format(const size_t level, const bool print_level, Args... args) {
78 if (!enabled() || level < log_level()) {
79 return *this;
80 }
81 if (print_level) {
82 *sink << '[' << level_map[level] << "] ";
83 }
84 *sink << std::format(std::forward<Args>(args)...);
85 return *this;
86 }
87
88 ~Logger();
89
91 static void flush_sink(const std::filesystem::path& path);
92
93 // Flush all the registered sinks
94 static void flush_all_sinks();
95
96 protected:
97 std::filesystem::path sink_path;
98 std::ostream* sink = nullptr;
99 bool tracked_sink = false;
100 bool is_enabled = false;
101 size_t current_log_level = 0;
102 std::vector<std::string> level_map;
103};
104
105} // namespace Simo::Log
106
107#endif // SIMO_LOG_HH
Definition InitializationStatus.h:32
Definition Log.h:38
Logger & enabled(bool new_enabled_value)
Enable/disable logger.
Definition Log.cc:94
Logger & add_log_level(size_t level, std::string_view name)
Add a log level and give it a name.
Definition Log.cc:55
InitializationStatus initialize(const std::filesystem::path &sink_path_v)
Initialize the logger.
Definition Log.cc:33
static void flush_sink(const std::filesystem::path &path)
Flush sink at path if this path is identified as a used sink.
Definition Log.cc:114
Logger & populate_default_log_levels()
Setup default log levels and set default log level to the highest.
Definition Log.cc:61
Logger & log_level(LogLevel level)
Setup log level explicitly.
Definition Log.cc:72