Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/common/log.h
4802 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "log_channels.h"
7
#include "types.h"
8
9
#include "fmt/base.h"
10
11
#include <array>
12
#include <cinttypes>
13
#include <cstdarg>
14
#include <mutex>
15
#include <string_view>
16
17
namespace Log {
18
enum class Level : u32
19
{
20
None, // Silences all log traffic
21
Error,
22
Warning,
23
Info,
24
Verbose,
25
Dev,
26
Debug,
27
Trace,
28
29
MaxCount
30
};
31
32
enum class Color : u32
33
{
34
Default,
35
Black,
36
Red,
37
Green,
38
Blue,
39
Magenta,
40
Orange,
41
Cyan,
42
Yellow,
43
White,
44
StrongBlack,
45
StrongRed,
46
StrongGreen,
47
StrongBlue,
48
StrongMagenta,
49
StrongOrange,
50
StrongCyan,
51
StrongYellow,
52
StrongWhite,
53
54
MaxCount
55
};
56
57
enum class Channel : u32
58
{
59
#define LOG_CHANNEL_ENUM(X) X,
60
ENUMERATE_LOG_CHANNELS(LOG_CHANNEL_ENUM)
61
#undef LOG_CHANNEL_ENUM
62
63
MaxCount
64
};
65
66
// Default log level.
67
static constexpr Log::Level DEFAULT_LOG_LEVEL = Log::Level::Info;
68
69
// Packs a level and channel into one 16-bit number.
70
using MessageCategory = u32;
71
[[maybe_unused]] ALWAYS_INLINE constexpr u32 PackCategory(Channel channel, Level level, Color colour)
72
{
73
return ((static_cast<MessageCategory>(colour) << 10) | (static_cast<MessageCategory>(channel) << 3) |
74
static_cast<MessageCategory>(level));
75
}
76
[[maybe_unused]] ALWAYS_INLINE constexpr Color UnpackColor(MessageCategory cat)
77
{
78
return static_cast<Color>((cat >> 10) & 0x1f);
79
}
80
[[maybe_unused]] ALWAYS_INLINE constexpr Channel UnpackChannel(MessageCategory cat)
81
{
82
return static_cast<Channel>((cat >> 3) & 0x7f);
83
}
84
[[maybe_unused]] ALWAYS_INLINE constexpr Level UnpackLevel(MessageCategory cat)
85
{
86
return static_cast<Level>(cat & 0x7);
87
}
88
89
// log message callback type
90
using CallbackFunctionType = void (*)(void* pUserParam, MessageCategory category, const char* functionName,
91
std::string_view message);
92
93
// registers a log callback
94
void RegisterCallback(CallbackFunctionType callbackFunction, void* pUserParam);
95
96
// unregisters a log callback
97
void UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam);
98
99
// returns a list of all log channels
100
const std::array<const char*, static_cast<size_t>(Channel::MaxCount)>& GetChannelNames();
101
102
// returns the time in seconds since the start of the process
103
float GetCurrentMessageTime();
104
bool AreConsoleOutputTimestampsEnabled();
105
106
// adds a standard console output
107
bool IsConsoleOutputCurrentlyAvailable();
108
bool IsConsoleOutputEnabled();
109
void SetConsoleOutputParams(bool enabled, bool timestamps = true);
110
111
// adds a debug console output [win32/android only]
112
bool IsDebugOutputEnabled();
113
void SetDebugOutputParams(bool enabled);
114
115
// adds a file output
116
void SetFileOutputParams(bool enabled, const char* filename, bool timestamps = true);
117
118
// Returns the current global filtering level.
119
Level GetLogLevel();
120
121
// Returns true if log messages for the specified log level/filter would not be filtered (and visible).
122
bool IsLogVisible(Level level, Channel channel);
123
124
// Sets global filtering level, messages below this level won't be sent to any of the logging sinks.
125
void SetLogLevel(Level level);
126
127
// Sets global filter, any messages from these channels won't be sent to any of the logging sinks.
128
void SetLogChannelEnabled(Channel channel, bool enabled);
129
130
// Returns the name of the specified log channel.
131
const char* GetChannelName(Channel channel);
132
133
// Returns the default colour for a log level.
134
Color GetColorForLevel(Level level);
135
136
// writes a message to the log
137
void Write(MessageCategory cat, std::string_view message);
138
void Write(MessageCategory cat, const char* functionName, std::string_view message);
139
void WriteFmtArgs(MessageCategory cat, fmt::string_view fmt, fmt::format_args args);
140
void WriteFmtArgs(MessageCategory cat, const char* functionName, fmt::string_view fmt, fmt::format_args args);
141
142
ALWAYS_INLINE void FastWrite(Channel channel, Level level, std::string_view message)
143
{
144
if (level <= GetLogLevel()) [[unlikely]]
145
Write(PackCategory(channel, level, Color::Default), message);
146
}
147
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, std::string_view message)
148
{
149
if (level <= GetLogLevel()) [[unlikely]]
150
Write(PackCategory(channel, level, Color::Default), functionName, message);
151
}
152
template<typename... T>
153
ALWAYS_INLINE void FastWrite(Channel channel, Level level, fmt::format_string<T...> fmt, T&&... args)
154
{
155
if (level <= GetLogLevel()) [[unlikely]]
156
WriteFmtArgs(PackCategory(channel, level, Color::Default), fmt, fmt::make_format_args(args...));
157
}
158
template<typename... T>
159
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, fmt::format_string<T...> fmt,
160
T&&... args)
161
{
162
if (level <= GetLogLevel()) [[unlikely]]
163
WriteFmtArgs(PackCategory(channel, level, Color::Default), functionName, fmt, fmt::make_format_args(args...));
164
}
165
ALWAYS_INLINE void FastWrite(Channel channel, Level level, Color colour, std::string_view message)
166
{
167
if (level <= GetLogLevel()) [[unlikely]]
168
Write(PackCategory(channel, level, colour), message);
169
}
170
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, Color colour,
171
std::string_view message)
172
{
173
if (level <= GetLogLevel()) [[unlikely]]
174
Write(PackCategory(channel, level, colour), functionName, message);
175
}
176
template<typename... T>
177
ALWAYS_INLINE void FastWrite(Channel channel, Level level, Color colour, fmt::format_string<T...> fmt, T&&... args)
178
{
179
if (level <= GetLogLevel()) [[unlikely]]
180
WriteFmtArgs(PackCategory(channel, level, colour), fmt, fmt::make_format_args(args...));
181
}
182
template<typename... T>
183
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, Color colour,
184
fmt::format_string<T...> fmt, T&&... args)
185
{
186
if (level <= GetLogLevel()) [[unlikely]]
187
WriteFmtArgs(PackCategory(channel, level, colour), functionName, fmt, fmt::make_format_args(args...));
188
}
189
} // namespace Log
190
191
// log wrappers
192
#define LOG_CHANNEL(name) [[maybe_unused]] static constexpr Log::Channel ___LogChannel___ = Log::Channel::name;
193
194
#define ERROR_LOG(...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Error, __VA_ARGS__)
195
#define WARNING_LOG(...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Warning, __VA_ARGS__)
196
#define INFO_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Info, __VA_ARGS__)
197
#define VERBOSE_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Verbose, __VA_ARGS__)
198
#define DEV_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Dev, __VA_ARGS__)
199
200
#if defined(_DEBUG) || defined(_DEVEL)
201
#define DEBUG_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Debug, __VA_ARGS__)
202
#define TRACE_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Trace, __VA_ARGS__)
203
#else
204
#define DEBUG_LOG(...) \
205
do \
206
{ \
207
} while (0)
208
#define TRACE_LOG(...) \
209
do \
210
{ \
211
} while (0)
212
#endif
213
214
// clang-format off
215
#define ERROR_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Error, Log::Color::colour, __VA_ARGS__)
216
#define WARNING_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Warning, Log::Color::colour, __VA_ARGS__)
217
#define INFO_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Info, Log::Color::colour, __VA_ARGS__)
218
#define VERBOSE_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Verbose, Log::Color::colour, __VA_ARGS__)
219
#define DEV_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Dev, Log::Color::colour, __VA_ARGS__)
220
221
#if defined(_DEBUG) || defined(_DEVEL)
222
#define DEBUG_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Debug, Log::Color::colour, __VA_ARGS__)
223
#define TRACE_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Trace, Log::Color::colour,__VA_ARGS__)
224
#else
225
#define DEBUG_COLOR_LOG(colour, ...) \
226
do \
227
{ \
228
} while (0)
229
#define TRACE_COLOR_LOG(colour, ...) \
230
do \
231
{ \
232
} while (0)
233
#endif
234
235
// clang-format on
236
237