WinRT - Moved Logging

Decided it was best to have it in winrt since most of debugging would be here
This commit is contained in:
Serenity
2025-06-03 01:26:26 -04:00
parent 222cdf0a22
commit ae4d7ea58c
13 changed files with 185 additions and 7 deletions
-1
View File
@@ -6,7 +6,6 @@
#include <strsafe.h>
#include <string>
#include <vector>
#include "../common/debug.h"
CRITICAL_SECTION CriticalSection;
__int64 qword_18009E948 = 0;
+1
View File
@@ -1,5 +1,6 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
#include "../common/DebugLogger.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+1 -1
View File
@@ -5,7 +5,7 @@
#include <d3d11.h>
#include "device_context_x.h"
#include "device_x.h"
#include "../common/debug.h"
#include "../common/DebugLogger.h"
HRESULT CreateDevice(UINT Flags, wdi::ID3D11Device** ppDevice, wdi::ID3D11DeviceContext** ppImmediateContext)
{
-1
View File
@@ -1,7 +1,6 @@
#include "EtwPlus.h"
#include "pch.h"
#include <stdio.h>
#include "../common/debug.h"
#include "../common/common.h"
VOID DurangoAPI EtxFillCommonFields_v7_X(EVENT_DATA_DESCRIPTOR* eventDataDescriptors, UINT8* buffer, UINT32 bufferSize) {
+1
View File
@@ -1,5 +1,6 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
#include "../common/DebugLogger.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
-1
View File
@@ -1,6 +1,5 @@
#include "pch.h"
#include "../common/common.h"
#include "../common/debug.h"
uint32_t dword_180021AA0[16];
uint32_t dword_180021A60[16];
+1 -1
View File
@@ -6,7 +6,7 @@
#include "CoreWindowWrapperX.h"
#include "ICoreWindowX.h"
#include "../common/DebugLogger.h"
#include "../../Thirdparty/Detours/src/detours.h"
#include <intrin.h>
@@ -24,7 +24,7 @@ namespace winrt::Microsoft::Xbox::Services::UserStatistics::implementation
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Xbox::Services::UserStatistics::UserStatisticsResult>> UserStatisticsService::GetMultipleUserStatisticsAsync(winrt::Windows::Foundation::Collections::IVectorView<hstring> xboxUserIds, hstring serviceConfigurationId, winrt::Windows::Foundation::Collections::IVectorView<hstring> statisticNames)
{
for (uint32_t i = 0; i < statisticNames.Size( ); i++) {
Logger::Warning("[UserStatisticsService] GetMultipleUserStatisticsAsync [Statistic (%u)] [%ls]\n", i, statisticNames.GetAt(i).c_str( ));
printf("[UserStatisticsService] GetMultipleUserStatisticsAsync [Statistic (%u)] [%ls]\n", i, statisticNames.GetAt(i).c_str( ));
}
co_return winrt::single_threaded_vector<UserStatisticsResult>( ).GetView( );
+115
View File
@@ -0,0 +1,115 @@
#include "Logger.h"
#include <ctime>
#include <fstream>
#include <iostream>
#include <mutex>
static std::mutex logMutex;
static std::ofstream logFile("debug.log", std::ios::app);
static const char* ToString(LogLevel level) {
switch (level) {
case LogLevel::Debug: return "DEBUG";
case LogLevel::Info: return "INFO";
case LogLevel::Warning: return "WARNING";
case LogLevel::Error: return "ERROR";
case LogLevel::Fatal: return "FATAL";
case LogLevel::NotImplemented: return "NOT_IMPLEMENTED";
default: return "UNKNOWN";
}
}
static const char* LevelColor(LogLevel level) {
switch (level) {
case LogLevel::Debug: return "\x1B[36m"; // Cyan
case LogLevel::Info: return "\x1B[32m"; // Green
case LogLevel::Warning: return "\x1B[33m"; // Yellow
case LogLevel::Error: return "\x1B[31m"; // Red
case LogLevel::Fatal: return "\x1B[41m"; // Red Background
case LogLevel::NotImplemented: return "\x1B[35m"; // Magenta
default: return "\x1B[0m";
}
}
static std::string CurrentTime( ) {
std::time_t now = std::time(nullptr);
char buffer[ 32 ];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M:%S %p", std::localtime(&now));
return buffer;
}
void Logger::Log(LogLevel level, const std::string& message, const char* file, int line, const char* function) {
std::lock_guard<std::mutex> lock(logMutex);
const char* levelStr = ToString(level);
const char* color = LevelColor(level);
const char* reset = "\x1B[0m";
std::string func = ExtractFunctionName(function);
std::string timeStr = CurrentTime( );
// Console
std::cout << color << timeStr << " - " << func << " - " << levelStr << " - Line " << line;
if (!message.empty( )) std::cout << " - " << message;
std::cout << reset << std::endl;
// File
if (logFile.is_open( )) {
logFile << timeStr << " - " << func << " - " << levelStr << " - Line " << line;
if (!message.empty( )) logFile << " - " << message;
logFile << std::endl;
}
}
// Shortcuts
void Logger::Debug(const char* message) { Log(LogLevel::Debug, message, __FILE__, __LINE__, FUNCTION_NAME); }
void Logger::Info(const char* message) { Log(LogLevel::Info, message, __FILE__, __LINE__, FUNCTION_NAME); }
void Logger::Warning(const char* message) { Log(LogLevel::Warning, message, __FILE__, __LINE__, FUNCTION_NAME); }
void Logger::Error(const char* message) { Log(LogLevel::Error, message, __FILE__, __LINE__, FUNCTION_NAME); }
void Logger::Fatal(const char* message) { Log(LogLevel::Fatal, message, __FILE__, __LINE__, FUNCTION_NAME); }
void Logger::NotImplemented(const char* message) { Log(LogLevel::NotImplemented, message, __FILE__, __LINE__, FUNCTION_NAME); }
// Utility
const char* ExtractProjectName(const char* filePath) {
const char* lastSlash = strrchr(filePath, '/');
if (!lastSlash) lastSlash = strrchr(filePath, '\\');
if (lastSlash) {
const char* secondLastSlash = filePath;
while (secondLastSlash < lastSlash) {
const char* temp = strpbrk(secondLastSlash + 1, "/\\");
if (temp && temp < lastSlash) secondLastSlash = temp;
else break;
}
if (secondLastSlash != filePath) {
static char projectName[ 256 ];
size_t length = lastSlash - secondLastSlash - 1;
length = (length < sizeof(projectName) - 1) ? length : sizeof(projectName) - 1;
#ifdef _MSC_VER
strncpy_s(projectName, sizeof(projectName), secondLastSlash + 1, length);
#else
strncpy(projectName, secondLastSlash + 1, length);
projectName[ length ] = '\0';
#endif
return projectName;
}
}
return "UnknownProject";
}
const char* ExtractFunctionName(const char* fullSignature) {
const char* paren = strchr(fullSignature, '(');
if (paren) {
static char functionName[ 256 ];
size_t length = paren - fullSignature;
length = (length < sizeof(functionName) - 1) ? length : sizeof(functionName) - 1;
#ifdef _MSC_VER
strncpy_s(functionName, sizeof(functionName), fullSignature, length);
#else
strncpy(functionName, fullSignature, length);
functionName[ length ] = '\0';
#endif
return functionName;
}
return fullSignature;
}
+50
View File
@@ -0,0 +1,50 @@
#include "pch.h"
#include <string>
#ifndef DEBUG_LOGGER_H
#define DEBUG_LOGGER_H
enum class LogLevel {
Debug,
Info,
Warning,
Error,
Fatal,
NotImplemented
};
class Logger {
public:
static void Log(LogLevel level, const std::string& message, const char* file, int line, const char* function);
// Logging APIs
static void Debug(const char* message = "");
static void Info(const char* message = "");
static void Warning(const char* message = "");
static void Error(const char* message = "");
static void Fatal(const char* message = "");
static void NotImplemented(const char* message = "");
};
#if defined(__GNUC__) || defined(__clang__)
#define FUNCTION_NAME __PRETTY_FUNCTION__
#elif defined(_MSC_VER)
#define FUNCTION_NAME __FUNCSIG__
#else
#define FUNCTION_NAME __FUNCTION__
#endif
// Debug macros
#ifdef _DEBUG
#define DEBUG_LOG() Logger::Debug()
#define DEBUGLOG(fmt, ...) Logger::Debug(fmt, ##__VA_ARGS__)
#else
#define DEBUG_LOG()
#define DEBUGLOG(fmt, ...)
#endif
const char* ExtractProjectName(const char* filePath);
const char* ExtractFunctionName(const char* fullSignature);
#endif // DEBUG_LOGGER_H
+1 -1
View File
@@ -2,4 +2,4 @@
#include <unknwn.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include "../../common/DebugLogger.h"
#include "Logger.h"
+5
View File
@@ -163,6 +163,7 @@
<ItemGroup>
<ClInclude Include="common.h" />
<ClInclude Include="ConnectedStorage\ConnectedStorage.h" />
<ClInclude Include="Logger.h" />
<ClInclude Include="Implementation\Microsoft.Xbox.GameChat.AccessibilitySettingsChangedEventArgs.h" />
<ClInclude Include="Implementation\Microsoft.Xbox.GameChat.ChannelUpdatedEventArgs.h" />
<ClInclude Include="Implementation\Microsoft.Xbox.GameChat.ChatManager.h" />
@@ -553,6 +554,10 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="ConnectedStorage\ConnectedStorage.cpp" />
<ClCompile Include="Logger.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Implementation\Microsoft.Xbox.GameChat.AccessibilitySettingsChangedEventArgs.cpp" />
<ClCompile Include="Implementation\Microsoft.Xbox.GameChat.ChannelUpdatedEventArgs.cpp" />
<ClCompile Include="Implementation\Microsoft.Xbox.GameChat.ChatManager.cpp" />
+9
View File
@@ -161,6 +161,9 @@
<Filter Include="Implementation\Windows\Xbox\SmartGlass">
<UniqueIdentifier>{a9003d68-6bbb-43e4-ab6e-f1243e316c7c}</UniqueIdentifier>
</Filter>
<Filter Include="Logger">
<UniqueIdentifier>{4b531b63-769a-4159-9dd6-10ef56d7f838}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
@@ -1327,6 +1330,9 @@
<ClCompile Include="Implementation\Windows.Xbox.Multiplayer.MultiplayerSessionReference.cpp">
<Filter>Implementation\Windows\Xbox\Multiplayer</Filter>
</ClCompile>
<ClCompile Include="Logger.cpp">
<Filter>Logger</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Implementation\Microsoft.Xbox.GameChat.TextMessageReceivedEventArgs.h">
@@ -2492,6 +2498,9 @@
<ClInclude Include="Implementation\Windows.Xbox.Multiplayer.MultiplayerSessionReference.h">
<Filter>Implementation\Windows\Xbox\Multiplayer</Filter>
</ClInclude>
<ClInclude Include="Logger.h">
<Filter>Logger</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="winrt_x.def" />