Skip to content

Commit 9e744ff

Browse files
Fix logger eventually causing a crash without a console on Windows. (#481)
1 parent 8817129 commit 9e744ff

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

UnleashedRecomp/install/update_checker.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <shellapi.h>
1010
#endif
1111

12+
#include <os/logger.h>
13+
1214
// UpdateChecker
1315

1416
using json = nlohmann::json;
@@ -52,7 +54,7 @@ static bool parseVersion(const std::string &versionStr, int &major, int &minor,
5254
}
5355
catch (const std::exception &e)
5456
{
55-
fmt::println("Error while parsing version: {}.", e.what());
57+
LOGF_ERROR("Error while parsing version: {}.", e.what());
5658
return false;
5759
}
5860

@@ -93,25 +95,25 @@ void updateCheckerThread()
9395
}
9496
else
9597
{
96-
fmt::println("Error while parsing response: tag_name does not contain a valid version string.");
98+
LOG_ERROR("Error while parsing response: tag_name does not contain a valid version string.");
9799
g_updateCheckerResult = UpdateChecker::Result::Failed;
98100
}
99101
}
100102
else
101103
{
102-
fmt::println("Error while parsing response: tag_name not found or not the right type.");
104+
LOG_ERROR("Error while parsing response: tag_name not found or not the right type.");
103105
g_updateCheckerResult = UpdateChecker::Result::Failed;
104106
}
105107
}
106108
catch (const json::exception &e)
107109
{
108-
fmt::println("Error while parsing response: {}", e.what());
110+
LOGF_ERROR("Error while parsing response: {}", e.what());
109111
g_updateCheckerResult = UpdateChecker::Result::Failed;
110112
}
111113
}
112114
else
113115
{
114-
fmt::println("Error while performing request: {}", curl_easy_strerror(res));
116+
LOGF_ERROR("Error while performing request: {}", curl_easy_strerror(res));
115117
g_updateCheckerResult = UpdateChecker::Result::Failed;
116118
}
117119

UnleashedRecomp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ int main(int argc, char *argv[])
153153
timeBeginPeriod(1);
154154
#endif
155155

156+
os::process::CheckConsole();
157+
156158
if (!os::registry::Init())
157159
LOGN_WARNING("OS doesn't support registry");
158160

UnleashedRecomp/os/linux/process_linux.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ bool os::process::StartProcess(const std::filesystem::path& path, const std::vec
6161
return true;
6262
}
6363

64+
void os::process::CheckConsole()
65+
{
66+
// Always visible on Linux.
67+
g_consoleVisible = true;
68+
}
69+
6470
void os::process::ShowConsole()
6571
{
6672
// Unnecessary on Linux.

UnleashedRecomp/os/process.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace os::process
44
{
5+
inline bool g_consoleVisible;
6+
57
std::filesystem::path GetExecutablePath();
68
std::filesystem::path GetWorkingDirectory();
79
bool SetWorkingDirectory(const std::filesystem::path& path);
810
bool StartProcess(const std::filesystem::path& path, const std::vector<std::string>& args, std::filesystem::path work = {});
11+
void CheckConsole();
912
void ShowConsole();
1013
}

UnleashedRecomp/os/win32/logger_win32.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <os/logger.h>
2+
#include <os/process.h>
23

34
#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
45
#define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
56

6-
HANDLE g_hStandardOutput;
7+
static HANDLE g_hStandardOutput;
78

89
void os::logger::Init()
910
{
@@ -12,6 +13,9 @@ void os::logger::Init()
1213

1314
void os::logger::Log(const std::string_view str, ELogType type, const char* func)
1415
{
16+
if (!os::process::g_consoleVisible)
17+
return;
18+
1519
switch (type)
1620
{
1721
case ELogType::Utility:

UnleashedRecomp/os/win32/process_win32.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ bool os::process::StartProcess(const std::filesystem::path& path, const std::vec
5252
return true;
5353
}
5454

55+
void os::process::CheckConsole()
56+
{
57+
g_consoleVisible = (GetConsoleWindow() != nullptr);
58+
}
59+
5560
void os::process::ShowConsole()
5661
{
5762
if (GetConsoleWindow() == nullptr)
@@ -60,5 +65,7 @@ void os::process::ShowConsole()
6065
freopen("CONIN$", "r", stdin);
6166
freopen("CONOUT$", "w", stderr);
6267
freopen("CONOUT$", "w", stdout);
68+
69+
g_consoleVisible = true;
6370
}
6471
}

0 commit comments

Comments
 (0)