From 5aa458ffaecc42cbf6ee37ee4f985d255e31443e Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 11 Sep 2018 18:32:56 -0400 Subject: [PATCH] Simplify debug logging by allowing log functions to return String objects. Previously, debug log functions had to handle NULLs and truncate output to the available buffer size. This was verbose for both coding and testing. Instead, create a function/macro combination that allows log functions to return a simple String object. The wrapper function takes care of the memory context, handles NULLs, and truncates the log string based on the available buffer size. --- doc/xml/release.xml | 4 ++ src/common/io/filter/buffer.c | 21 +------ src/common/io/filter/buffer.h | 4 +- src/common/io/filter/group.c | 21 +------ src/common/io/filter/group.h | 4 +- src/common/io/filter/size.c | 21 +------ src/common/io/filter/size.h | 4 +- src/common/type/buffer.c | 21 +------ src/common/type/buffer.h | 4 +- src/common/type/list.c | 21 +------ src/common/type/list.h | 4 +- src/common/type/string.c | 18 ++++-- src/common/type/string.h | 14 ++++- src/common/type/stringList.c | 21 +------ src/common/type/stringList.h | 4 +- src/common/type/variant.c | 70 ++++++++------------- src/common/type/variant.h | 4 +- src/compress/gzipCompress.c | 27 ++------ src/compress/gzipCompress.h | 4 +- src/compress/gzipDecompress.c | 27 ++------ src/compress/gzipDecompress.h | 4 +- src/info/infoPg.c | 24 ++----- src/info/infoPg.h | 6 +- src/storage/storage.c | 21 +------ src/storage/storage.h | 4 +- test/define.yaml | 2 +- test/expect/mock-archive-001.log | 4 +- test/expect/mock-stanza-001.log | 4 +- test/src/module/common/typeBufferTest.c | 6 ++ test/src/module/common/typeStringListTest.c | 18 +----- test/src/module/common/typeStringTest.c | 11 +--- test/src/module/common/typeVariantTest.c | 20 ++---- test/src/module/compress/gzipTest.c | 10 +-- test/src/module/info/infoPgTest.c | 14 ++--- 34 files changed, 139 insertions(+), 327 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index fcddc3590..fc171dc0f 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -79,6 +79,10 @@

Change locking around async process forking to be more test friendly.

+ + +

Simplify debug logging by allowing log functions to return String objects.

+
diff --git a/src/common/io/filter/buffer.c b/src/common/io/filter/buffer.c index 21f809497..e04fdbefa 100644 --- a/src/common/io/filter/buffer.c +++ b/src/common/io/filter/buffer.c @@ -126,25 +126,10 @@ ioBufferFilter(const IoBuffer *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -ioBufferToLog(const IoBuffer *this, char *buffer, size_t bufferSize) +String * +ioBufferToLog(const IoBuffer *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{inputSame: %s, inputPos: %zu}", cvtBoolToConstZ(this->inputSame), this->inputPos); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{inputSame: %s, inputPos: %zu}", cvtBoolToConstZ(this->inputSame), this->inputPos); } /*********************************************************************************************************************************** diff --git a/src/common/io/filter/buffer.h b/src/common/io/filter/buffer.h index accc4d738..b8da85474 100644 --- a/src/common/io/filter/buffer.h +++ b/src/common/io/filter/buffer.h @@ -39,11 +39,11 @@ void ioBufferFree(IoBuffer *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t ioBufferToLog(const IoBuffer *this, char *buffer, size_t bufferSize); +String *ioBufferToLog(const IoBuffer *this); #define FUNCTION_DEBUG_IO_BUFFER_TYPE \ IoBuffer * #define FUNCTION_DEBUG_IO_BUFFER_FORMAT(value, buffer, bufferSize) \ - ioBufferToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, ioBufferToLog, buffer, bufferSize) #endif diff --git a/src/common/io/filter/group.c b/src/common/io/filter/group.c index 54d721c21..c6bd99f53 100644 --- a/src/common/io/filter/group.c +++ b/src/common/io/filter/group.c @@ -404,25 +404,10 @@ ioFilterGroupResult(const IoFilterGroup *this, const String *filterType) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -ioFilterGroupToLog(const IoFilterGroup *this, char *buffer, size_t bufferSize) +String * +ioFilterGroupToLog(const IoFilterGroup *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{inputSame: %s, done: %s}", cvtBoolToConstZ(this->inputSame), cvtBoolToConstZ(this->done)); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{inputSame: %s, done: %s}", cvtBoolToConstZ(this->inputSame), cvtBoolToConstZ(this->done)); } /*********************************************************************************************************************************** diff --git a/src/common/io/filter/group.h b/src/common/io/filter/group.h index 95fa3cc0e..8e80aa0eb 100644 --- a/src/common/io/filter/group.h +++ b/src/common/io/filter/group.h @@ -46,11 +46,11 @@ void ioFilterGroupFree(IoFilterGroup *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t ioFilterGroupToLog(const IoFilterGroup *this, char *buffer, size_t bufferSize); +String *ioFilterGroupToLog(const IoFilterGroup *this); #define FUNCTION_DEBUG_IO_FILTER_GROUP_TYPE \ IoFilterGroup * #define FUNCTION_DEBUG_IO_FILTER_GROUP_FORMAT(value, buffer, bufferSize) \ - ioFilterGroupToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, ioFilterGroupToLog, buffer, bufferSize) #endif diff --git a/src/common/io/filter/size.c b/src/common/io/filter/size.c index 1fdf6ef9d..4a2c3a4fb 100644 --- a/src/common/io/filter/size.c +++ b/src/common/io/filter/size.c @@ -84,25 +84,10 @@ ioSizeFilter(const IoSize *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -ioSizeToLog(const IoSize *this, char *buffer, size_t bufferSize) +String * +ioSizeToLog(const IoSize *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{size: %" PRIu64 "}", this->size); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{size: %" PRIu64 "}", this->size); } /*********************************************************************************************************************************** diff --git a/src/common/io/filter/size.h b/src/common/io/filter/size.h index 378b3c038..7a8c38350 100644 --- a/src/common/io/filter/size.h +++ b/src/common/io/filter/size.h @@ -39,11 +39,11 @@ void ioSizeFree(IoSize *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t ioSizeToLog(const IoSize *this, char *buffer, size_t bufferSize); +String *ioSizeToLog(const IoSize *this); #define FUNCTION_DEBUG_IO_SIZE_TYPE \ IoSize * #define FUNCTION_DEBUG_IO_SIZE_FORMAT(value, buffer, bufferSize) \ - ioSizeToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, ioSizeToLog, buffer, bufferSize) #endif diff --git a/src/common/type/buffer.c b/src/common/type/buffer.c index f8629c344..dc51bf6bd 100644 --- a/src/common/type/buffer.c +++ b/src/common/type/buffer.c @@ -401,25 +401,10 @@ bufUsedZero(Buffer *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -bufToLog(const Buffer *this, char *buffer, size_t bufferSize) +String * +bufToLog(const Buffer *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{used: %zu, size: %zu}", this->used, this->size); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{used: %zu, size: %zu}", this->used, this->size); } /*********************************************************************************************************************************** diff --git a/src/common/type/buffer.h b/src/common/type/buffer.h index 595fef2a2..7142b7b73 100644 --- a/src/common/type/buffer.h +++ b/src/common/type/buffer.h @@ -41,11 +41,11 @@ void bufFree(Buffer *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t bufToLog(const Buffer *this, char *buffer, size_t bufferSize); +String *bufToLog(const Buffer *this); #define FUNCTION_DEBUG_BUFFER_TYPE \ Buffer * #define FUNCTION_DEBUG_BUFFER_FORMAT(value, buffer, bufferSize) \ - bufToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, bufToLog, buffer, bufferSize) #endif diff --git a/src/common/type/list.c b/src/common/type/list.c index 761558cfc..3d7b3d639 100644 --- a/src/common/type/list.c +++ b/src/common/type/list.c @@ -176,25 +176,10 @@ lstSort(List *this, int (*comparator)(const void *, const void*)) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -lstToLog(const List *this, char *buffer, size_t bufferSize) +String * +lstToLog(const List *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{size: %u}", this->listSize); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{size: %u}", this->listSize); } /*********************************************************************************************************************************** diff --git a/src/common/type/list.h b/src/common/type/list.h index 82394b91c..34789057b 100644 --- a/src/common/type/list.h +++ b/src/common/type/list.h @@ -32,11 +32,11 @@ void lstFree(List *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t lstToLog(const List *this, char *buffer, size_t bufferSize); +String *lstToLog(const List *this); #define FUNCTION_DEBUG_LIST_TYPE \ List * #define FUNCTION_DEBUG_LIST_FORMAT(value, buffer, bufferSize) \ - lstToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, lstToLog, buffer, bufferSize) #endif diff --git a/src/common/type/string.c b/src/common/type/string.c index 6b77bb409..d4e5ae019 100644 --- a/src/common/type/string.c +++ b/src/common/type/string.c @@ -708,10 +708,9 @@ strTrunc(String *this, int idx) } /*********************************************************************************************************************************** -Convert to a zero-terminated string for logging +Convert an object to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -strToLog(const String *this, char *buffer, size_t bufferSize) +size_t strObjToLog(const void *object, StrObjToLogFormat formatFunc, char *buffer, size_t bufferSize) { size_t result = 0; @@ -719,10 +718,10 @@ strToLog(const String *this, char *buffer, size_t bufferSize) { String *string = NULL; - if (this == NULL) + if (object == NULL) string = strNew("null"); else - string = strNewFmt("{\"%s\"}", strPtr(this)); + string = formatFunc(object); result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); } @@ -731,6 +730,15 @@ strToLog(const String *this, char *buffer, size_t bufferSize) return result; } +/*********************************************************************************************************************************** +Convert to a zero-terminated string for logging +***********************************************************************************************************************************/ +String * +strToLog(const String *this) +{ + return strNewFmt("{\"%s\"}", strPtr(this)); +} + /*********************************************************************************************************************************** Free the string ***********************************************************************************************************************************/ diff --git a/src/common/type/string.h b/src/common/type/string.h index a8f4a311f..5c9863752 100644 --- a/src/common/type/string.h +++ b/src/common/type/string.h @@ -49,10 +49,20 @@ String *strTrunc(String *this, int idx); void strFree(String *this); +/*********************************************************************************************************************************** +Helper function/macro for object logging +***********************************************************************************************************************************/ +typedef String *(*StrObjToLogFormat)(const void *object); + +size_t strObjToLog(const void *object, StrObjToLogFormat formatFunc, char *buffer, size_t bufferSize); + +#define FUNCTION_DEBUG_STRING_OBJECT_FORMAT(object, formatFunc, buffer, bufferSize) \ + strObjToLog(object, (StrObjToLogFormat)formatFunc, buffer, bufferSize) + /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t strToLog(const String *this, char *buffer, size_t bufferSize); +String *strToLog(const String *this); #define FUNCTION_DEBUG_CONST_STRING_TYPE \ const String * @@ -62,7 +72,7 @@ size_t strToLog(const String *this, char *buffer, size_t bufferSize); #define FUNCTION_DEBUG_STRING_TYPE \ String * #define FUNCTION_DEBUG_STRING_FORMAT(value, buffer, bufferSize) \ - strToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, strToLog, buffer, bufferSize) #define FUNCTION_DEBUG_STRINGP_TYPE \ const String ** diff --git a/src/common/type/stringList.c b/src/common/type/stringList.c index 831714971..9ef12a688 100644 --- a/src/common/type/stringList.c +++ b/src/common/type/stringList.c @@ -520,25 +520,10 @@ strLstSort(StringList *this, SortOrder sortOrder) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -strLstToLog(const StringList *this, char *buffer, size_t bufferSize) +String * +strLstToLog(const StringList *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{[%s]}", strPtr(strLstJoinQuote(this, ", ", "\""))); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{[%s]}", strPtr(strLstJoinQuote(this, ", ", "\""))); } /*********************************************************************************************************************************** diff --git a/src/common/type/stringList.h b/src/common/type/stringList.h index 69cb7cc70..5fca67332 100644 --- a/src/common/type/stringList.h +++ b/src/common/type/stringList.h @@ -50,11 +50,11 @@ void strLstFree(StringList *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t strLstToLog(const StringList *this, char *buffer, size_t bufferSize); +String *strLstToLog(const StringList *this); #define FUNCTION_DEBUG_STRING_LIST_TYPE \ StringList * #define FUNCTION_DEBUG_STRING_LIST_FORMAT(value, buffer, bufferSize) \ - strLstToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, strLstToLog, buffer, bufferSize) #endif diff --git a/src/common/type/variant.c b/src/common/type/variant.c index e1f612a0a..e5d91c280 100644 --- a/src/common/type/variant.c +++ b/src/common/type/variant.c @@ -926,61 +926,43 @@ varVarLst(const Variant *this) /*********************************************************************************************************************************** Convert variant to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -varToLog(const Variant *this, char *buffer, size_t bufferSize) +String * +varToLog(const Variant *this) { - size_t result = 0; + String *string = NULL; - MEM_CONTEXT_TEMP_BEGIN() + switch (varType(this)) { - String *string = NULL; - - if (this == NULL) + case varTypeString: { - string = strNew("null"); - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); + string = strNewFmt("\"%s\"", strPtr(varStrForce(this))); + break; } - else + + case varTypeKeyValue: { - switch (varType(this)) - { - case varTypeString: - { - String *temp = varStrForce(this); - string = strNewFmt("\"%s\"", strPtr(temp)); - strFree(temp); - break; - } + string = strNew("KeyValue"); + break; + } - case varTypeKeyValue: - { - string = strNew("KeyValue"); - break; - } + case varTypeVariantList: + { + string = strNew("VariantList"); + break; + } - case varTypeVariantList: - { - string = strNew("VariantList"); - break; - } - - case varTypeBool: - case varTypeDouble: - case varTypeInt: - case varTypeInt64: - case varTypeUInt64: - { - string = varStrForce(this); - break; - } - } - - result = (size_t)snprintf(buffer, bufferSize, "{%s}", strPtr(string)); + case varTypeBool: + case varTypeDouble: + case varTypeInt: + case varTypeInt64: + case varTypeUInt64: + { + string = varStrForce(this); + break; } } - MEM_CONTEXT_TEMP_END(); - return result; + return strNewFmt("{%s}", strPtr(string)); } /*********************************************************************************************************************************** diff --git a/src/common/type/variant.h b/src/common/type/variant.h index b9dc415fe..fb042224e 100644 --- a/src/common/type/variant.h +++ b/src/common/type/variant.h @@ -73,7 +73,7 @@ void varFree(Variant *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t varToLog(const Variant *this, char *buffer, size_t bufferSize); +String *varToLog(const Variant *this); #define FUNCTION_DEBUG_CONST_VARIANT_TYPE \ const FUNCTION_DEBUG_VARIANT_TYPE @@ -83,6 +83,6 @@ size_t varToLog(const Variant *this, char *buffer, size_t bufferSize); #define FUNCTION_DEBUG_VARIANT_TYPE \ Variant * #define FUNCTION_DEBUG_VARIANT_FORMAT(value, buffer, bufferSize) \ - varToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, varToLog, buffer, bufferSize) #endif diff --git a/src/compress/gzipCompress.c b/src/compress/gzipCompress.c index ae6bb86b1..a39c3b22a 100644 --- a/src/compress/gzipCompress.c +++ b/src/compress/gzipCompress.c @@ -177,29 +177,12 @@ gzipCompressInputSame(const GzipCompress *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -gzipCompressToLog(const GzipCompress *this, char *buffer, size_t bufferSize) +String * +gzipCompressToLog(const GzipCompress *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - { - string = strNewFmt( - "{inputSame: %s, done: %s, flushing: %s, availIn: %u}", cvtBoolToConstZ(this->inputSame), - cvtBoolToConstZ(this->done), cvtBoolToConstZ(this->done), this->stream != NULL ? this->stream->avail_in : 0); - } - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt( + "{inputSame: %s, done: %s, flushing: %s, availIn: %u}", cvtBoolToConstZ(this->inputSame), cvtBoolToConstZ(this->done), + cvtBoolToConstZ(this->done), this->stream != NULL ? this->stream->avail_in : 0); } /*********************************************************************************************************************************** diff --git a/src/compress/gzipCompress.h b/src/compress/gzipCompress.h index 3116e7d39..b86e055b4 100644 --- a/src/compress/gzipCompress.h +++ b/src/compress/gzipCompress.h @@ -39,11 +39,11 @@ void gzipCompressFree(GzipCompress *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t gzipCompressToLog(const GzipCompress *this, char *buffer, size_t bufferSize); +String *gzipCompressToLog(const GzipCompress *this); #define FUNCTION_DEBUG_GZIP_COMPRESS_TYPE \ GzipCompress * #define FUNCTION_DEBUG_GZIP_COMPRESS_FORMAT(value, buffer, bufferSize) \ - gzipCompressToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, gzipCompressToLog, buffer, bufferSize) #endif diff --git a/src/compress/gzipDecompress.c b/src/compress/gzipDecompress.c index 624797c32..8eb9d4946 100644 --- a/src/compress/gzipDecompress.c +++ b/src/compress/gzipDecompress.c @@ -152,29 +152,12 @@ gzipDecompressInputSame(const GzipDecompress *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -gzipDecompressToLog(const GzipDecompress *this, char *buffer, size_t bufferSize) +String * +gzipDecompressToLog(const GzipDecompress *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - { - string = strNewFmt( - "{inputSame: %s, done: %s, availIn: %u}", cvtBoolToConstZ(this->inputSame), cvtBoolToConstZ(this->done), - this->stream != NULL ? this->stream->avail_in : 0); - } - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt( + "{inputSame: %s, done: %s, availIn: %u}", cvtBoolToConstZ(this->inputSame), cvtBoolToConstZ(this->done), + this->stream != NULL ? this->stream->avail_in : 0); } /*********************************************************************************************************************************** diff --git a/src/compress/gzipDecompress.h b/src/compress/gzipDecompress.h index 57d5b87aa..31bc40c12 100644 --- a/src/compress/gzipDecompress.h +++ b/src/compress/gzipDecompress.h @@ -39,11 +39,11 @@ void gzipDecompressFree(GzipDecompress *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t gzipDecompressToLog(const GzipDecompress *this, char *buffer, size_t bufferSize); +String *gzipDecompressToLog(const GzipDecompress *this); #define FUNCTION_DEBUG_GZIP_DECOMPRESS_TYPE \ GzipDecompress * #define FUNCTION_DEBUG_GZIP_DECOMPRESS_FORMAT(value, buffer, bufferSize) \ - gzipDecompressToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, gzipDecompressToLog, buffer, bufferSize) #endif diff --git a/src/info/infoPg.c b/src/info/infoPg.c index 35e065105..0772683dd 100644 --- a/src/info/infoPg.c +++ b/src/info/infoPg.c @@ -216,26 +216,12 @@ infoPgDataTotal(const InfoPg *this) /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -infoPgDataToLog(const InfoPgData *this, char *buffer, size_t bufferSize) +String * +infoPgDataToLog(const InfoPgData *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{\"id: %u, version: %u, systemId %" PRIu64 ", catalog %"PRIu32", control %"PRIu32"\"}", - this->id, this->version, this->systemId, this->catalogVersion, this->controlVersion); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt( + "{id: %u, version: %u, systemId: %" PRIu64 ", catalogVersion: %" PRIu32 ", controlVersion: %" PRIu32 "}", + this->id, this->version, this->systemId, this->catalogVersion, this->controlVersion); } /*********************************************************************************************************************************** diff --git a/src/info/infoPg.h b/src/info/infoPg.h index d3d2b3eda..b1e8960a3 100644 --- a/src/info/infoPg.h +++ b/src/info/infoPg.h @@ -62,7 +62,7 @@ void infoPgFree(InfoPg *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t infoPgDataToLog(const InfoPgData *this, char *buffer, size_t bufferSize); +String *infoPgDataToLog(const InfoPgData *this); #define FUNCTION_DEBUG_INFO_PG_TYPE \ InfoPg * @@ -71,11 +71,11 @@ size_t infoPgDataToLog(const InfoPgData *this, char *buffer, size_t bufferSize); #define FUNCTION_DEBUG_INFO_PG_DATA_TYPE \ InfoPgData #define FUNCTION_DEBUG_INFO_PG_DATA_FORMAT(value, buffer, bufferSize) \ - infoPgDataToLog(&value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(&value, infoPgDataToLog, buffer, bufferSize) #define FUNCTION_DEBUG_INFO_PG_DATAP_TYPE \ InfoPgData * #define FUNCTION_DEBUG_INFO_PG_DATAP_FORMAT(value, buffer, bufferSize) \ - infoPgDataToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, infoPgDataToLog, buffer, bufferSize) #endif diff --git a/src/storage/storage.c b/src/storage/storage.c index 36a9e926c..3b7ad0823 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -620,25 +620,10 @@ storageRemove(const Storage *this, const String *fileExp, StorageRemoveParam par /*********************************************************************************************************************************** Convert to a zero-terminated string for logging ***********************************************************************************************************************************/ -size_t -storageToLog(const Storage *this, char *buffer, size_t bufferSize) +String * +storageToLog(const Storage *this) { - size_t result = 0; - - MEM_CONTEXT_TEMP_BEGIN() - { - String *string = NULL; - - if (this == NULL) - string = strNew("null"); - else - string = strNewFmt("{path: %s, write: %s}", strPtr(strQuoteZ(this->path, "\"")), cvtBoolToConstZ(this->write)); - - result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string)); - } - MEM_CONTEXT_TEMP_END(); - - return result; + return strNewFmt("{path: %s, write: %s}", strPtr(strQuoteZ(this->path, "\"")), cvtBoolToConstZ(this->write)); } /*********************************************************************************************************************************** diff --git a/src/storage/storage.h b/src/storage/storage.h index 4e12d4b77..eea5eb02a 100644 --- a/src/storage/storage.h +++ b/src/storage/storage.h @@ -251,11 +251,11 @@ void storageFree(const Storage *this); /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ -size_t storageToLog(const Storage *this, char *buffer, size_t bufferSize); +String *storageToLog(const Storage *this); #define FUNCTION_DEBUG_STORAGE_TYPE \ Storage * #define FUNCTION_DEBUG_STORAGE_FORMAT(value, buffer, bufferSize) \ - (size_t)storageToLog(value, buffer, bufferSize) + FUNCTION_DEBUG_STRING_OBJECT_FORMAT(value, storageToLog, buffer, bufferSize) #endif diff --git a/test/define.yaml b/test/define.yaml index 7d56e3590..b5d70ec39 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -166,7 +166,7 @@ unit: # ---------------------------------------------------------------------------------------------------------------------------- - name: type-buffer - total: 4 + total: 5 coverage: common/type/buffer: full diff --git a/test/expect/mock-archive-001.log b/test/expect/mock-archive-001.log index 7737f75cc..dd453382a 100644 --- a/test/expect/mock-archive-001.log +++ b/test/expect/mock-archive-001.log @@ -137,7 +137,7 @@ P00 DEBUG: info/info::infoNew: (fileName: {"/archive.info"}) P00 DEBUG: info/info::infoNew: => {Info} P00 DEBUG: info/info::infoIni: (this: {Info}) P00 DEBUG: info/info::infoIni: => {Ini} -P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {"id: 1, version: 90400, systemId 1000000000000000094, catalog 0, control 0"}) +P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {id: 1, version: 90400, systemId: 1000000000000000094, catalogVersion: 0, controlVersion: 0}) P00 DEBUG: info/infoPg::infoPgAdd: => 0 P00 DEBUG: info/infoPg::infoPgNew: => {InfoPg} P00 DEBUG: info/infoArchive::infoArchiveNew: => {InfoArchive} @@ -181,7 +181,7 @@ P00 DEBUG: info/info::infoNew: (fileName: {"/archive.info"}) P00 DEBUG: info/info::infoNew: => {Info} P00 DEBUG: info/info::infoIni: (this: {Info}) P00 DEBUG: info/info::infoIni: => {Ini} -P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {"id: 1, version: 90400, systemId 1000000000000000094, catalog 0, control 0"}) +P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {id: 1, version: 90400, systemId: 1000000000000000094, catalogVersion: 0, controlVersion: 0}) P00 DEBUG: info/infoPg::infoPgAdd: => 0 P00 DEBUG: info/infoPg::infoPgNew: => {InfoPg} P00 DEBUG: info/infoArchive::infoArchiveNew: => {InfoArchive} diff --git a/test/expect/mock-stanza-001.log b/test/expect/mock-stanza-001.log index 9a64ffe74..6f97adb54 100644 --- a/test/expect/mock-stanza-001.log +++ b/test/expect/mock-stanza-001.log @@ -554,9 +554,9 @@ P00 DEBUG: info/info::infoNew: (fileName: {"/archive.info"}) P00 DEBUG: info/info::infoNew: => {Info} P00 DEBUG: info/info::infoIni: (this: {Info}) P00 DEBUG: info/info::infoIni: => {Ini} -P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {"id: 2, version: 90400, systemId 1000000000000000094, catalog 0, control 0"}) +P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {id: 2, version: 90400, systemId: 1000000000000000094, catalogVersion: 0, controlVersion: 0}) P00 DEBUG: info/infoPg::infoPgAdd: => 0 -P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {"id: 1, version: 90300, systemId 1000000000000000093, catalog 0, control 0"}) +P00 DEBUG: info/infoPg::infoPgAdd: (this: {InfoPg}, infoPgData: {id: 1, version: 90300, systemId: 1000000000000000093, catalogVersion: 0, controlVersion: 0}) P00 DEBUG: info/infoPg::infoPgAdd: => 1 P00 DEBUG: info/infoPg::infoPgNew: => {InfoPg} P00 DEBUG: info/infoArchive::infoArchiveNew: => {InfoArchive} diff --git a/test/src/module/common/typeBufferTest.c b/test/src/module/common/typeBufferTest.c index a48d92e7a..c6f6e2a62 100644 --- a/test/src/module/common/typeBufferTest.c +++ b/test/src/module/common/typeBufferTest.c @@ -126,5 +126,11 @@ testRun(void) TEST_RESULT_STR(strPtr(strNewBuf(bufCat(buffer, bufNewStr(strNew("AB"))))), "AB", "cat buffer with space"); } + // ***************************************************************************************************************************** + if (testBegin("bufToLog()")) + { + TEST_RESULT_STR(strPtr(bufToLog(bufNew(100))), "{used: 0, size: 100}", "buf to log"); + } + FUNCTION_HARNESS_RESULT_VOID(); } diff --git a/test/src/module/common/typeStringListTest.c b/test/src/module/common/typeStringListTest.c index 6f8235233..436c5fe73 100644 --- a/test/src/module/common/typeStringListTest.c +++ b/test/src/module/common/typeStringListTest.c @@ -177,27 +177,15 @@ testRun(void) if (testBegin("strLstToLog()")) { StringList *list = strLstNew(); - char buffer[STACK_TRACE_PARAM_MAX]; - TEST_RESULT_INT(strLstToLog(NULL, buffer, 4), 4, "format null list with too small buffer"); - TEST_RESULT_STR(buffer, "nul", " check format"); - - TEST_RESULT_INT(strLstToLog(NULL, buffer, STACK_TRACE_PARAM_MAX), 4, "format null list"); - TEST_RESULT_STR(buffer, "null", " check format"); - - TEST_RESULT_INT(strLstToLog(list, buffer, STACK_TRACE_PARAM_MAX), 4, "format empty list"); - TEST_RESULT_STR(buffer, "{[]}", " check format"); + TEST_RESULT_STR(strPtr(strLstToLog(list)), "{[]}", "format empty list"); strLstAddZ(list, "item1"); - - TEST_RESULT_INT(strLstToLog(list, buffer, STACK_TRACE_PARAM_MAX), 11, "format 1 item list"); - TEST_RESULT_STR(buffer, "{[\"item1\"]}", " check format"); + TEST_RESULT_STR(strPtr(strLstToLog(list)), "{[\"item1\"]}", "format 1 item list"); strLstAddZ(list, "item2"); strLstAddZ(list, "item3"); - - TEST_RESULT_INT(strLstToLog(list, buffer, STACK_TRACE_PARAM_MAX), 29, "format 3 item list"); - TEST_RESULT_STR(buffer, "{[\"item1\", \"item2\", \"item3\"]}", " check format"); + TEST_RESULT_STR(strPtr(strLstToLog(list)), "{[\"item1\", \"item2\", \"item3\"]}", "format 3 item list"); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index 4fc68028a..5e496bdc9 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -201,16 +201,7 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("strToLog()")) { - char buffer[STACK_TRACE_PARAM_MAX]; - - TEST_RESULT_INT(strToLog(NULL, buffer, 4), 4, "format null string with too small buffer"); - TEST_RESULT_STR(buffer, "nul", " check format"); - - TEST_RESULT_INT(strToLog(NULL, buffer, STACK_TRACE_PARAM_MAX), 4, "format null string"); - TEST_RESULT_STR(buffer, "null", " check format"); - - TEST_RESULT_INT(strToLog(strNew("test"), buffer, STACK_TRACE_PARAM_MAX), 8, "format string"); - TEST_RESULT_STR(buffer, "{\"test\"}", " check format"); + TEST_RESULT_STR(strPtr(strToLog(strNew("test"))), "{\"test\"}", "format string"); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/common/typeVariantTest.c b/test/src/module/common/typeVariantTest.c index f455b2622..02ebb3a64 100644 --- a/test/src/module/common/typeVariantTest.c +++ b/test/src/module/common/typeVariantTest.c @@ -307,22 +307,10 @@ testRun(void) // ----------------------------------------------------------------------------------------------------------------------------- if (testBegin("varToLog")) { - char buffer[STACK_TRACE_PARAM_MAX]; - - TEST_RESULT_INT(varToLog(NULL, buffer, 4), 4, "format truncated null"); - TEST_RESULT_STR(buffer, "nul", " check buffer"); - - TEST_RESULT_INT(varToLog(varNewStrZ("testme"), buffer, STACK_TRACE_PARAM_MAX), 10, "format String"); - TEST_RESULT_STR(buffer, "{\"testme\"}", " check buffer"); - - TEST_RESULT_INT(varToLog(varNewBool(false), buffer, STACK_TRACE_PARAM_MAX), 7, "format bool"); - TEST_RESULT_STR(buffer, "{false}", " check buffer"); - - TEST_RESULT_INT(varToLog(varNewKv(), buffer, STACK_TRACE_PARAM_MAX), 10, "format KeyValue"); - TEST_RESULT_STR(buffer, "{KeyValue}", " check buffer"); - - TEST_RESULT_INT(varToLog(varNewVarLst(varLstNew()), buffer, STACK_TRACE_PARAM_MAX), 13, "format VariantList"); - TEST_RESULT_STR(buffer, "{VariantList}", " check buffer"); + TEST_RESULT_STR(strPtr(varToLog(varNewStrZ("testme"))), "{\"testme\"}", "format String"); + TEST_RESULT_STR(strPtr(varToLog(varNewBool(false))), "{false}", "format bool"); + TEST_RESULT_STR(strPtr(varToLog(varNewKv())), "{KeyValue}", "format KeyValue"); + TEST_RESULT_STR(strPtr(varToLog(varNewVarLst(varLstNew()))), "{VariantList}", "format VariantList"); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/compress/gzipTest.c b/test/src/module/compress/gzipTest.c index a74579512..b769480d2 100644 --- a/test/src/module/compress/gzipTest.c +++ b/test/src/module/compress/gzipTest.c @@ -164,21 +164,15 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("gzipDecompressToLog() and gzipCompressToLog()")) { - char buffer[STACK_TRACE_PARAM_MAX]; GzipDecompress *decompress = gzipDecompressNew(false); - TEST_RESULT_INT(gzipDecompressToLog(NULL, buffer, 4), 4, "format object with too small buffer"); - TEST_RESULT_STR(buffer, "nul", " check format"); - - TEST_RESULT_INT(gzipDecompressToLog(decompress, buffer, STACK_TRACE_PARAM_MAX), 43, "format object"); - TEST_RESULT_STR(buffer, "{inputSame: false, done: false, availIn: 0}", " check format"); + TEST_RESULT_STR(strPtr(gzipDecompressToLog(decompress)), "{inputSame: false, done: false, availIn: 0}", "format object"); decompress->inputSame = true; decompress->done = true; inflateEnd(decompress->stream); decompress->stream = NULL; - TEST_RESULT_INT(gzipDecompressToLog(decompress, buffer, STACK_TRACE_PARAM_MAX), 41, "format object"); - TEST_RESULT_STR(buffer, "{inputSame: true, done: true, availIn: 0}", " check format"); + TEST_RESULT_STR(strPtr(gzipDecompressToLog(decompress)), "{inputSame: true, done: true, availIn: 0}", "format object"); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/info/infoPgTest.c b/test/src/module/info/infoPgTest.c index abd08c377..470d50fb4 100644 --- a/test/src/module/info/infoPgTest.c +++ b/test/src/module/info/infoPgTest.c @@ -133,22 +133,16 @@ testRun(void) // infoPgDataToLog //-------------------------------------------------------------------------------------------------------------------------- - char buffer[STACK_TRACE_PARAM_MAX]; - - TEST_RESULT_INT(infoPgDataToLog(NULL, buffer, 4), 4, "infoPgDataToLog - format null string with too small buffer"); - - TEST_RESULT_INT(strToLog(NULL, buffer, STACK_TRACE_PARAM_MAX), 4, "infoPgDataToLog - format null string"); - TEST_RESULT_STR(buffer, "null", " check format"); - // test max values infoPgDataTest.id = (unsigned int)4294967295; infoPgDataTest.version = (unsigned int)4294967295; infoPgDataTest.systemId = 18446744073709551615U; infoPgDataTest.catalogVersion = (uint32_t)4294967295; infoPgDataTest.controlVersion = (uint32_t)4294967295; - TEST_RESULT_INT(infoPgDataToLog(&infoPgDataTest, buffer, STACK_TRACE_PARAM_MAX), 110, "infoPgDataToLog - format string"); - TEST_RESULT_STR(buffer, - "{\"id: 4294967295, version: 4294967295, systemId 18446744073709551615, catalog 4294967295, control 4294967295\"}", + TEST_RESULT_STR( + strPtr(infoPgDataToLog(&infoPgDataTest)), + "{id: 4294967295, version: 4294967295, systemId: 18446744073709551615, catalogVersion: 4294967295, controlVersion:" + " 4294967295}", " check max format"); } }