From 5062920b1d7aee4e3ac99d51d123f301ba4e47b9 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Tue, 18 Jun 2024 00:03:34 +0100 Subject: [PATCH] Allow disabling logging entirely (#18). --- src/common.h | 6 ++++-- src/directplay.c | 4 ++-- src/ipxconfig.cpp | 50 +++++++++++++++++++++++++++++++---------------- src/ipxwrapper.c | 12 ++++++------ src/log.c | 19 +++++++++++++----- src/stubdll.c | 4 ++-- 6 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/common.h b/src/common.h index 69e88bd..78d56d7 100644 --- a/src/common.h +++ b/src/common.h @@ -1,5 +1,5 @@ /* IPXWrapper - Common header - * Copyright (C) 2011-2021 Daniel Collins + * Copyright (C) 2011-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -35,7 +35,8 @@ enum ipx_log_level { LOG_DEBUG, LOG_INFO = 4, LOG_WARNING, - LOG_ERROR + LOG_ERROR, + LOG_DISABLED = 7, }; extern enum ipx_log_level min_log_level; @@ -74,6 +75,7 @@ void unload_dlls(void); void __stdcall *find_sym(unsigned int dllnum, const char *symbol); void __stdcall log_call(unsigned int entry, const char *symbol, unsigned int target); +void log_init(); void log_open(const char *file); void log_close(); void log_printf(enum ipx_log_level level, const char *fmt, ...); diff --git a/src/directplay.c b/src/directplay.c index 3cf2033..6c423b8 100644 --- a/src/directplay.c +++ b/src/directplay.c @@ -1,5 +1,5 @@ /* ipxwrapper - DirectPlay service provider - * Copyright (C) 2011-2019 Daniel Collins + * Copyright (C) 2011-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -727,7 +727,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { { fprof_init(stub_fstats, NUM_STUBS); - log_open("ipxwrapper.log"); + log_init(); min_log_level = get_main_config().log_level; } diff --git a/src/ipxconfig.cpp b/src/ipxconfig.cpp index a20abf9..1c2d44d 100644 --- a/src/ipxconfig.cpp +++ b/src/ipxconfig.cpp @@ -1,5 +1,5 @@ /* IPXWrapper - Configuration tool - * Copyright (C) 2011-2023 Daniel Collins + * Copyright (C) 2011-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -39,6 +39,7 @@ enum { ID_NIC_NODE = 14, ID_OPT_W95 = 22, + ID_OPT_LOG_ENABLE = 24, ID_OPT_LOG_DEBUG = 25, ID_OPT_LOG_TRACE = 26, ID_OPT_PROFILE = 27, @@ -143,6 +144,7 @@ static struct { HWND box_options; HWND opt_w95; + HWND opt_log_enable; HWND opt_log_debug; HWND opt_log_trace; HWND opt_profile; @@ -224,6 +226,7 @@ static LRESULT CALLBACK main_wproc(HWND window, UINT msg, WPARAM wp, LPARAM lp) break; } + case ID_OPT_LOG_ENABLE: case ID_OPT_LOG_DEBUG: { main_window_update(); break; @@ -596,15 +599,20 @@ static bool save_config() } main_config.w95_bug = get_checkbox(wh.opt_w95); - main_config.log_level = LOG_INFO; + main_config.log_level = LOG_DISABLED; - if(get_checkbox(wh.opt_log_debug)) + if(get_checkbox(wh.opt_log_enable)) { - main_config.log_level = LOG_DEBUG; + main_config.log_level = LOG_INFO; - if(get_checkbox(wh.opt_log_trace)) + if(get_checkbox(wh.opt_log_debug)) { - main_config.log_level = LOG_CALL; + main_config.log_level = LOG_DEBUG; + + if(get_checkbox(wh.opt_log_trace)) + { + main_config.log_level = LOG_CALL; + } } } @@ -818,8 +826,9 @@ static void main_window_init() /* +- Other options --------------------------------------+ * | □ Enable Windows 95 SO_BROADCAST bug | - * | □ Log debugging messages | - * | □ Log WinSock API calls | + * | □ Enable logging | + * | □ Log debugging messages (slow) | + * | □ Log WinSock API calls (slower!) | * | □ Log profiling counters | * +---------------------------------------------------------+ */ @@ -827,15 +836,17 @@ static void main_window_init() { wh.box_options = create_GroupBox(wh.main, "Other options"); - wh.opt_w95 = create_checkbox(wh.box_options, "Enable Windows 95 SO_BROADCAST bug", ID_OPT_W95); - wh.opt_log_debug = create_checkbox(wh.box_options, "Log debugging messages", ID_OPT_LOG_DEBUG); - wh.opt_log_trace = create_checkbox(wh.box_options, "Log WinSock API calls", ID_OPT_LOG_TRACE); - wh.opt_profile = create_checkbox(wh.box_options, "Log profiling counters", ID_OPT_PROFILE); + wh.opt_w95 = create_checkbox(wh.box_options, "Enable Windows 95 SO_BROADCAST bug", ID_OPT_W95); + wh.opt_log_enable = create_checkbox(wh.box_options, "Enable logging", ID_OPT_LOG_ENABLE); + wh.opt_log_debug = create_checkbox(wh.box_options, "Log debugging messages (slow)", ID_OPT_LOG_DEBUG); + wh.opt_log_trace = create_checkbox(wh.box_options, "Log WinSock API calls (even slower!)", ID_OPT_LOG_TRACE); + wh.opt_profile = create_checkbox(wh.box_options, "Log profiling counters", ID_OPT_PROFILE); - set_checkbox(wh.opt_w95, main_config.w95_bug); - set_checkbox(wh.opt_log_debug, main_config.log_level <= LOG_DEBUG); - set_checkbox(wh.opt_log_trace, main_config.log_level <= LOG_CALL); - set_checkbox(wh.opt_profile, main_config.profile); + set_checkbox(wh.opt_w95, main_config.w95_bug); + set_checkbox(wh.opt_log_enable, main_config.log_level < LOG_DISABLED); + set_checkbox(wh.opt_log_debug, main_config.log_level <= LOG_DEBUG); + set_checkbox(wh.opt_log_trace, main_config.log_level <= LOG_CALL); + set_checkbox(wh.opt_profile, main_config.profile); } wh.ok_btn = create_child(wh.main, "BUTTON", "OK", BS_PUSHBUTTON | WS_TABSTOP, 0, ID_OK); @@ -1006,6 +1017,9 @@ static void main_window_init() MoveWindow(wh.opt_w95, BOX_SIDE_PAD, box_options_y, BOX_INNER_WIDTH, text_h, TRUE); box_options_y += text_h + 2; + MoveWindow(wh.opt_log_enable, BOX_SIDE_PAD, box_options_y, BOX_INNER_WIDTH, text_h, TRUE); + box_options_y += text_h + 2; + MoveWindow(wh.opt_log_debug, BOX_SIDE_PAD, box_options_y, BOX_INNER_WIDTH, text_h, TRUE); box_options_y += text_h + 2; @@ -1058,7 +1072,9 @@ static void main_window_update() SetWindowText(wh.nic_node, node_s); } - EnableWindow(wh.opt_log_trace, get_checkbox(wh.opt_log_debug)); + EnableWindow(wh.opt_log_debug, get_checkbox(wh.opt_log_enable)); + EnableWindow(wh.opt_log_trace, get_checkbox(wh.opt_log_enable) && get_checkbox(wh.opt_log_debug)); + EnableWindow(wh.opt_profile, get_checkbox(wh.opt_log_enable)); std::vector visible_groups = { wh.box_encap, diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index a6d7c54..8603797 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -1,5 +1,5 @@ /* ipxwrapper - Library functions - * Copyright (C) 2008-2023 Daniel Collins + * Copyright (C) 2008-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -127,7 +127,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) fprof_init(stub_fstats, NUM_STUBS); fprof_init(ipxwrapper_fstats, ipxwrapper_fstats_size); - log_open("ipxwrapper.log"); + log_init(); + + main_config = get_main_config(); + min_log_level = main_config.log_level; + ipx_encap_type = main_config.encap_type; log_printf(LOG_INFO, "IPXWrapper %s", version_string); log_printf(LOG_INFO, "Compiled at %s", compile_time); @@ -144,10 +148,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) _putenv(env); } - main_config = get_main_config(); - min_log_level = main_config.log_level; - ipx_encap_type = main_config.encap_type; - if(main_config.fw_except) { log_printf(LOG_INFO, "Adding exception to Windows Firewall"); diff --git a/src/log.c b/src/log.c index dd93aca..03b8238 100644 --- a/src/log.c +++ b/src/log.c @@ -1,5 +1,5 @@ /* ipxwrapper - Logging functions - * Copyright (C) 2011 Daniel Collins + * Copyright (C) 2011-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -27,11 +27,14 @@ static HANDLE log_fh = NULL; static HANDLE log_mutex = NULL; -void log_open(const char *file) { +void log_init() +{ if(!(log_mutex = CreateMutex(NULL, FALSE, NULL))) { abort(); } - +} + +void log_open(const char *file) { log_fh = CreateFile( file, GENERIC_READ | GENERIC_WRITE, @@ -53,8 +56,10 @@ void log_close() { log_fh = NULL; } - CloseHandle(log_mutex); - log_mutex = NULL; + if(log_mutex) { + CloseHandle(log_mutex); + log_mutex = NULL; + } } void log_printf(enum ipx_log_level level, const char *fmt, ...) { @@ -66,6 +71,10 @@ void log_printf(enum ipx_log_level level, const char *fmt, ...) { WaitForSingleObject(log_mutex, INFINITE); + if(!log_fh) { + log_open("ipxwrapper.log"); + } + if(!log_fh) { ReleaseMutex(log_mutex); return; diff --git a/src/stubdll.c b/src/stubdll.c index 05914e6..d8f7284 100644 --- a/src/stubdll.c +++ b/src/stubdll.c @@ -1,5 +1,5 @@ /* IPXWrapper - Stub DLL functions - * Copyright (C) 2008-2023 Daniel Collins + * Copyright (C) 2008-2024 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -33,7 +33,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { { fprof_init(stub_fstats, NUM_STUBS); - log_open("ipxwrapper.log"); + log_init(); main_config_t config = get_main_config();