Pioneer
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "DateTime.h"
7#include <fmt/format.h>
8#include <fmt/printf.h>
9#include <sigc++/signal.h>
10#include <string_view>
11
12namespace Log {
13 enum class Severity : int8_t {
14 Fatal = -3,
15 Error = -2,
16 Warning = -1,
17 Info = 0,
18 Debug = 1,
19 Verbose = 2
20 };
21
22 struct Logger {
23 ~Logger();
24
25 // Handle formatting, indentation, etc.
26 // Prefer the Verbose, Info, Warning, etc. functions instead of directly using this one
27 void LogLevel(Severity sv, std::string &message);
28 void LogLevel(Severity sv, std::string_view message);
29 void LogLevel(Severity sv, const char *message);
30
31 bool SetLogFile(std::string filename);
32 FILE *GetLogFileHandle() { return file; }
33
34 // Return the severity cutoff at which log messages will be printed to stderr.
35 Severity GetSeverity() { return m_maxSeverity; }
36 void SetSeverity(Severity sv) { m_maxSeverity = sv; }
37
38 Severity GetFileSeverity() { return m_maxFileSeverity; }
39 void SetFileSeverity(Severity sv) { m_maxFileSeverity = sv; }
40
41 Severity GetMsgSeverity() { return m_maxMsgSeverity; }
42 void SetMsgSeverity(Severity sv) { m_maxMsgSeverity = sv; }
43
44 void IncreaseIndent() { current_indent += 1; }
46 {
47 if (current_indent) current_indent -= 1;
48 }
49
50 sigc::signal<void, Time::DateTime, Severity, std::string_view> printCallback;
51
52 private:
53 void WriteLog(Time::DateTime t, Severity sv, std::string_view msg);
54
55 FILE *file;
56 Severity m_maxSeverity = Severity::Info;
57 Severity m_maxFileSeverity = Severity::Debug;
58 Severity m_maxMsgSeverity = Severity::Error;
59 uint8_t current_indent;
60 std::string m_logName;
61 };
62
63 void SetLog(Logger &log);
64 Logger *GetLog();
65
66 inline Severity GetLogLevel() { return GetLog()->GetSeverity(); }
67 inline void SetLogLevel(Severity sv) { GetLog()->SetSeverity(sv); }
68
69 void LogOld(Severity sv, const char *message, fmt::printf_args args);
70 [[noreturn]] void LogFatalOld(const char *message, fmt::printf_args args);
71
72 void LogInternal(Severity sv, const char *message, fmt::format_args args);
73 [[noreturn]] void LogFatalInternal(const char *message, fmt::format_args args);
74
75 void IncreaseIndent();
76 void DecreaseIndent();
77
78 template <typename... Args>
79 inline void Verbose(const char *message, Args... args)
80 {
81 LogInternal(Severity::Verbose, message, fmt::make_format_args(args...));
82 }
83
84 template <typename... Args>
85 inline void Info(const char *message, Args... args)
86 {
87 LogInternal(Severity::Info, message, fmt::make_format_args(args...));
88 }
89
90 template <typename... Args>
91 inline void Debug(const char *message, Args... args)
92 {
93 LogInternal(Severity::Debug, message, fmt::make_format_args(args...));
94 }
95
96 template <typename... Args>
97 inline void Warning(const char *message, Args... args)
98 {
99 LogInternal(Severity::Warning, message, fmt::make_format_args(args...));
100 }
101
102 template <typename... Args>
103 inline void Error(const char *message, Args... args)
104 {
105 LogInternal(Severity::Error, message, fmt::make_format_args(args...));
106 }
107
108 template <typename... Args>
109 [[noreturn]] inline void Fatal(const char *message, Args... args)
110 {
111 LogFatalInternal(message, fmt::make_format_args(args...));
112 }
113
114} // namespace Log
115
116// Compatibility functions for old printf-style formatting
117template <typename... Args>
118inline void Output(const char *message, Args... args)
119{
120 Log::LogOld(Log::Severity::Info, message, fmt::make_printf_args(args...));
121}
122
123template <typename... Args>
124inline void Warning(const char *message, Args... args)
125{
126 Log::LogOld(Log::Severity::Warning, message, fmt::make_printf_args(args...));
127}
128
129template <typename... Args>
130[[noreturn]] inline void Error(const char *message, Args... args)
131{
132 Log::LogFatalOld(message, fmt::make_printf_args(args...));
133}
134
135template <typename... Args>
136inline void DebugMsg(const char *message, Args... args)
137{
138 Log::LogOld(Log::Severity::Debug, message, fmt::make_printf_args(args...));
139}
void Output(const char *message, Args... args)
Definition: Log.h:118
void DebugMsg(const char *message, Args... args)
Definition: Log.h:136
void Warning(const char *message, Args... args)
Definition: Log.h:124
void Error(const char *message, Args... args)
Definition: Log.h:130
Definition: DateTime.h:84
Definition: Log.cpp:18
void LogFatalOld(const char *message, fmt::printf_args args)
Definition: Log.cpp:176
Severity GetLogLevel()
Definition: Log.h:66
void LogOld(Severity sv, const char *message, fmt::printf_args args)
Definition: Log.cpp:167
void LogFatalInternal(const char *message, fmt::format_args args)
Definition: Log.cpp:159
void SetLog(Logger &log)
Definition: Log.cpp:138
Logger * GetLog()
Definition: Log.cpp:133
void DecreaseIndent()
Definition: Log.cpp:148
void IncreaseIndent()
Definition: Log.cpp:143
Severity
Definition: Log.h:13
void SetLogLevel(Severity sv)
Definition: Log.h:67
void LogInternal(Severity sv, const char *message, fmt::format_args args)
Definition: Log.cpp:153
Definition: Log.h:22
void LogLevel(Severity sv, std::string &message)
Definition: Log.cpp:58
Severity GetFileSeverity()
Definition: Log.h:38
bool SetLogFile(std::string filename)
Definition: Log.cpp:38
Severity GetMsgSeverity()
Definition: Log.h:41
~Logger()
Definition: Log.cpp:32
void SetFileSeverity(Severity sv)
Definition: Log.h:39
void IncreaseIndent()
Definition: Log.h:44
void SetSeverity(Severity sv)
Definition: Log.h:36
Severity GetSeverity()
Definition: Log.h:35
void DecreaseIndent()
Definition: Log.h:45
FILE * GetLogFileHandle()
Definition: Log.h:32
void SetMsgSeverity(Severity sv)
Definition: Log.h:42
sigc::signal< void, Time::DateTime, Severity, std::string_view > printCallback
Definition: Log.h:50