pwn++  0.1.4
A (toy) Windows & Linux pwn library to play with modern C++.
Log.hpp
Go to the documentation of this file.
1 #pragma once
10 
11 #include <format>
12 #include <source_location>
13 #include <sstream>
14 #include <string_view>
15 
16 #include "Common.hpp"
17 
18 // clang-format off
19 #ifdef PWN_LOG_USE_COLOR
20 #define PWN_COLOR_RESET "\033[0m "
21 #define PWN_COLOR_BOLD "\033[1m "
22 #define PWN_COLOR_UNDERLINE "\033[4m "
23 #define PWN_COLOR_FG_BLACK "\033[30m "
24 #define PWN_COLOR_FG_RED "\033[31m "
25 #define PWN_COLOR_FG_GREEN "\033[32m "
26 #define PWN_COLOR_FG_YELLOW "\033[33m "
27 #define PWN_COLOR_FG_BLUE "\033[34m "
28 #define PWN_COLOR_FG_MAGENTA "\033[35m "
29 #define PWN_COLOR_FG_CYAN "\033[36m "
30 #define PWN_COLOR_FG_WHITE "\033[37m "
31 #else
32 #define PWN_COLOR_RESET ""
33 #define PWN_COLOR_BOLD ""
34 #define PWN_COLOR_UNDERLINE ""
35 #define PWN_COLOR_FG_BLACK ""
36 #define PWN_COLOR_FG_RED ""
37 #define PWN_COLOR_FG_GREEN ""
38 #define PWN_COLOR_FG_YELLOW ""
39 #define PWN_COLOR_FG_BLUE ""
40 #define PWN_COLOR_FG_MAGENTA ""
41 #define PWN_COLOR_FG_CYAN ""
42 #define PWN_COLOR_FG_WHITE ""
43 #endif
44 
45 #define PWN_LOG_STRINGS_DEBUG "[ DEBUG ] "
46 #define PWN_LOG_STRINGS_INFO "[ INFO ] "
47 #define PWN_LOG_STRINGS_SUCCESS "[ SUCCESS ] "
48 #define PWN_LOG_STRINGS_WARN "[ WARN ] "
49 #define PWN_LOG_STRINGS_ERROR "[ ERROR ] "
50 #define PWN_LOG_STRINGS_CRITICAL "[ CRITICAL ] "
51 // clang-format on
52 
53 
54 namespace pwn::Log
55 {
59 enum class LogLevel : u8
60 {
62  Debug,
63 
65  Info,
66 
68  Success,
69 
71  Warning,
72 
74  Error,
75 
77  Critical,
78 };
79 
80 
88 void
89 Log(const LogLevel level, std::source_location const& location, std::ostringstream& msg);
90 
91 
101 template<typename... Args>
102 void
103 Log(const LogLevel level, std::source_location const& location, std::string_view const& fmt, Args&&... args)
104 {
105  std::ostringstream msg;
106  msg << std::vformat(fmt, std::make_format_args(args...)) << '\n';
107  Log(level, location, msg);
108 }
109 
110 
118 void
119 Log(const LogLevel level, std::source_location const& location, std::wostringstream& msg);
120 
121 
131 template<typename... Args>
132 void
133 Log(const LogLevel level, std::source_location const& location, std::wstring_view const& fmt, Args&&... args)
134 {
135  std::wostringstream msg;
136  msg << std::vformat(fmt, std::make_wformat_args(args...)) << std::endl;
137  Log::Log(level, location, msg);
138 }
139 
140 
141 #ifdef PWN_BUILD_FOR_WINDOWS
149 template<typename T = std::wstring>
150 auto
151 FormatLastError(const u32 gle) -> T
152 {
153  if constexpr ( std::is_same_v<T, std::wstring> )
154  {
155  wchar_t msg[1024] {0};
156  ::FormatMessageW(
157  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
158  nullptr,
159  gle,
160  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
161  msg,
162  __countof(msg),
163  nullptr);
164  return std::wstring(msg);
165  }
166 
167  if constexpr ( std::is_same_v<T, std::string> )
168  {
169  char msg[1024] {0};
170  ::FormatMessageA(
171  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
172  nullptr,
173  gle,
174  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
175  msg,
176  __countof(msg),
177  nullptr);
178  return std::string(msg);
179  }
180 
181  throw std::bad_variant_access();
182 }
183 
189 void PWNAPI
190 perror(const std::wstring_view& prefix);
191 
192 
198 void PWNAPI
199 perror(const std::string_view& prefix);
200 
201 
208 void PWNAPI
209 ntperror(const std::wstring_view& prefix, NTSTATUS Status);
210 
211 
218 void PWNAPI
219 ntperror(const std::string_view& prefix, NTSTATUS Status);
220 #endif
221 
222 } // namespace pwn::Log
223 
224 
228 // clang-format off
229 #define dbg(...) Log::Log(Log::LogLevel::Debug, std::source_location::current(), ##__VA_ARGS__)
230 #define info(...) Log::Log(Log::LogLevel::Info, std::source_location::current(), ##__VA_ARGS__)
231 #define ok(...) Log::Log(Log::LogLevel::Success, std::source_location::current(), ##__VA_ARGS__)
232 #define warn(...) Log::Log(Log::LogLevel::Warning, std::source_location::current(), ##__VA_ARGS__)
233 #define err(...) Log::Log(Log::LogLevel::Error, std::source_location::current(), ##__VA_ARGS__)
234 // clang-format on
void Log(const LogLevel level, std::source_location const &location, std::ostringstream &msg)
Print an output string stream as a log message.
LogLevel
Define the logging level.
Definition: Log.hpp:60
@ Warning
Warning logging level
@ Critical
Critical logging level
@ Info
Info logging level
@ Debug
Debug logging level