mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-11-03 20:38:59 +00:00 
			
		
		
		
	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
		
	
		
			
				
	
	
		
			127 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * Asterisk -- An open source telephony toolkit.
 | 
						|
 *
 | 
						|
 * Copyright (C) 2020, Sangoma Technologies Corp
 | 
						|
 *
 | 
						|
 * George Joseph <gjoseph@digium.com>
 | 
						|
 *
 | 
						|
 * See http://www.asterisk.org for more information about
 | 
						|
 * the Asterisk project. Please do not directly contact
 | 
						|
 * any of the maintainers of this project for assistance;
 | 
						|
 * the project provides a web site, mailing lists and IRC
 | 
						|
 * channels for your use.
 | 
						|
 *
 | 
						|
 * This program is free software, distributed under the terms of
 | 
						|
 * the GNU General Public License Version 2. See the LICENSE file
 | 
						|
 * at the top of the source tree.
 | 
						|
 */
 | 
						|
 | 
						|
/*!
 | 
						|
 * \file
 | 
						|
 * \brief Test for Scope Trace
 | 
						|
 *
 | 
						|
 * \author\verbatim George Joseph <gjoseph@digium.com> \endverbatim
 | 
						|
 *
 | 
						|
 * tests for Scope Trace
 | 
						|
 * \ingroup tests
 | 
						|
 */
 | 
						|
 | 
						|
/*** MODULEINFO
 | 
						|
	<depend>TEST_FRAMEWORK</depend>
 | 
						|
	<support_level>core</support_level>
 | 
						|
 ***/
 | 
						|
 | 
						|
#include "asterisk.h"
 | 
						|
 | 
						|
#include "asterisk/utils.h"
 | 
						|
#include "asterisk/module.h"
 | 
						|
#include "asterisk/test.h"
 | 
						|
#include "asterisk/logger.h"
 | 
						|
 | 
						|
 | 
						|
static const char *str_appender(struct ast_str**buf, char *a)
 | 
						|
{
 | 
						|
	ast_str_append(buf, 0, "<append %s>", a);
 | 
						|
	return ast_str_buffer(*buf);
 | 
						|
}
 | 
						|
 | 
						|
static void test_scope_trace(void)
 | 
						|
{
 | 
						|
	SCOPE_ENTER(1, "subfunction\n");
 | 
						|
	SCOPE_EXIT_RTN("got out\n");
 | 
						|
}
 | 
						|
 | 
						|
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"))
 | 
						|
		);
 | 
						|
 | 
						|
	test_scope_trace();
 | 
						|
 | 
						|
	SCOPE_EXIT_RTN_VALUE(AST_TEST_PASS, "test no variables\n");
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
AST_TEST_DEFINE(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";
 | 
						|
		/* 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_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_enter_function();
 | 
						|
	}
 | 
						|
 | 
						|
	ast_trace(1);
 | 
						|
	ast_trace(1, "test no variables\n");
 | 
						|
	ast_trace(1, "%s\n", "test variable");
 | 
						|
 | 
						|
	SCOPE_EXIT_RTN_VALUE(AST_TEST_PASS, "Something: %d\n", AST_TEST_PASS);
 | 
						|
}
 | 
						|
 | 
						|
static int unload_module(void)
 | 
						|
{
 | 
						|
	AST_TEST_UNREGISTER(scope_test);
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
static int load_module(void)
 | 
						|
{
 | 
						|
	AST_TEST_REGISTER(scope_test);
 | 
						|
	return AST_MODULE_LOAD_SUCCESS;
 | 
						|
}
 | 
						|
 | 
						|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Scope Trace Test");
 |