Skip to content

Commit bdc1da3

Browse files
refactor: only alloc console when needed and no longer freopen
1 parent c0a78db commit bdc1da3

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed

src/shell/config.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <mutex>
66
#include <thread>
77

8+
#include "entry.h"
89
#include "logger.h"
910
#include "rfl.hpp"
1011
#include "rfl/DefaultIfMissing.hpp"
@@ -96,9 +97,9 @@ void config::read_config() {
9697
#endif
9798

9899
if (config::current->debug_console) {
99-
ShowWindow(GetConsoleWindow(), SW_SHOW);
100+
init_console(true);
100101
} else {
101-
ShowWindow(GetConsoleWindow(), SW_HIDE);
102+
init_console(false);
102103
}
103104
}
104105

src/shell/entry.cc

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,36 @@
5454

5555
namespace mb_shell {
5656
window_proc_hook entry::main_window_loop_hook{};
57+
58+
static bool console_allocated = false;
59+
60+
void init_console(bool show) {
61+
if (show && !console_allocated) {
62+
AllocConsole();
63+
console_allocated = true;
64+
add_console_sink();
65+
ShowWindow(GetConsoleWindow(), SW_SHOW);
66+
} else if (show && console_allocated) {
67+
ShowWindow(GetConsoleWindow(), SW_SHOW);
68+
SetForegroundWindow(GetConsoleWindow());
69+
} else if (!show && console_allocated) {
70+
remove_console_sink();
71+
FreeConsole();
72+
console_allocated = false;
73+
}
74+
}
75+
5776
void main() {
5877
set_thread_locale_utf8();
59-
AllocConsole();
60-
freopen("CONOUT$", "w", stdout);
61-
freopen("CONOUT$", "w", stderr);
62-
freopen("CONIN$", "r", stdin);
63-
ShowWindow(GetConsoleWindow(), SW_HIDE);
6478

6579
init_logger();
6680
install_error_handlers();
6781
config::run_config_loader();
6882

83+
if (config::current->debug_console) {
84+
init_console(true);
85+
}
86+
6987
static script_context script_ctx;
7088
std::thread([]() {
7189
auto data_dir = config::data_directory();
@@ -82,15 +100,15 @@ void main() {
82100
std::set_terminate([]() {
83101
auto eptr = std::current_exception();
84102
if (eptr) {
103+
init_console(true);
85104
try {
86105
std::rethrow_exception(eptr);
87106
} catch (const std::exception &e) {
88-
std::cerr << "Uncaught exception: " << e.what() << std::endl;
107+
spdlog::critical("Uncaught exception: {}", e.what());
89108
} catch (...) {
90-
std::cerr << "Uncaught exception of unknown type" << std::endl;
109+
spdlog::critical("Uncaught exception of unknown type");
91110
}
92111

93-
ShowWindow(GetConsoleWindow(), SW_SHOW);
94112
std::getchar();
95113
}
96114
std::abort();
@@ -169,8 +187,7 @@ void main() {
169187
taskbar.rt.start_loop();
170188
}
171189
catch(const std::exception &e) {
172-
std::cerr << "Error in taskbar thread: " << e.what()
173-
<< std::endl;
190+
spdlog::error("Error in taskbar thread: {}", e.what());
174191
}
175192
}).detach();
176193
}
@@ -179,7 +196,7 @@ void main() {
179196
if (filename == "asan_test.exe") {
180197
// ASAN environment
181198
init_render_global();
182-
ShowWindow(GetConsoleWindow(), SW_SHOW);
199+
init_console(true);
183200
std::thread([]() {
184201
script_ctx.is_js_ready.wait(false);
185202
spdlog::info( "Is js ready: %d", script_ctx.is_js_ready.load());

src/shell/entry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ namespace mb_shell {
66
struct entry {
77
static window_proc_hook main_window_loop_hook;
88
};
9+
10+
void init_console(bool show);
911
} // namespace mb_shell

src/shell/logger.cc

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
#include <spdlog/sinks/basic_file_sink.h>
77
#include <spdlog/sinks/stdout_color_sinks.h>
88
#include <spdlog/sinks/msvc_sink.h>
9+
#include <memory>
10+
#include <mutex>
911

1012
namespace mb_shell {
13+
static std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> console_sink;
14+
static std::mutex logger_mutex;
15+
1116
void init_logger() {
1217
try {
13-
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
1418
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
1519
(config::data_directory() / "debug.log").string(), true);
1620
auto msvc_sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
1721

18-
std::vector<spdlog::sink_ptr> sinks{console_sink, file_sink, msvc_sink};
22+
std::vector<spdlog::sink_ptr> sinks{file_sink, msvc_sink};
1923

20-
auto logger = std::make_shared<spdlog::logger>("multi_sink", sinks.begin(), sinks.end());
24+
auto logger = std::make_shared<spdlog::logger>("breeze", sinks.begin(), sinks.end());
2125
spdlog::set_default_logger(logger);
2226
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
2327
spdlog::set_level(spdlog::level::debug);
@@ -27,4 +31,27 @@ void init_logger() {
2731
printf("Log initialization failed: %s\n", ex.what());
2832
}
2933
}
34+
35+
void add_console_sink() {
36+
std::lock_guard<std::mutex> lock(logger_mutex);
37+
if (!console_sink) {
38+
console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
39+
auto logger = spdlog::default_logger();
40+
if (logger) {
41+
logger->sinks().push_back(console_sink);
42+
}
43+
}
44+
}
45+
46+
void remove_console_sink() {
47+
std::lock_guard<std::mutex> lock(logger_mutex);
48+
if (console_sink) {
49+
auto logger = spdlog::default_logger();
50+
if (logger) {
51+
auto &sinks = logger->sinks();
52+
sinks.erase(std::remove(sinks.begin(), sinks.end(), console_sink), sinks.end());
53+
}
54+
console_sink.reset();
55+
}
56+
}
3057
} // namespace mb_shell

src/shell/logger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
namespace mb_shell {
66
void init_logger();
7+
void add_console_sink();
8+
void remove_console_sink();
79
}

0 commit comments

Comments
 (0)