Skip to content

Commit f20ac87

Browse files
00thirdeye00carlescufi
authored andcommitted
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 <[email protected]>
1 parent 52ffbd8 commit f20ac87

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

tests/bsim/bluetooth/host/misc/sample_test/Kconfig

-18
This file was deleted.

tests/bsim/bluetooth/host/misc/sample_test/prj.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CONFIG_ASSERT=y
2626
# the test will have a hard time understanding what the problem is if it's
2727
# buried in thousands of lines of debug logs.
2828
CONFIG_LOG=y
29+
CONFIG_LOG_RUNTIME_FILTERING=y
2930

3031
# Those two options together add the thread name in every log print, very useful
3132
# for debugging if you expect the same functions to be called from different
@@ -45,7 +46,6 @@ CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y
4546
# It's OK to leave useful debug options commented out, with a short comment
4647
# explaining why they might be useful. That way, someone trying to debug your
4748
# test will get a headstart.
48-
# CONFIG_APP_LOG_LEVEL_DBG=y
4949
# CONFIG_BT_CONN_LOG_LEVEL_DBG=y
5050
# CONFIG_BT_ATT_LOG_LEVEL_DBG=y
5151
# CONFIG_BT_GATT_LOG_LEVEL_DBG=y

tests/bsim/bluetooth/host/misc/sample_test/src/dut.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "testlib/conn.h"
1414
#include "testlib/scan.h"
15+
#include "testlib/log_utils.h"
1516

1617
#include "babblekit/flags.h"
1718
#include "babblekit/sync.h"
@@ -20,10 +21,12 @@
2021
/* local includes */
2122
#include "data.h"
2223

23-
LOG_MODULE_REGISTER(dut, CONFIG_APP_LOG_LEVEL);
24+
LOG_MODULE_REGISTER(dut, LOG_LEVEL_DBG);
2425

2526
static DEFINE_FLAG(is_subscribed);
2627

28+
extern unsigned long runtime_log_level;
29+
2730
static void ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
2831
{
2932
/* assume we only get it for the `test_gatt_service` */
@@ -98,6 +101,9 @@ void entrypoint_dut(void)
98101
/* Initialize device sync library */
99102
bk_sync_init();
100103

104+
/* Set the log level given by the `log_level` CLI argument */
105+
bt_testlib_log_level_set("dut", runtime_log_level);
106+
101107
/* Initialize Bluetooth */
102108
err = bt_enable(NULL);
103109
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);

tests/bsim/bluetooth/host/misc/sample_test/src/main.c

+24
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,33 @@
99
#include "bs_tracing.h"
1010
#include "bstests.h"
1111
#include "babblekit/testcase.h"
12+
#include "testlib/log_utils.h"
1213

1314
extern void entrypoint_dut(void);
1415
extern void entrypoint_peer(void);
1516
extern enum bst_result_t bst_result;
1617

18+
unsigned long runtime_log_level = LOG_LEVEL_INF;
19+
20+
static void test_args(int argc, char *argv[])
21+
{
22+
size_t argn = 0;
23+
const char *arg = argv[argn];
24+
25+
if (strcmp(arg, "log_level") == 0) {
26+
27+
runtime_log_level = strtoul(argv[++argn], NULL, 10);
28+
29+
if (runtime_log_level >= LOG_LEVEL_NONE &&
30+
runtime_log_level <= LOG_LEVEL_DBG){
31+
TEST_PRINT("Runtime log level configuration: %d", runtime_log_level);
32+
} else {
33+
TEST_FAIL("Invalid arguments to set log level: %d", runtime_log_level);
34+
}
35+
} else {
36+
TEST_PRINT("Default runtime log level configuration: INFO");
37+
}
38+
}
1739

1840
static void test_end_cb(void)
1941
{
@@ -38,11 +60,13 @@ static const struct bst_test_instance entrypoints[] = {
3860
.test_id = "dut",
3961
.test_delete_f = test_end_cb,
4062
.test_main_f = entrypoint_dut,
63+
.test_args_f = test_args,
4164
},
4265
{
4366
.test_id = "peer",
4467
.test_delete_f = test_end_cb,
4568
.test_main_f = entrypoint_peer,
69+
.test_args_f = test_args,
4670
},
4771
BSTEST_END_MARKER,
4872
};

tests/bsim/bluetooth/host/misc/sample_test/src/peer.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "testlib/att_read.h"
1616
#include "testlib/att_write.h"
1717
#include "testlib/conn.h"
18+
#include "testlib/log_utils.h"
1819

1920
#include "babblekit/flags.h"
2021
#include "babblekit/sync.h"
@@ -23,12 +24,14 @@
2324
/* local includes */
2425
#include "data.h"
2526

26-
LOG_MODULE_REGISTER(peer, CONFIG_APP_LOG_LEVEL);
27+
LOG_MODULE_REGISTER(peer, LOG_LEVEL_DBG);
2728

2829
static DEFINE_FLAG(is_subscribed);
2930
static DEFINE_FLAG(got_notification_1);
3031
static DEFINE_FLAG(got_notification_2);
3132

33+
extern unsigned long runtime_log_level;
34+
3235
int find_characteristic(struct bt_conn *conn,
3336
const struct bt_uuid *svc,
3437
const struct bt_uuid *chrc,
@@ -166,6 +169,9 @@ void entrypoint_peer(void)
166169
/* Initialize device sync library */
167170
bk_sync_init();
168171

172+
/* Set the log level given by the `log_level` CLI argument */
173+
bt_testlib_log_level_set("peer", runtime_log_level);
174+
169175
/* Initialize Bluetooth */
170176
err = bt_enable(NULL);
171177
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);

tests/bsim/bluetooth/host/misc/sample_test/test_scripts/run.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ cd ${BSIM_OUT_PATH}/bin
6262

6363
# Instantiate all devices in the simulation.
6464
# The `testid` parameter is used to run the right role or procedure (here "dut" vs "tester").
65-
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=dut
66-
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69 -testid=peer
65+
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=dut \
66+
-argstest log_level 3
67+
Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69 -testid=peer \
68+
-argstest log_level 3
6769

6870
# Start the PHY. Double-check the `-D` parameter: it has to match the number of
6971
# devices started in the lines above.

0 commit comments

Comments
 (0)