mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-26 22:52:34 +02:00
libflashrom: Add set log level functionality
Before this commit, any message from Flashrom would trigger the user's callback. This could lead to additional delays and slow down overall Flashrom performance. This patch adds the ability to configure the log level for messages from Flashrom. It sets the default log level to INFO Testing: Both unit tests and CLI tools serve as libflashrom clients. All unit tests run successfully. Change-Id: I095d48b8feb5fbc950a36eb17bed0d7cb8d9df64 Signed-off-by: Dmitry Zhadinets <dzhadinets@gmail.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/87047 Reviewed-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-by: Peter Marheine <pmarheine@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
parent
b1794138f0
commit
6571f263b5
@ -57,6 +57,20 @@ enum flashrom_log_level {
|
|||||||
FLASHROM_MSG_SPEW = 5,
|
FLASHROM_MSG_SPEW = 5,
|
||||||
};
|
};
|
||||||
typedef int(flashrom_log_callback)(enum flashrom_log_level, const char *format, va_list);
|
typedef int(flashrom_log_callback)(enum flashrom_log_level, const char *format, va_list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the log level.
|
||||||
|
*
|
||||||
|
* Set a log level for messages from libflashrom.
|
||||||
|
* The print callback will be invoked only for messages
|
||||||
|
* with a log level less than or equal to the configured value.
|
||||||
|
* The default log level is FLASHROM_MSG_INFO.
|
||||||
|
*
|
||||||
|
* @param level The log level to be set.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void flashrom_set_log_level(enum flashrom_log_level level);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the log callback function.
|
* @brief Set the log callback function.
|
||||||
*
|
*
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (C) 2012, 2016 secunet Security Networks AG
|
* Copyright (C) 2012, 2016 secunet Security Networks AG
|
||||||
* (Written by Nico Huber <nico.huber@secunet.com> for secunet)
|
* (Written by Nico Huber <nico.huber@secunet.com> for secunet)
|
||||||
|
* Copyright (C) 2025 Dmitry Zhadinets <dzhadinets@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -33,6 +34,7 @@
|
|||||||
static flashrom_log_callback *global_log_callback = NULL;
|
static flashrom_log_callback *global_log_callback = NULL;
|
||||||
static flashrom_log_callback_v2 *global_log_callback_v2 = NULL;
|
static flashrom_log_callback_v2 *global_log_callback_v2 = NULL;
|
||||||
static void *global_log_user_data = NULL;
|
static void *global_log_user_data = NULL;
|
||||||
|
static enum flashrom_log_level global_log_level = FLASHROM_MSG_INFO;
|
||||||
|
|
||||||
int flashrom_init(const int perform_selfcheck)
|
int flashrom_init(const int perform_selfcheck)
|
||||||
{
|
{
|
||||||
@ -46,8 +48,11 @@ int flashrom_shutdown(void)
|
|||||||
return 0; /* TODO: nothing to do? */
|
return 0; /* TODO: nothing to do? */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: flashrom_set_loglevel()? do we need it?
|
void flashrom_set_log_level(enum flashrom_log_level level)
|
||||||
For now, let the user decide in their callback. */
|
{
|
||||||
|
global_log_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
|
void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
|
||||||
{
|
{
|
||||||
global_log_callback = log_callback;
|
global_log_callback = log_callback;
|
||||||
@ -98,7 +103,7 @@ void flashrom_set_log_callback_v2(flashrom_log_callback_v2 *const log_callback,
|
|||||||
/** @private */
|
/** @private */
|
||||||
int print(const enum flashrom_log_level level, const char *const fmt, ...)
|
int print(const enum flashrom_log_level level, const char *const fmt, ...)
|
||||||
{
|
{
|
||||||
if (global_log_callback) {
|
if (global_log_callback && level <= global_log_level) {
|
||||||
int ret;
|
int ret;
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
|
@ -23,26 +23,22 @@
|
|||||||
static int test_log_callback(enum flashrom_log_level level, const char *format,
|
static int test_log_callback(enum flashrom_log_level level, const char *format,
|
||||||
va_list vargs)
|
va_list vargs)
|
||||||
{
|
{
|
||||||
/* check that loglevel has passed corectly */
|
|
||||||
assert_int_equal(level, FLASHROM_MSG_INFO);
|
|
||||||
char message[3] = {0};
|
char message[3] = {0};
|
||||||
vsnprintf(message, 3, format, vargs);
|
vsnprintf(message, 3, format, vargs);
|
||||||
assert_string_equal(message, "1\n");
|
assert_string_equal(message, "1\n");
|
||||||
return 0x666 + 1;
|
return 0x666 + (int)level;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_log_callback_v2(enum flashrom_log_level level,
|
static void test_log_callback_v2(enum flashrom_log_level level,
|
||||||
const char *message, void *user_data)
|
const char *message, void *user_data)
|
||||||
{
|
{
|
||||||
/* check that loglevel has passed corectly */
|
|
||||||
assert_int_equal(level, FLASHROM_MSG_ERROR);
|
|
||||||
/* check that user dta has passed */
|
/* check that user dta has passed */
|
||||||
assert_ptr_not_equal(user_data, 0);
|
assert_ptr_not_equal(user_data, 0);
|
||||||
/* check that user_data is correct */
|
/* check that user_data is correct */
|
||||||
assert_int_equal(*(int *)(user_data), 100500);
|
assert_int_equal(*(int *)(user_data), 100500);
|
||||||
/* check that format is working correctly */
|
/* check that format is working correctly */
|
||||||
assert_string_equal(message, "2\n");
|
assert_string_equal(message, "2\n");
|
||||||
*(int*)user_data = 0x666 + 2;
|
*(int*)user_data = 0x666 + (int)level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void flashrom_set_log_callback_test_success(void **state)
|
void flashrom_set_log_callback_test_success(void **state)
|
||||||
@ -50,7 +46,7 @@ void flashrom_set_log_callback_test_success(void **state)
|
|||||||
(void)state; /* unused */
|
(void)state; /* unused */
|
||||||
flashrom_set_log_callback(test_log_callback);
|
flashrom_set_log_callback(test_log_callback);
|
||||||
/* check that callback is called */
|
/* check that callback is called */
|
||||||
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + 1);
|
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_INFO);
|
||||||
flashrom_set_log_callback(NULL);
|
flashrom_set_log_callback(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +57,43 @@ void flashrom_set_log_callback_v2_test_success(void **state)
|
|||||||
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data);
|
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data);
|
||||||
print(FLASHROM_MSG_ERROR, "2%s", "\n");
|
print(FLASHROM_MSG_ERROR, "2%s", "\n");
|
||||||
/* check that callback is called */
|
/* check that callback is called */
|
||||||
assert_int_equal(user_data, 0x666 + 2);
|
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_ERROR);
|
||||||
flashrom_set_log_callback_v2(NULL, NULL);
|
flashrom_set_log_callback_v2(NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void flashrom_set_log_level_test_success(void **state)
|
||||||
|
{
|
||||||
|
(void)state; /* unused */
|
||||||
|
int user_data;
|
||||||
|
|
||||||
|
flashrom_set_log_level(FLASHROM_MSG_WARN);
|
||||||
|
|
||||||
|
/* v2 API check */
|
||||||
|
user_data = 100500;
|
||||||
|
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data);
|
||||||
|
print(FLASHROM_MSG_DEBUG, "2%s", "\n");
|
||||||
|
/* check that callback is not called */
|
||||||
|
assert_int_equal(user_data, 100500);
|
||||||
|
print(FLASHROM_MSG_ERROR, "2%s", "\n");
|
||||||
|
/* check that callback is called for less */
|
||||||
|
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_ERROR);
|
||||||
|
user_data = 100500;
|
||||||
|
print(FLASHROM_MSG_WARN, "2%s", "\n");
|
||||||
|
/* check that callback is called for equal */
|
||||||
|
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_WARN);
|
||||||
|
|
||||||
|
/* v1 API check */
|
||||||
|
flashrom_set_log_callback(test_log_callback);
|
||||||
|
|
||||||
|
/* check that callback is not called */
|
||||||
|
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0);
|
||||||
|
/* check that callback is called for equal */
|
||||||
|
assert_int_equal(print(FLASHROM_MSG_WARN, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_WARN);
|
||||||
|
/* check that callback is called for less*/
|
||||||
|
assert_int_equal(print(FLASHROM_MSG_ERROR, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_ERROR);
|
||||||
|
|
||||||
|
flashrom_set_log_level(FLASHROM_MSG_INFO);
|
||||||
|
/* check that callback is called after the change*/
|
||||||
|
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_INFO);
|
||||||
|
flashrom_set_log_callback(NULL);
|
||||||
|
}
|
@ -481,6 +481,7 @@ int main(int argc, char *argv[])
|
|||||||
const struct CMUnitTest libflashrom_tests[] = {
|
const struct CMUnitTest libflashrom_tests[] = {
|
||||||
cmocka_unit_test(flashrom_set_log_callback_test_success),
|
cmocka_unit_test(flashrom_set_log_callback_test_success),
|
||||||
cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
|
cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
|
||||||
|
cmocka_unit_test(flashrom_set_log_level_test_success),
|
||||||
};
|
};
|
||||||
ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);
|
ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ void flashbuses_to_text_test_success(void **state);
|
|||||||
/* libflashrom.c */
|
/* libflashrom.c */
|
||||||
void flashrom_set_log_callback_test_success(void **state);
|
void flashrom_set_log_callback_test_success(void **state);
|
||||||
void flashrom_set_log_callback_v2_test_success(void **state);
|
void flashrom_set_log_callback_v2_test_success(void **state);
|
||||||
|
void flashrom_set_log_level_test_success(void **state);
|
||||||
|
|
||||||
/* spi25.c */
|
/* spi25.c */
|
||||||
void spi_write_enable_test_success(void **state);
|
void spi_write_enable_test_success(void **state);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user