Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/logging/log.h
ShadPKG / common / logging / log.h
1// SPDX-FileCopyrightText: Copyright 2014 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#pragma once
5 
6#include <algorithm>
7#include <array>
8#include <string_view>
9 
10#include "common/logging/formatter.h"
11#include "common/logging/types.h"
12 
13namespace Common::Log {
14 
15#undef max
16inline const char* TrimSourcePath(std::string_view source) {
17 const auto rfind = [source](const std::string_view match) {
18 return source.rfind(match) == source.npos ? 0 : (source.rfind(match) + match.size());
19 };
20 size_t idx = 0;
21 idx = std::max(rfind("/"), rfind("\\"));
22 return source.data() + idx;
23}
24 
25/// Logs a message to the global logger, using fmt
26void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
27 unsigned int line_num, const char* function, const char* format,
28 const fmt::format_args& args);
29 
30template <typename... Args>
31void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num,
32 const char* function, const char* format, const Args&... args) {
33 FmtLogMessageImpl(log_class, log_level, filename, line_num, function, format,
34 fmt::make_format_args(args...));
35}
36 
37} // namespace Common::Log
38 
39// Define the fmt lib macros
40#define LOG_GENERIC(log_class, log_level, ...) \
41 Common::Log::FmtLogMessage(log_class, log_level, Common::Log::TrimSourcePath(__FILE__), \
42 __LINE__, __func__, __VA_ARGS__)
43 
44#ifdef _DEBUG
45#define LOG_TRACE(log_class, ...) \
46 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Trace, \
47 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
48 __VA_ARGS__)
49#else
50#define LOG_TRACE(log_class, fmt, ...) (void(0))
51#endif
52 
53#define LOG_DEBUG(log_class, ...) \
54 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Debug, \
55 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
56 __VA_ARGS__)
57#define LOG_INFO(log_class, ...) \
58 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Info, \
59 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
60 __VA_ARGS__)
61#define LOG_WARNING(log_class, ...) \
62 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Warning, \
63 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
64 __VA_ARGS__)
65#define LOG_ERROR(log_class, ...) \
66 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Error, \
67 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
68 __VA_ARGS__)
69#define LOG_CRITICAL(log_class, ...) \
70 Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Critical, \
71 Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
72 __VA_ARGS__)
73