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 4f86118bd8
commit 7440fd0397
4 changed files with 232 additions and 24 deletions

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;
}