-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.h
87 lines (70 loc) · 2.27 KB
/
logging.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// ReSharper disable CppNonExplicitConvertingConstructor
#pragma once
#include <string>
#include <source_location>
#include <format>
#include <memory>
#include "bakkesmod/wrappers/cvarmanagerwrapper.h"
extern std::shared_ptr<CVarManagerWrapper> _globalCvarManager;
constexpr bool DEBUG_LOG = false;
struct FormatString
{
std::string_view str;
std::source_location loc{};
FormatString(const char* str, const std::source_location& loc = std::source_location::current()) : str(str), loc(loc)
{
}
FormatString(const std::string&& str, const std::source_location& loc = std::source_location::current()) : str(str), loc(loc)
{
}
[[nodiscard]] std::string GetLocation() const
{
return std::format("[{} ({}:{})]", loc.function_name(), loc.file_name(), loc.line());
}
};
struct FormatWstring
{
std::wstring_view str;
std::source_location loc{};
FormatWstring(const wchar_t* str, const std::source_location& loc = std::source_location::current()) : str(str), loc(loc)
{
}
FormatWstring(const std::wstring&& str, const std::source_location& loc = std::source_location::current()) : str(str), loc(loc)
{
}
[[nodiscard]] std::wstring GetLocation() const
{
auto basic_string = std::format("[{} ({}:{})]", loc.function_name(), loc.file_name(), loc.line());
return std::wstring(basic_string.begin(), basic_string.end());
}
};
template <typename... Args>
void LOG(std::string_view format_str, Args&&... args)
{
_globalCvarManager->log(std::vformat(format_str, std::make_format_args(args...)));
}
template <typename... Args>
void LOG(std::wstring_view format_str, Args&&... args)
{
_globalCvarManager->log(std::vformat(format_str, std::make_wformat_args(args...)));
}
template <typename... Args>
void DEBUGLOG(const FormatString& format_str, Args&&... args)
{
if constexpr (DEBUG_LOG)
{
auto text = std::vformat(format_str.str, std::make_format_args(args...));
auto location = format_str.GetLocation();
_globalCvarManager->log(std::format("{} {}", text, location));
}
}
template <typename... Args>
void DEBUGLOG(const FormatWstring& format_str, Args&&... args)
{
if constexpr (DEBUG_LOG)
{
auto text = std::vformat(format_str.str, std::make_wformat_args(args...));
auto location = format_str.GetLocation();
_globalCvarManager->log(std::format(L"{} {}", text, location));
}
}