pgbackrest.git / summary / log / commit / refs

commit    fccca0d716de2be92ddd5c7d8f532dcad41fd828
Author:   David Steele <david@pgmasters.net>
Date:     Thu Aug 20 18:11:40 2020 +0000

    Refactor option logging into a general-purpose function.


src/command/command.c | 78 ++++++++++++++++++++++++----------- src/command/command.h | 4 ++ test/src/module/command/commandTest.c | 7 ++++ 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 1cfb89df4..fe8e3b8d8 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -19,6 +19,7 @@ Common Command Routines Track time command started ***********************************************************************************************************************************/ static TimeMSec timeBegin; +static String *cmdOptionStr; /**********************************************************************************************************************************/ void @@ -32,27 +33,19 @@ cmdInit(void) } /**********************************************************************************************************************************/ -void -cmdBegin(bool logOption) +const String * +cmdOption(void) { - FUNCTION_LOG_BEGIN(logLevelTrace); - FUNCTION_LOG_PARAM(BOOL, logOption); - FUNCTION_LOG_END(); + FUNCTION_TEST_VOID(); - ASSERT(cfgCommand() != cfgCmdNone); - - // This is fairly expensive log message to generate so skip it if it won't be output - if (logAny(cfgLogLevelDefault())) + if (cmdOptionStr == NULL) { - MEM_CONTEXT_TEMP_BEGIN() + MEM_CONTEXT_BEGIN(memContextTop()) { - // Basic info on command start - String *info = strNewFmt("%s command begin", strZ(cfgCommandRoleName())); + cmdOptionStr = strNew(""); - if (logOption) + MEM_CONTEXT_TEMP_BEGIN() { - strCatFmt(info, " %s:", PROJECT_VERSION); - // Get command define id used to determine which options are valid for this command ConfigDefineCommand commandDefId = cfgCommandDefIdFromId(cfgCommand()); @@ -61,22 +54,22 @@ cmdBegin(bool logOption) if (strLstSize(commandParamList) != 0) { - strCatFmt(info, " ["); + strCatFmt(cmdOptionStr, " ["); for (unsigned int commandParamIdx = 0; commandParamIdx < strLstSize(commandParamList); commandParamIdx++) { const String *commandParam = strLstGet(commandParamList, commandParamIdx); if (commandParamIdx != 0) - strCatFmt(info, ", "); + strCatFmt(cmdOptionStr, ", "); if (strchr(strZ(commandParam), ' ') != NULL) commandParam = strNewFmt("\"%s\"", strZ(commandParam)); - strCat(info, commandParam); + strCat(cmdOptionStr, commandParam); } - strCatFmt(info, "]"); + strCatFmt(cmdOptionStr, "]"); } // Loop though options and add the ones that are interesting @@ -90,10 +83,10 @@ cmdBegin(bool logOption) // If option was negated if (cfgOptionNegate(optionId)) - strCatFmt(info, " --no-%s", cfgOptionName(optionId)); + strCatFmt(cmdOptionStr, " --no-%s", cfgOptionName(optionId)); // If option was reset else if (cfgOptionReset(optionId)) - strCatFmt(info, " --reset-%s", cfgOptionName(optionId)); + strCatFmt(cmdOptionStr, " --reset-%s", cfgOptionName(optionId)); // Else set and not default else if (cfgOptionSource(optionId) != cfgSourceDefault && cfgOptionTest(optionId)) { @@ -101,10 +94,10 @@ cmdBegin(bool logOption) // Don't show redacted options if (cfgDefOptionSecure(optionDefId)) - strCatFmt(info, " --%s=<redacted>", cfgOptionName(optionId)); + strCatFmt(cmdOptionStr, " --%s=<redacted>", cfgOptionName(optionId)); // Output boolean option else if (cfgDefOptionType(optionDefId) == cfgDefOptTypeBoolean) - strCatFmt(info, " --%s", cfgOptionName(optionId)); + strCatFmt(cmdOptionStr, " --%s", cfgOptionName(optionId)); // Output other options else { @@ -144,17 +137,52 @@ cmdBegin(bool logOption) { const String *value = strLstGet(valueList, valueListIdx); - strCatFmt(info, " --%s", cfgOptionName(optionId)); + strCatFmt(cmdOptionStr, " --%s", cfgOptionName(optionId)); if (strchr(strZ(value), ' ') != NULL) value = strNewFmt("\"%s\"", strZ(value)); - strCatFmt(info, "=%s", strZ(value)); + strCatFmt(cmdOptionStr, "=%s", strZ(value)); } } } } } + MEM_CONTEXT_TEMP_END(); + } + MEM_CONTEXT_END(); + } + + FUNCTION_TEST_RETURN(cmdOptionStr); +} + +/**********************************************************************************************************************************/ +void +cmdBegin(bool logOption) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(BOOL, logOption); + FUNCTION_LOG_END(); + + ASSERT(cfgCommand() != cfgCmdNone); + + // This is fairly expensive log message to generate so skip it if it won't be output + if (logAny(cfgLogLevelDefault())) + { + MEM_CONTEXT_TEMP_BEGIN() + { + // Basic info on command start + String *info = strNewFmt("%s command begin", strZ(cfgCommandRoleName())); + + if (logOption) + { + // Free the old option string if it exists. This is needed when more than one command is run in a row so an option + // string gets created for the new command. + strFree(cmdOptionStr); + cmdOptionStr = NULL; + + strCatFmt(info, " %s:%s", PROJECT_VERSION, strZ(cmdOption())); + } LOG(cfgLogLevelDefault(), 0, strZ(info)); } diff --git a/src/command/command.h b/src/command/command.h index 91e7d4940..d6e6d6c51 100644 --- a/src/command/command.h +++ b/src/command/command.h @@ -15,6 +15,10 @@ void cmdInit(void); // Begin the command void cmdBegin(bool logOption); +// Get the command options as a string. This is output in cmdBegin() if the log level is high enough but can also be used for +// debugging or error reporting later on. +const String *cmdOption(void); + // End the command void cmdEnd(int code, const String *errorMessage); diff --git a/test/src/module/command/commandTest.c b/test/src/module/command/commandTest.c index 5d15e19f3..3b3b38373 100644 --- a/test/src/module/command/commandTest.c +++ b/test/src/module/command/commandTest.c @@ -93,6 +93,13 @@ testRun(void) harnessLogResult( "P00 INFO: archive-get command begin"); + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("check options in cache"); + + TEST_RESULT_STR_Z( + cmdOption(), " --no-config --reset-repo1-host --repo1-path=\"/path/to the/repo\" --repo1-s3-key=<redacted>", + "command options"); + // Nothing should be logged for command begin when the log level is too low // ------------------------------------------------------------------------------------------------------------------------- harnessLogLevelSet(logLevelWarn); [parent: 959f77cd6a3b]