Skip to content

Commit

Permalink
Add basic logging functionality
Browse files Browse the repository at this point in the history
There's more that will need to be done, like actually calling log_init
somewhere, or actually logging stuff.
  • Loading branch information
32th-System committed Feb 27, 2024
1 parent 42508d0 commit 0349bd1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
74 changes: 74 additions & 0 deletions thprac/src/thprac/thprac_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <Windows.h>
#include <Shlwapi.h>
#include "thprac_log.h"
#include "thprac_launcher_cfg.h"

namespace THPrac {

HANDLE hLog = INVALID_HANDLE_VALUE;
bool console_open = false;

void log_print(const char* msg, size_t len)
{
DWORD byteRet;
if (console_open) {
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, len, &byteRet, nullptr);
}
if (hLog != INVALID_HANDLE_VALUE) {
WriteFile(hLog, msg, len, &byteRet, nullptr);
}
}

void log_init(bool launcher, bool console)
{
// The lengths I'll go to not allocate on the heap

constexpr unsigned int rot_max = 9;
constexpr unsigned int scratch_size = 32;
wchar_t fn_rot_temp_1[scratch_size] = {};
wchar_t fn_rot_temp_2[scratch_size] = {};

const wchar_t* const fn_launcher = L"thprac_launcher_log.txt";
const wchar_t* const fn_ingame = L"thprac_log.txt";

const wchar_t* const fn_rot_launcher = L"thprac_launcher_log.9.txt";
const wchar_t* const fn_rot_ingame = L"thprac_log.9.txt";

if (launcher) {
memcpy(fn_rot_temp_1, fn_rot_launcher, wcslen(fn_rot_launcher) * sizeof(wchar_t));
} else {
memcpy(fn_rot_temp_1, fn_rot_ingame, wcslen(fn_rot_ingame) * sizeof(wchar_t));
}

const unsigned int rot_num_off = launcher ? 20 : 11;
const wchar_t* const fn = launcher ? fn_launcher : fn_ingame;

memcpy(fn_rot_temp_2, fn_rot_temp_1, scratch_size * sizeof(wchar_t));

DeleteFileW(fn_rot_temp_1);

for (size_t i = 0; i < rot_max - 1; i++) {
fn_rot_temp_2[rot_num_off]--;
MoveFileW(fn_rot_temp_2, fn_rot_temp_1);
fn_rot_temp_1[rot_num_off]--;
}

MoveFileW(fn, fn_rot_temp_1);
hLog = CreateFileW(fn, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

// Attach to existing console if it exists, open new console if no console exists but console is set to true
// I wonder if this is gonna make command prompt appear for a millisecond for some people
BOOL console_success = AllocConsole();

if (console_success && !console) {
FreeConsole();
} else if (!console_success) {
AttachConsole(GetCurrentProcessId());
console_open = true;
} else {
console_open = true;
}

log_print("THPrac: Logging initialized\r\n");
}
}
22 changes: 22 additions & 0 deletions thprac/src/thprac/thprac_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <cstring>
#include <format>


namespace THPrac {

void log_print(const char* const msg, size_t len);

inline void log_print(const char* const null_terminated) {
return log_print(null_terminated, strlen(null_terminated));
}

template <typename... Args>
inline void log_printf(const std::format_string<Args...> fmt, Args&&... args) {
auto str = std::vformat(fmt.get(), std::make_format_args(args...));
return log_print(str.data(), str.length());
}

void log_init(bool launcher, bool console);

}
2 changes: 2 additions & 0 deletions thprac/thprac.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<ClInclude Include="src\thprac\thprac_version.h" />
<ClInclude Include="src\thprac\utils\utils.h" />
<ClInclude Include="src\thprac\utils\wininternal.h" />
<ClInclude Include="src\thprac\thprac_log.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\3rdParties\distorm\src\decoder.c" />
Expand Down Expand Up @@ -142,6 +143,7 @@
<ClCompile Include="src\thprac\thprac_th19.cpp" />
<ClCompile Include="src\thprac\thprac_utils.cpp" />
<ClCompile Include="src\thprac\thprac_version.cpp" />
<ClCompile Include="src\thprac\thprac_log.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
9 changes: 9 additions & 0 deletions thprac/thprac.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<Filter Include="3rdParties\distorm">
<UniqueIdentifier>{653ef50e-68b6-4a06-8e84-3ddf50867dfe}</UniqueIdentifier>
</Filter>
<Filter Include="THPrac Logging">
<UniqueIdentifier>{89b3b162-0590-4673-a372-b4699cc95cb7}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
Expand Down Expand Up @@ -205,6 +208,9 @@
<ClInclude Include="src\thprac\thprac_th19.h">
<Filter>THPrac Games</Filter>
</ClInclude>
<ClInclude Include="src\thprac\thprac_log.h">
<Filter>THPrac Logging</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\thprac\thprac_utils.cpp">
Expand Down Expand Up @@ -405,6 +411,9 @@
<ClCompile Include="src\thprac\thprac_th19.cpp">
<Filter>THPrac Games</Filter>
</ClCompile>
<ClCompile Include="src\thprac\thprac_log.cpp">
<Filter>THPrac Logging</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config">
Expand Down

0 comments on commit 0349bd1

Please sign in to comment.