Scope Trace: Add some new tracing macros and an ast_str helper

Created new SCOPE_ functions that don't depend on RAII_VAR.  Besides
generating less code, the use of the explicit SCOPE_EXIT macros
capture the line number where the scope exited.  The RAII_VAR
versions can't do that.

 * SCOPE_ENTER(level, ...): Like SCOPE_TRACE but doesn't use
   RAII_VAR and therefore needs needs one of...

 * SCOPE_EXIT(...): Decrements the trace stack counter and optionally
   prints a message.

 * SCOPE_EXIT_EXPR(__expr, ...): Decrements the trace stack counter,
   optionally prints a message, then executes the expression.
   SCOPE_EXIT_EXPR(break, "My while got broken\n");

 * SCOPE_EXIT_RTN(, ...): Decrements the trace stack counter,
   optionally prints a message, then returns without a value.
   SCOPE_EXIT_RTN("Bye\n");

 * SCOPE_EXIT_RTN_VALUE(__return_value, ...): Decrements the trace
   stack counter, optionally prints a message, then returns the value
   specified.
   SCOPE_EXIT_RTN_VALUE(rc, "Returning with RC: %d\n", rc);

Create an ast_str helper ast_str_tmp() that allocates a temporary
ast_str that can be passed to a function that needs it, then frees
it.  This makes using the above macros easier.  Example:

   SCOPE_ENTER(1, Format Caps 1: %s  Format Caps 2: %s\n",
       ast_str_tmp(32, ast_format_cap_get_names(cap1, &STR_TMP),
       ast_str_tmp(32, ast_format_cap_get_names(cap2, &STR_TMP));

The calls to ast_str_tmp create an ast_str of the specified initial
length which can be referenced as STR_TMP.  It then calls the
expression, which must return a char *, ast_strdupa's it, frees
STR_TMP, then returns the ast_strdupa'd string.  That string is
freed when the function returns.

Change-Id: I44059b20d55a889aa91440d2f8a590865998be51
This commit is contained in:
George Joseph
2020-06-30 07:56:34 -06:00
parent f252eae438
commit 43ba72dea0
4 changed files with 232 additions and 24 deletions

View File

@@ -38,58 +38,77 @@
#include "asterisk/test.h"
#include "asterisk/logger.h"
static void test_scope2(void)
static const char *str_appender(struct ast_str**buf, char *a)
{
SCOPE_TRACE(1);
ast_str_append(buf, 0, "<append %s>", a);
return ast_str_buffer(*buf);
}
static void test_scope(void)
static void test_scope_trace(void)
{
SCOPE_TRACE(1, "nested function: %d * %d = %d\n", 6, 7, (6 * 7));
SCOPE_ENTER(1, "subfunction\n");
SCOPE_EXIT_RTN("got out\n");
}
test_scope2();
static int test_scope_enter_function(void)
{
SCOPE_ENTER(1, "%s %s %s %s %s %s %s\n",
ast_str_tmp(12, str_appender(&STR_TMP, "str1")),
ast_str_tmp(12, str_appender(&STR_TMP, "str2")),
ast_str_tmp(32, str_appender(&STR_TMP, "AAAAAAAAAAAAAAAAAAAAAAAA")),
ast_str_tmp(12, str_appender(&STR_TMP, "B")),
"ccccccccccccc",
ast_str_tmp(12, str_appender(&STR_TMP, "DDDDD")),
ast_str_tmp(12, str_appender(&STR_TMP, "ww"))
);
ast_trace(1, "test no variables\n");
test_scope_trace();
SCOPE_EXIT_RTN_VALUE(AST_TEST_PASS, "test no variables\n");
}
AST_TEST_DEFINE(scope_test)
{
SCOPE_TRACE(1, "top %s function\n", "scope_test");
SCOPE_ENTER(1, "top %s function\n", "scope_test");
ast_trace(1, "%s\n", "test outer");
switch (cmd) {
case TEST_INIT:
{
SCOPE_ENTER(1, "TEST_INIT\n");
info->name = "scope_test";
info->category = "/main/logging/";
info->summary = "Scope Trace Tests";
info->description = "Scope Trace Tests";
return AST_TEST_NOT_RUN;
/* need to exit the case scope */
SCOPE_EXIT("TEST_INIT\n");
/* need to exit the function */
SCOPE_EXIT_RTN_VALUE(AST_TEST_NOT_RUN, "BYE\n");
}
case TEST_EXECUTE:
{
SCOPE_TRACE(1, "CASE statement\n");
ast_trace(1, "%s\n", "test case");
}
break;
{
SCOPE_ENTER(1, "TEST_EXECUTE\n");
ast_trace(1, "%s\n", "test execute");
SCOPE_EXIT_EXPR(break, "TEST_EXECUTE\n");
}
default:
ast_test_status_update(test, "Shouldn't have gotten here\n");
return AST_TEST_FAIL;
}
if (1) {
SCOPE_TRACE(1, "IF block\n");
test_scope();
test_scope_enter_function();
}
ast_trace(1);
ast_trace(1, "test no variables\n");
ast_trace(1, "%s\n", "test variable");
return AST_TEST_PASS;
SCOPE_EXIT_RTN_VALUE(AST_TEST_PASS, "Something: %d\n", AST_TEST_PASS);
}
static int unload_module(void)

View File

@@ -583,6 +583,43 @@ AST_TEST_DEFINE(strings_match)
return AST_TEST_PASS;
}
/*!
* \brief Function that needs a temporary ast_str
*/
static const char *str_appender(struct ast_str**buf, char *a)
{
ast_str_append(buf, 0, "<%s>", a);
return ast_str_buffer(*buf);
}
AST_TEST_DEFINE(temp_strings)
{
char *return_buffer = ast_malloc(128);
switch (cmd) {
case TEST_INIT:
info->name = "temp_strings";
info->category = "/main/strings/";
info->summary = "Test ast_str_temp_buffer";
info->description = "Test ast_str_temp_buffer";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
snprintf(return_buffer, 128, "%s %s %s %s %s",
ast_str_tmp(12, str_appender(&STR_TMP, "str1")),
ast_str_tmp(12, str_appender(&STR_TMP, "str2")),
ast_str_tmp(12, str_appender(&STR_TMP, "B")),
"ccccccccccccc",
ast_str_tmp(12, str_appender(&STR_TMP, "ww"))
);
ast_test_validate(test, ast_strings_match(return_buffer, "=", "<str1> <str2> <B> ccccccccccccc <ww>"));
ast_free(return_buffer);
return AST_TEST_PASS;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(str_test);
@@ -592,6 +629,7 @@ static int unload_module(void)
AST_TEST_UNREGISTER(escape_semicolons_test);
AST_TEST_UNREGISTER(escape_test);
AST_TEST_UNREGISTER(strings_match);
AST_TEST_UNREGISTER(temp_strings);
return 0;
}
@@ -604,6 +642,7 @@ static int load_module(void)
AST_TEST_REGISTER(escape_semicolons_test);
AST_TEST_REGISTER(escape_test);
AST_TEST_REGISTER(strings_match);
AST_TEST_REGISTER(temp_strings);
return AST_MODULE_LOAD_SUCCESS;
}