Reactos

[CMD] Support dynamic trace (#7093)

Based on Hans Harder's patch.
Realize tracing cmd on console
output.
JIRA issue: CORE-6669
- Add FEATURE_DYNAMIC_TRACE
feature.
- Add CmdTrace function.
- Re-define FIXME, ERR, WARN,
and TRACE macros.
- Check CMDTRACE environment
variable. If it was "ON", then enable
dynamic trace.

authored by

Katayama Hirofumi MZ and committed by
GitHub
3dcae2ce 6c74e69d

+106 -4
+1
base/shell/cmd/CMakeLists.txt
··· 54 54 time.c 55 55 timer.c 56 56 title.c 57 + trace.c 57 58 type.c 58 59 ver.c 59 60 verify.c
+2 -4
base/shell/cmd/config.h
··· 18 18 /* Define to enable the alias command, and aliases.*/ 19 19 #define FEATURE_ALIASES 20 20 21 - 22 21 /* Define to enable history */ 23 22 #define FEATURE_HISTORY 24 23 25 24 /*Define to enable history wrap (4nt's style)*/ 26 25 #define WRAP_HISTORY 27 26 28 - 29 27 /* Define one of these to enable filename completion */ 30 28 //#define FEATURE_UNIX_FILENAME_COMPLETION 31 29 #define FEATURE_4NT_FILENAME_COMPLETION 32 - 33 30 34 31 /* Define to enable the directory stack */ 35 32 #define FEATURE_DIRECTORY_STACK 36 33 37 - 38 34 /* Define to activate redirections and piping */ 39 35 #define FEATURE_REDIRECTION 40 36 37 + /* Define to enable dynamic switching of TRACE output in console */ 38 + #define FEATURE_DYNAMIC_TRACE 41 39 42 40 /* Define one of these to select the used locale. */ 43 41 /* (date and time formats etc.) used in DATE, TIME, */
+24
base/shell/cmd/precomp.h
··· 37 37 38 38 #include <wine/debug.h> 39 39 WINE_DEFAULT_DEBUG_CHANNEL(cmd); 40 + 40 41 #ifdef UNICODE 41 42 #define debugstr_aw debugstr_w 42 43 #else 43 44 #define debugstr_aw debugstr_a 44 45 #endif 46 + 47 + #ifdef FEATURE_DYNAMIC_TRACE 48 + 49 + extern BOOL g_bDynamicTrace; 50 + void CmdTrace(INT type, LPCSTR file, INT line, LPCSTR func, LPCSTR fmt, ...); 51 + 52 + #undef FIXME 53 + #define FIXME(fmt, ...) \ 54 + CmdTrace(__WINE_DBCL_FIXME, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__) 55 + 56 + #undef ERR 57 + #define ERR(fmt, ...) \ 58 + CmdTrace(__WINE_DBCL_ERR, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__) 59 + 60 + #undef WARN 61 + #define WARN(fmt, ...) \ 62 + CmdTrace(__WINE_DBCL_WARN, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__) 63 + 64 + #undef TRACE 65 + #define TRACE(fmt, ...) \ 66 + CmdTrace(__WINE_DBCL_TRACE, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__) 67 + 68 + #endif /* def FEATURE_DYNAMIC_TRACE */ 45 69 46 70 #endif /* __CMD_PRECOMP_H */
+7
base/shell/cmd/set.c
··· 175 175 } 176 176 177 177 *p++ = _T('\0'); 178 + 179 + #ifdef FEATURE_DYNAMIC_TRACE 180 + /* Check for dynamic TRACE ON/OFF */ 181 + if (!_tcsicmp(param, _T("CMDTRACE"))) 182 + g_bDynamicTrace = !_tcsicmp(p, _T("ON")); 183 + #endif 184 + 178 185 if (!SetEnvironmentVariable(param, *p ? p : NULL)) 179 186 { 180 187 retval = 1;
+72
base/shell/cmd/trace.c
··· 1 + /* 2 + * PROJECT: ReactOS Command Shell 3 + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) 4 + * PURPOSE: Dynamic trace (generates debugging output to console output) 5 + * COPYRIGHT: Copyright 2011 Hans Harder 6 + * Copyright 2024 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com> 7 + */ 8 + 9 + #include "precomp.h" 10 + 11 + #ifdef FEATURE_DYNAMIC_TRACE 12 + 13 + BOOL g_bDynamicTrace = FALSE; 14 + 15 + VOID CmdTrace(INT type, LPCSTR file, INT line, LPCSTR func, LPCSTR fmt, ...) 16 + { 17 + va_list va; 18 + int cch; 19 + char szTextA[800]; 20 + #ifdef _UNICODE 21 + wchar_t szTextW[800]; 22 + #endif 23 + static struct __wine_debug_functions s_Debug; 24 + 25 + va_start(va, fmt); 26 + 27 + /* Console output */ 28 + if (g_bDynamicTrace) 29 + { 30 + StringCchPrintfA(szTextA, _countof(szTextA), "%s (%d): ", file, line); 31 + cch = lstrlenA(szTextA); 32 + StringCchVPrintfA(&szTextA[cch], _countof(szTextA) - cch, fmt, va); 33 + 34 + #ifdef _UNICODE 35 + MultiByteToWideChar(OutputCodePage, 0, szTextA, -1, szTextW, _countof(szTextW)); 36 + szTextW[_countof(szTextW) - 1] = UNICODE_NULL; /* Avoid buffer overrun */ 37 + ConOutPuts(szTextW); 38 + #else 39 + ConOutPuts(szTextA); 40 + #endif 41 + } 42 + 43 + if (!s_Debug.dbg_vlog) 44 + __wine_dbg_set_functions(NULL, &s_Debug, sizeof(s_Debug)); 45 + 46 + /* Debug logging */ 47 + switch (type) 48 + { 49 + case __WINE_DBCL_FIXME: 50 + if (__WINE_IS_DEBUG_ON(_FIXME, __wine_dbch___default)) 51 + s_Debug.dbg_vlog(__WINE_DBCL_FIXME, __wine_dbch___default, file, func, line, fmt, va); 52 + break; 53 + case __WINE_DBCL_ERR: 54 + if (__WINE_IS_DEBUG_ON(_ERR, __wine_dbch___default)) 55 + s_Debug.dbg_vlog(__WINE_DBCL_ERR, __wine_dbch___default, file, func, line, fmt, va); 56 + break; 57 + case __WINE_DBCL_WARN: 58 + if (__WINE_IS_DEBUG_ON(_WARN, __wine_dbch___default)) 59 + s_Debug.dbg_vlog(__WINE_DBCL_WARN, __wine_dbch___default, file, func, line, fmt, va); 60 + break; 61 + case __WINE_DBCL_TRACE: 62 + if (__WINE_IS_DEBUG_ON(_TRACE, __wine_dbch___default)) 63 + s_Debug.dbg_vlog(__WINE_DBCL_TRACE, __wine_dbch___default, file, func, line, fmt, va); 64 + break; 65 + default: 66 + break; 67 + } 68 + 69 + va_end(va); 70 + } 71 + 72 + #endif /* def FEATURE_DYNAMIC_TRACE */