mirror of
https://review.coreboot.org/flashrom.git
synced 2025-07-02 22:43:17 +02:00
libflashrom: Update the API for Logger Callback
The initial implementation does not account for user_data, requiring the calling application to use a global scope. This may lead to issues related to object lifecycle management and other architectural concerns. This patch adds user_data to the user’s log callback. Moreover, it performs message formatting, so the application only needs to pass the formatted string to the selected output. The change does not break the existing logging API but extends it. A new API version is introduced with the v2 suffix. Testing: Both unit tests and CLI tools serve as libflashrom clients. All unit tests run successfully. Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39 Signed-off-by: Dmitry Zhadinets <dzhadinets@gmail.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/86875 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:

committed by
Anastasia Klimchuk

parent
90cc93d9bb
commit
b1794138f0
@ -31,6 +31,8 @@
|
||||
|
||||
/** Pointer to log callback function. */
|
||||
static flashrom_log_callback *global_log_callback = NULL;
|
||||
static flashrom_log_callback_v2 *global_log_callback_v2 = NULL;
|
||||
static void *global_log_user_data = NULL;
|
||||
|
||||
int flashrom_init(const int perform_selfcheck)
|
||||
{
|
||||
@ -50,6 +52,49 @@ void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
|
||||
{
|
||||
global_log_callback = log_callback;
|
||||
}
|
||||
|
||||
/** @private */
|
||||
static int format_message_and_invoke_log_callback_v2(enum flashrom_log_level level,
|
||||
const char *format, va_list args)
|
||||
{
|
||||
char message[LOG_MESSAGE_LENGTH_LIMIT] = {0};
|
||||
int actual_len;
|
||||
|
||||
/* sanity check */
|
||||
if (!global_log_callback_v2)
|
||||
return -EINVAL;
|
||||
|
||||
actual_len = vsnprintf(message, sizeof(message), format, args);
|
||||
|
||||
if (actual_len < 0)
|
||||
return actual_len;
|
||||
|
||||
global_log_callback_v2(level, message, global_log_user_data);
|
||||
|
||||
if ((size_t)actual_len >= sizeof(message)) {
|
||||
snprintf(message, sizeof(message),
|
||||
"%zu characters were truncated from the previous log message",
|
||||
(size_t)actual_len - sizeof(message) + 1);
|
||||
global_log_callback_v2(FLASHROM_MSG_WARN, message, global_log_user_data);
|
||||
return -ERANGE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void flashrom_set_log_callback_v2(flashrom_log_callback_v2 *const log_callback, void* user_data)
|
||||
{
|
||||
if (!log_callback) {
|
||||
/* Reset v1 callback only if it was installed by flashrom_set_log_callback_v2 op */
|
||||
if (global_log_callback == format_message_and_invoke_log_callback_v2) {
|
||||
global_log_callback = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
global_log_callback = format_message_and_invoke_log_callback_v2;
|
||||
global_log_callback_v2 = log_callback;
|
||||
global_log_user_data = user_data;
|
||||
}
|
||||
|
||||
/** @private */
|
||||
int print(const enum flashrom_log_level level, const char *const fmt, ...)
|
||||
{
|
||||
|
Reference in New Issue
Block a user