This sample has a log module of its own.
Its Kconfig sources the log level template, which defines
CONFIG_SAMPLE_LOG_LEVEL and CONFIG_SAMPLE_LOG_LEVEL_RUNTIME; prj.conf
picks the debug level, and src/sensor.c joins the module with
PBL_LOG_MODULE_DECLARE().
It logs once at each level: the verbose message is dropped. With a runtime
level, PBL_LOG_MODULE_LEVEL_SET() then raises it to warnings, and only the
error and the warning remain. Without one, the level is fixed when building,
and messages above it are not even in the image.
pbl samples samples/subsys/logging/levels
every board
mainmenu "Logging sample"
source "Kconfig.pebbleos"
module = SAMPLE
module-str = Sample
source "subsys/logging/Kconfig.template.log_level"
CONFIG_CONSOLE=y
CONFIG_CONSOLE_STDOUT=y
CONFIG_LOG=y
CONFIG_SAMPLE_LOG_LEVEL_DEBUG=y
CONFIG_SAMPLE_LOG_LEVEL_RUNTIME=y
#include "sensor.h"
#include <stdint.h>
#include <stdio.h>
#include <pbl/logging/logging.h>
/* The sample's Kconfig defines CONFIG_SAMPLE_LOG_LEVEL from the log level template. */
PBL_LOG_MODULE_DEFINE(sample, CONFIG_SAMPLE_LOG_LEVEL);
static void prv_log_each_level(void) {
PBL_LOG_ERR("an error");
PBL_LOG_WRN("a warning");
PBL_LOG_INFO("an event worth attention");
PBL_LOG_DBG("a routine state change");
PBL_LOG_VERBOSE("a detail");
}
int main(void) {
uint8_t data[16];
/* The module starts at its configured level, debug: verbose messages are dropped. */
prv_log_each_level();
if (sensor_read(data, sizeof(data)) == 0) {
PBL_LOG_HEXDUMP_INFO(data, sizeof(data));
}
(void)sensor_read(data, 32U);
/* Built with CONFIG_SAMPLE_LOG_LEVEL_RUNTIME, the level can change while running. */
PBL_LOG_MODULE_LEVEL_SET(sample, LOG_LEVEL_WARNING);
PBL_LOG_WRN("level now %d, warning", PBL_LOG_MODULE_LEVEL_GET(sample));
prv_log_each_level();
printf("Sample done\n");
return 0;
}
#include "sensor.h"
#include <errno.h>
#include <pbl/logging/logging.h>
/* Another file of the same module: it shares its level. */
PBL_LOG_MODULE_DECLARE(sample, CONFIG_SAMPLE_LOG_LEVEL);
int sensor_read(uint8_t *buf, size_t len) {
if (len > 16U) {
PBL_LOG_WRN("read of %zu bytes, at most 16", len);
return -EINVAL;
}
for (size_t i = 0; i < len; i++) {
buf[i] = (uint8_t)(0xa0U + i);
}
PBL_LOG_DBG("read %zu bytes", len);
return 0;
}
E .* sample: an error
W .* sample: a warning
I .* sample: an event worth attention
D .* sample: a routine state change
D .* sample: read 16 bytes
I .* 0000 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
W .* sample: read of 32 bytes, at most 16
W .* sample: level now 50, warning
E .* sample: an error
W .* sample: a warning
Sample done
Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/logging/levels.md.