[FL-3363] More descriptive error messages for the log command (#2835)

* More descriptive error messages for the log command
* Log level description improvements
* Log help changes

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Astra 2023-07-11 14:41:16 +03:00 committed by GitHub
parent 14fc960246
commit 8bccfd6fd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 19 deletions

View file

@ -165,24 +165,23 @@ void cli_command_log_tx_callback(const uint8_t* buffer, size_t size, void* conte
furi_stream_buffer_send(context, buffer, size, 0);
}
void cli_command_log_level_set_from_string(FuriString* level) {
if(furi_string_cmpi_str(level, "default") == 0) {
furi_log_set_level(FuriLogLevelDefault);
} else if(furi_string_cmpi_str(level, "none") == 0) {
furi_log_set_level(FuriLogLevelNone);
} else if(furi_string_cmpi_str(level, "error") == 0) {
furi_log_set_level(FuriLogLevelError);
} else if(furi_string_cmpi_str(level, "warn") == 0) {
furi_log_set_level(FuriLogLevelWarn);
} else if(furi_string_cmpi_str(level, "info") == 0) {
furi_log_set_level(FuriLogLevelInfo);
} else if(furi_string_cmpi_str(level, "debug") == 0) {
furi_log_set_level(FuriLogLevelDebug);
} else if(furi_string_cmpi_str(level, "trace") == 0) {
furi_log_set_level(FuriLogLevelTrace);
bool cli_command_log_level_set_from_string(FuriString* level) {
FuriLogLevel log_level;
if(furi_log_level_from_string(furi_string_get_cstr(level), &log_level)) {
furi_log_set_level(log_level);
return true;
} else {
printf("Unknown log level\r\n");
printf("<log> — start logging using the current level from the system settings\r\n");
printf("<log error> — only critical errors and other important messages\r\n");
printf("<log warn> — non-critical errors and warnings including <log error>\r\n");
printf("<log info> — non-critical information including <log warn>\r\n");
printf("<log default> — the default system log level (equivalent to <log info>)\r\n");
printf(
"<log debug> — debug information including <log info> (may impact system performance)\r\n");
printf(
"<log trace> — system traces including <log debug> (may impact system performance)\r\n");
}
return false;
}
void cli_command_log(Cli* cli, FuriString* args, void* context) {
@ -193,12 +192,20 @@ void cli_command_log(Cli* cli, FuriString* args, void* context) {
bool restore_log_level = false;
if(furi_string_size(args) > 0) {
cli_command_log_level_set_from_string(args);
if(!cli_command_log_level_set_from_string(args)) {
furi_stream_buffer_free(ring);
return;
}
restore_log_level = true;
}
const char* current_level;
furi_log_level_to_string(furi_log_get_level(), &current_level);
printf("Current log level: %s\r\n", current_level);
furi_hal_console_set_tx_callback(cli_command_log_tx_callback, ring);
printf("Use <log ?> to list available log levels\r\n");
printf("Press CTRL+C to stop...\r\n");
while(!cli_cmd_interrupt_received(cli)) {
size_t ret = furi_stream_buffer_receive(ring, buffer, CLI_COMMAND_LOG_BUFFER_SIZE, 50);

View file

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,34.0,,
Version,+,34.1,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
@ -1281,6 +1281,8 @@ Function,+,furi_kernel_restore_lock,int32_t,int32_t
Function,+,furi_kernel_unlock,int32_t,
Function,+,furi_log_get_level,FuriLogLevel,
Function,-,furi_log_init,void,
Function,+,furi_log_level_from_string,_Bool,"const char*, FuriLogLevel*"
Function,+,furi_log_level_to_string,_Bool,"FuriLogLevel, const char**"
Function,+,furi_log_print_format,void,"FuriLogLevel, const char*, const char*, ..."
Function,+,furi_log_print_raw_format,void,"FuriLogLevel, const char*, ..."
Function,+,furi_log_set_level,void,FuriLogLevel

1 entry status name type params
2 Version + 34.0 34.1
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
1281 Function + furi_kernel_unlock int32_t
1282 Function + furi_log_get_level FuriLogLevel
1283 Function - furi_log_init void
1284 Function + furi_log_level_from_string _Bool const char*, FuriLogLevel*
1285 Function + furi_log_level_to_string _Bool FuriLogLevel, const char**
1286 Function + furi_log_print_format void FuriLogLevel, const char*, const char*, ...
1287 Function + furi_log_print_raw_format void FuriLogLevel, const char*, ...
1288 Function + furi_log_set_level void FuriLogLevel

View file

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,34.0,,
Version,+,34.1,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
@ -1450,6 +1450,8 @@ Function,+,furi_kernel_restore_lock,int32_t,int32_t
Function,+,furi_kernel_unlock,int32_t,
Function,+,furi_log_get_level,FuriLogLevel,
Function,-,furi_log_init,void,
Function,+,furi_log_level_from_string,_Bool,"const char*, FuriLogLevel*"
Function,+,furi_log_level_to_string,_Bool,"FuriLogLevel, const char**"
Function,+,furi_log_print_format,void,"FuriLogLevel, const char*, const char*, ..."
Function,+,furi_log_print_raw_format,void,"FuriLogLevel, const char*, ..."
Function,+,furi_log_set_level,void,FuriLogLevel

1 entry status name type params
2 Version + 34.0 34.1
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
1450 Function + furi_kernel_unlock int32_t
1451 Function + furi_log_get_level FuriLogLevel
1452 Function - furi_log_init void
1453 Function + furi_log_level_from_string _Bool const char*, FuriLogLevel*
1454 Function + furi_log_level_to_string _Bool FuriLogLevel, const char**
1455 Function + furi_log_print_format void FuriLogLevel, const char*, const char*, ...
1456 Function + furi_log_print_raw_format void FuriLogLevel, const char*, ...
1457 Function + furi_log_set_level void FuriLogLevel

View file

@ -14,6 +14,21 @@ typedef struct {
static FuriLogParams furi_log;
typedef struct {
const char* str;
FuriLogLevel level;
} FuriLogLevelDescription;
static const FuriLogLevelDescription FURI_LOG_LEVEL_DESCRIPTIONS[] = {
{"default", FuriLogLevelDefault},
{"none", FuriLogLevelNone},
{"error", FuriLogLevelError},
{"warn", FuriLogLevelWarn},
{"info", FuriLogLevelInfo},
{"debug", FuriLogLevelDebug},
{"trace", FuriLogLevelTrace},
};
void furi_log_init() {
// Set default logging parameters
furi_log.log_level = FURI_LOG_LEVEL_DEFAULT;
@ -117,3 +132,23 @@ void furi_log_set_timestamp(FuriLogTimestamp timestamp) {
furi_assert(timestamp);
furi_log.timestamp = timestamp;
}
bool furi_log_level_to_string(FuriLogLevel level, const char** str) {
for(size_t i = 0; i < COUNT_OF(FURI_LOG_LEVEL_DESCRIPTIONS); i++) {
if(level == FURI_LOG_LEVEL_DESCRIPTIONS[i].level) {
*str = FURI_LOG_LEVEL_DESCRIPTIONS[i].str;
return true;
}
}
return false;
}
bool furi_log_level_from_string(const char* str, FuriLogLevel* level) {
for(size_t i = 0; i < COUNT_OF(FURI_LOG_LEVEL_DESCRIPTIONS); i++) {
if(strcmp(str, FURI_LOG_LEVEL_DESCRIPTIONS[i].str) == 0) {
*level = FURI_LOG_LEVEL_DESCRIPTIONS[i].level;
return true;
}
}
return false;
}

View file

@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@ -87,6 +88,23 @@ void furi_log_set_puts(FuriLogPuts puts);
*/
void furi_log_set_timestamp(FuriLogTimestamp timestamp);
/** Log level to string
*
* @param[in] level The level
*
* @return The string
*/
bool furi_log_level_to_string(FuriLogLevel level, const char** str);
/** Log level from string
*
* @param[in] str The string
* @param level The level
*
* @return True if success, False otherwise
*/
bool furi_log_level_from_string(const char* str, FuriLogLevel* level);
/** Log methods
*
* @param tag The application tag