| 1 |
/* |
| 2 |
* Logging.h |
| 3 |
* --------- |
| 4 |
* Purpose: General logging |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
#pragma once |
| 11 |
|
| 12 |
#include "openmpt/all/BuildSettings.hpp" |
| 13 |
|
| 14 |
#include "openmpt/logging/Logger.hpp" |
| 15 |
|
| 16 |
#include "mptPathString.h" |
| 17 |
#include "mptString.h" |
| 18 |
|
| 19 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 20 |
#include <atomic> |
| 21 |
#endif |
| 22 |
|
| 23 |
OPENMPT_NAMESPACE_BEGIN |
| 24 |
|
| 25 |
|
| 26 |
/* |
| 27 |
|
| 28 |
|
| 29 |
Build time logging configuration: |
| 30 |
|
| 31 |
* #define MPT_LOG_GLOBAL_LEVEL_STATIC |
| 32 |
#define MPT_LOG_GLOBAL_LEVEL # |
| 33 |
Define the former (to anything) and the latter (to one of the log levels |
| 34 |
below) in order to statically select the verbosity of logging at build time. |
| 35 |
MPT_LOG calls that exceed the specified logging level will get dead-code |
| 36 |
eliminated at compile time. |
| 37 |
This especially means that, when setting MPT_LOG_GLOBAL_LEVEL to 0, no |
| 38 |
MPT_LOG call (with a constant level parameter) remains in the resulting |
| 39 |
binary, however, they still do get parsed and properly type checked by the |
| 40 |
compiler. |
| 41 |
|
| 42 |
|
| 43 |
Logging: |
| 44 |
|
| 45 |
If the context is related to a particular CSoundfile instance, use |
| 46 |
CSoundfile::AddToLog. |
| 47 |
|
| 48 |
Logging a simple message: |
| 49 |
MPT_LOG_GLOBAL(LogWarning, "sounddev", "some message"); |
| 50 |
MPT_LOG_GLOBAL(LogWarning, "sounddev", U_("some message")); |
| 51 |
Facility is some course grained code section identifier (more coarse grained |
| 52 |
than the current file name probably), useful to do some selective logging. |
| 53 |
|
| 54 |
Logging a more complex message: |
| 55 |
MPT_LOG_GLOBAL(LogWarning, "sounddev", MPT_UFORMAT("Some message: foo={}, bar=0x{}")(foo, mpt::ufmt::hex0<8>(bar))); |
| 56 |
|
| 57 |
Note that even with full enabled logging and a runtime configurable logging |
| 58 |
level, the runtime overhead of a MPT_LOG_GLOBAL(level, facility, text) call is just a |
| 59 |
single conditional in case the verbosity does not require logging the respective |
| 60 |
message. Even the expression "text" is not evaluated. |
| 61 |
|
| 62 |
|
| 63 |
*/ |
| 64 |
|
| 65 |
|
| 66 |
inline mpt::ustring LogLevelToString(LogLevel level) |
| 67 |
{ |
| 68 |
switch(level) |
| 69 |
{ |
| 70 |
case LogError: return U_("error"); break; |
| 71 |
case LogWarning: return U_("warning"); break; |
| 72 |
case LogNotification: return U_("notify"); break; |
| 73 |
case LogInformation: return U_("info"); break; |
| 74 |
case LogDebug: return U_("debug"); break; |
| 75 |
} |
| 76 |
return U_("unknown"); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
class ILog |
| 81 |
{ |
| 82 |
protected: |
| 83 |
virtual ~ILog() { } |
| 84 |
public: |
| 85 |
virtual void AddToLog(LogLevel level, const mpt::ustring &text) const = 0; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
namespace mpt |
| 91 |
{ |
| 92 |
namespace log |
| 93 |
{ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#if defined(MPT_LOG_GLOBAL_LEVEL_STATIC) |
| 98 |
#if (MPT_LOG_GLOBAL_LEVEL <= 0) |
| 99 |
// All logging has beeen statically disabled. |
| 100 |
// All logging code gets compiled and immediately dead-code eliminated. |
| 101 |
#define MPT_LOG_IS_DISABLED |
| 102 |
#endif |
| 103 |
inline constexpr int GlobalLogLevel = MPT_LOG_GLOBAL_LEVEL ; |
| 104 |
#else |
| 105 |
extern int GlobalLogLevel; |
| 106 |
#endif |
| 107 |
|
| 108 |
|
| 109 |
#if defined(MODPLUG_TRACKER) && !defined(MPT_LOG_IS_DISABLED) |
| 110 |
extern bool FileEnabled; |
| 111 |
extern bool DebuggerEnabled; |
| 112 |
extern bool ConsoleEnabled; |
| 113 |
void SetFacilities(const std::string &solo, const std::string &blocked); |
| 114 |
bool IsFacilityActive(const char *facility) noexcept; |
| 115 |
#else |
| 116 |
MPT_FORCEINLINE bool IsFacilityActive(const char * /*facility*/ ) noexcept { return true; } |
| 117 |
#endif |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
class GlobalLogger final |
| 122 |
: public ILogger |
| 123 |
{ |
| 124 |
public: |
| 125 |
GlobalLogger() = default; |
| 126 |
~GlobalLogger() final = default; |
| 127 |
public: |
| 128 |
bool IsLevelActive(LogLevel level) const noexcept override |
| 129 |
{ |
| 130 |
return (mpt::log::GlobalLogLevel >= level); |
| 131 |
} |
| 132 |
bool IsFacilityActive(const char *facility) const noexcept override |
| 133 |
{ |
| 134 |
return mpt::log::IsFacilityActive(facility); |
| 135 |
} |
| 136 |
void SendLogMessage(const mpt::source_location &loc, LogLevel level, const char *facility, const mpt::ustring &message) const override; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
#define MPT_LOG_GLOBAL(level, facility, text) MPT_LOG(mpt::log::GlobalLogger{}, (level), (facility), (text)) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 145 |
|
| 146 |
namespace Trace { |
| 147 |
|
| 148 |
// This is not strictly thread safe in all corner cases because of missing barriers. |
| 149 |
// We do not care in order to not harm the fast path with additional barriers. |
| 150 |
// Enabled tracing incurs a runtime overhead with multiple threads as a global atomic variable |
| 151 |
// gets modified. |
| 152 |
// This cacheline bouncing does not matter at all |
| 153 |
// if there are not multiple thread adding trace points at high frequency (way greater than 1000Hz), |
| 154 |
// which, in OpenMPT, is only ever the case for just a single thread (the audio thread), if at all. |
| 155 |
extern std::atomic<bool> g_Enabled; |
| 156 |
inline bool IsEnabled() { return g_Enabled; } |
| 157 |
|
| 158 |
enum class Direction : int8 |
| 159 |
{ |
| 160 |
Unknown = 0, |
| 161 |
Enter = 1, |
| 162 |
Leave = -1, |
| 163 |
}; |
| 164 |
|
| 165 |
MPT_NOINLINE void Trace(const mpt::source_location & loc, Direction direction = Direction::Unknown) noexcept; |
| 166 |
|
| 167 |
enum ThreadKind { |
| 168 |
ThreadKindGUI, |
| 169 |
ThreadKindAudio, |
| 170 |
ThreadKindNotify, |
| 171 |
ThreadKindWatchdir, |
| 172 |
}; |
| 173 |
|
| 174 |
void Enable(std::size_t numEntries); |
| 175 |
void Disable(); |
| 176 |
|
| 177 |
void SetThreadId(mpt::log::Trace::ThreadKind kind, uint32 id); |
| 178 |
uint32 GetThreadId(mpt::log::Trace::ThreadKind kind); |
| 179 |
|
| 180 |
void Seal(); |
| 181 |
bool Dump(const mpt::PathString &filename); |
| 182 |
|
| 183 |
class Scope |
| 184 |
{ |
| 185 |
private: |
| 186 |
const mpt::source_location loc; |
| 187 |
public: |
| 188 |
MPT_FORCEINLINE Scope(mpt::source_location loc) noexcept |
| 189 |
: loc(loc) |
| 190 |
{ |
| 191 |
if(mpt::log::Trace::g_Enabled) |
| 192 |
{ |
| 193 |
mpt::log::Trace::Trace(loc, mpt::log::Trace::Direction::Enter); |
| 194 |
} |
| 195 |
} |
| 196 |
MPT_FORCEINLINE ~Scope() noexcept |
| 197 |
{ |
| 198 |
if(mpt::log::Trace::g_Enabled) |
| 199 |
{ |
| 200 |
mpt::log::Trace::Trace(loc, mpt::log::Trace::Direction::Leave); |
| 201 |
} |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
#define MPT_TRACE_CONCAT_HELPER(x, y) x ## y |
| 206 |
#define MPT_TRACE_CONCAT(x, y) MPT_TRACE_CONCAT_HELPER(x, y) |
| 207 |
|
| 208 |
#define MPT_TRACE_SCOPE() mpt::log::Trace::Scope MPT_TRACE_CONCAT(MPT_TRACE_VAR, __LINE__)(MPT_SOURCE_LOCATION_CURRENT()) |
| 209 |
|
| 210 |
#define MPT_TRACE() do { if(mpt::log::Trace::g_Enabled) { mpt::log::Trace::Trace(MPT_SOURCE_LOCATION_CURRENT()); } } while(0) |
| 211 |
|
| 212 |
} // namespace Trace |
| 213 |
|
| 214 |
#else // !MODPLUG_TRACKER |
| 215 |
|
| 216 |
#define MPT_TRACE_SCOPE() do { } while(0) |
| 217 |
|
| 218 |
#define MPT_TRACE() do { } while(0) |
| 219 |
|
| 220 |
#endif // MODPLUG_TRACKER |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
} // namespace log |
| 225 |
} // namespace mpt |
| 226 |
|
| 227 |
|
| 228 |
OPENMPT_NAMESPACE_END |