tests: bsim: set log level from command-line argument

Implementation to take command-line arguments to set log level in run
time for bsim module. Set default log level if no log level is passed
at runtime

Signed-off-by: Guru Mehar Rachaputi <gurumeharrachaputi@gmail.com>
This commit is contained in:
Guru Mehar Rachaputi 2024-07-19 17:48:54 +02:00 committed by Carles Cufí
commit f20ac87536
6 changed files with 43 additions and 23 deletions

View file

@ -1,18 +0,0 @@
# Kconfig options for the test
#
# Only used as single point for log level configuration.
# Can be extended with any new kconfig, really.
#
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
menu "Test configuration"
module = APP
module-str = app
source "subsys/logging/Kconfig.template.log_config"
endmenu
source "Kconfig.zephyr"

View file

@ -26,6 +26,7 @@ CONFIG_ASSERT=y
# the test will have a hard time understanding what the problem is if it's
# buried in thousands of lines of debug logs.
CONFIG_LOG=y
CONFIG_LOG_RUNTIME_FILTERING=y
# Those two options together add the thread name in every log print, very useful
# for debugging if you expect the same functions to be called from different
@ -45,7 +46,6 @@ CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y
# It's OK to leave useful debug options commented out, with a short comment
# explaining why they might be useful. That way, someone trying to debug your
# test will get a headstart.
# CONFIG_APP_LOG_LEVEL_DBG=y
# CONFIG_BT_CONN_LOG_LEVEL_DBG=y
# CONFIG_BT_ATT_LOG_LEVEL_DBG=y
# CONFIG_BT_GATT_LOG_LEVEL_DBG=y

View file

@ -12,6 +12,7 @@
#include "testlib/conn.h"
#include "testlib/scan.h"
#include "testlib/log_utils.h"
#include "babblekit/flags.h"
#include "babblekit/sync.h"
@ -20,10 +21,12 @@
/* local includes */
#include "data.h"
LOG_MODULE_REGISTER(dut, CONFIG_APP_LOG_LEVEL);
LOG_MODULE_REGISTER(dut, LOG_LEVEL_DBG);
static DEFINE_FLAG(is_subscribed);
extern unsigned long runtime_log_level;
static void ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
/* assume we only get it for the `test_gatt_service` */
@ -98,6 +101,9 @@ void entrypoint_dut(void)
/* Initialize device sync library */
bk_sync_init();
/* Set the log level given by the `log_level` CLI argument */
bt_testlib_log_level_set("dut", runtime_log_level);
/* Initialize Bluetooth */
err = bt_enable(NULL);
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);

View file

@ -9,11 +9,33 @@
#include "bs_tracing.h"
#include "bstests.h"
#include "babblekit/testcase.h"
#include "testlib/log_utils.h"
extern void entrypoint_dut(void);
extern void entrypoint_peer(void);
extern enum bst_result_t bst_result;
unsigned long runtime_log_level = LOG_LEVEL_INF;
static void test_args(int argc, char *argv[])
{
size_t argn = 0;
const char *arg = argv[argn];
if (strcmp(arg, "log_level") == 0) {
runtime_log_level = strtoul(argv[++argn], NULL, 10);
if (runtime_log_level >= LOG_LEVEL_NONE &&
runtime_log_level <= LOG_LEVEL_DBG){
TEST_PRINT("Runtime log level configuration: %d", runtime_log_level);
} else {
TEST_FAIL("Invalid arguments to set log level: %d", runtime_log_level);
}
} else {
TEST_PRINT("Default runtime log level configuration: INFO");
}
}
static void test_end_cb(void)
{
@ -38,11 +60,13 @@ static const struct bst_test_instance entrypoints[] = {
.test_id = "dut",
.test_delete_f = test_end_cb,
.test_main_f = entrypoint_dut,
.test_args_f = test_args,
},
{
.test_id = "peer",
.test_delete_f = test_end_cb,
.test_main_f = entrypoint_peer,
.test_args_f = test_args,
},
BSTEST_END_MARKER,
};

View file

@ -15,6 +15,7 @@
#include "testlib/att_read.h"
#include "testlib/att_write.h"
#include "testlib/conn.h"
#include "testlib/log_utils.h"
#include "babblekit/flags.h"
#include "babblekit/sync.h"
@ -23,12 +24,14 @@
/* local includes */
#include "data.h"
LOG_MODULE_REGISTER(peer, CONFIG_APP_LOG_LEVEL);
LOG_MODULE_REGISTER(peer, LOG_LEVEL_DBG);
static DEFINE_FLAG(is_subscribed);
static DEFINE_FLAG(got_notification_1);
static DEFINE_FLAG(got_notification_2);
extern unsigned long runtime_log_level;
int find_characteristic(struct bt_conn *conn,
const struct bt_uuid *svc,
const struct bt_uuid *chrc,
@ -166,6 +169,9 @@ void entrypoint_peer(void)
/* Initialize device sync library */
bk_sync_init();
/* Set the log level given by the `log_level` CLI argument */
bt_testlib_log_level_set("peer", runtime_log_level);
/* Initialize Bluetooth */
err = bt_enable(NULL);
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);

View file

@ -62,8 +62,10 @@ cd ${BSIM_OUT_PATH}/bin
# Instantiate all devices in the simulation.
# The `testid` parameter is used to run the right role or procedure (here "dut" vs "tester").
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=dut
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69 -testid=peer
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=dut \
-argstest log_level 3
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69 -testid=peer \
-argstest log_level 3
# Start the PHY. Double-check the `-D` parameter: it has to match the number of
# devices started in the lines above.