Pioneer
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1// Copyright © 2008-2023 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 <sigc++/signal.h>
9#include <string_view>
10
11namespace Log {
12 enum class Severity : int8_t {
13 Fatal = -3,
14 Error = -2,
15 Warning = -1,
16 Info = 0,
17 Debug = 1,
18 Verbose = 2
19 };
20
21 struct Logger {
22 ~Logger();
23
24 // Handle formatting, indentation, etc.
25 // Prefer the Verbose, Info, Warning, etc. functions instead of directly using this one
26 void LogLevel(Severity sv, std::string &message);
27 void LogLevel(Severity sv, std::string_view message);
28 void LogLevel(Severity sv, const char *message);
29
30 bool SetLogFile(std::string filename);
31
32 // Return the severity cutoff at which log messages will be printed to stderr.
33 Severity GetSeverity() { return m_maxSeverity; }
34 void SetSeverity(Severity sv) { m_maxSeverity = sv; }
35
36 Severity GetFileSeverity() { return m_maxFileSeverity; }
37 void SetFileSeverity(Severity sv) { m_maxFileSeverity = sv; }
38
39 Severity GetMsgSeverity() { return m_maxMsgSeverity; }
40 void SetMsgSeverity(Severity sv) { m_maxMsgSeverity = sv; }
41
42 void IncreaseIndent() { current_indent += 1; }
44 {
45 if (current_indent) current_indent -= 1;
46 }
47
48 sigc::signal<void, Time::DateTime, Severity, std::string_view> printCallback;
49
50 private:
51 void WriteLog(Time::DateTime t, Severity sv, std::string_view msg);
52
53 FILE *file;
54 Severity m_maxSeverity = Severity::Info;
55 Severity m_maxFileSeverity = Severity::Debug;
56 Severity m_maxMsgSeverity = Severity::Error;
57 uint8_t current_indent;
58 std::string m_logName;
59 };
60
61 void SetLog(Logger &log);
62 Logger *GetLog();
63
64 inline Severity GetLogLevel() { return GetLog()->GetSeverity(); }
65 inline void SetLogLevel(Severity sv) { GetLog()->SetSeverity(sv); }
66
67 void LogOld(Severity sv, std::string message);
68 [[noreturn]] void LogFatalOld(std::string message);
69
70 void LogInternal(Severity sv, const char *message, fmt::format_args args);
71 [[noreturn]] void LogFatalInternal(const char *message, fmt::format_args args);
72
73 void IncreaseIndent();
74 void DecreaseIndent();
75
76 template <typename... Args>
77 inline void Verbose(const char *message, Args... args)
78 {
79 LogInternal(Severity::Verbose, message, fmt::make_format_args(args...));
80 }
81
82 template <typename... Args>
83 inline void Info(const char *message, Args... args)
84 {
85 LogInternal(Severity::Info, message, fmt::make_format_args(args...));
86 }
87
88 template <typename... Args>
89 inline void Debug(const char *message, Args... args)
90 {
91 LogInternal(Severity::Debug, message, fmt::make_format_args(args...));
92 }
93
94 template <typename... Args>
95 inline void Warning(const char *message, Args... args)
96 {
97 LogInternal(Severity::Warning, message, fmt::make_format_args(args...));
98 }
99
100 template <typename... Args>
101 inline void Error(const char *message, Args... args)
102 {
103 LogInternal(Severity::Error, message, fmt::make_format_args(args...));
104 }
105
106 template <typename... Args>
107 [[noreturn]] inline void Fatal(const char *message, Args... args)
108 {
109 LogFatalInternal(message, fmt::make_format_args(args...));
110 }
111
112} // namespace Log
Definition DateTime.h:84
Definition Log.cpp:18
Severity GetLogLevel()
Definition Log.h:64
void LogOld(Severity sv, std::string message)
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
void LogFatalOld(std::string message)
Definition Log.cpp:175
Severity
Definition Log.h:12
void SetLogLevel(Severity sv)
Definition Log.h:65
void LogInternal(Severity sv, const char *message, fmt::format_args args)
Definition Log.cpp:153
Definition Log.h:21
void LogLevel(Severity sv, std::string &message)
Definition Log.cpp:58
Severity GetFileSeverity()
Definition Log.h:36
bool SetLogFile(std::string filename)
Definition Log.cpp:38
Severity GetMsgSeverity()
Definition Log.h:39
~Logger()
Definition Log.cpp:32
void SetFileSeverity(Severity sv)
Definition Log.h:37
void IncreaseIndent()
Definition Log.h:42
void SetSeverity(Severity sv)
Definition Log.h:34
Severity GetSeverity()
Definition Log.h:33
void DecreaseIndent()
Definition Log.h:43
void SetMsgSeverity(Severity sv)
Definition Log.h:40
sigc::signal< void, Time::DateTime, Severity, std::string_view > printCallback
Definition Log.h:48