5454
5555namespace mb_shell {
5656window_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+
5776void 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 ());
0 commit comments