diff --git a/doc/libfprint-2-sections.txt b/doc/libfprint-2-sections.txt index cb67aef5..e6bb0ef3 100644 --- a/doc/libfprint-2-sections.txt +++ b/doc/libfprint-2-sections.txt @@ -348,6 +348,8 @@ fpi_image_device_set_bz3_threshold
fpi-log fp_dbg +fp_dbg_hex_dump_bytes +fp_dbg_hex_dump_data fp_info fp_warn fp_err diff --git a/libfprint/fpi-log.c b/libfprint/fpi-log.c new file mode 100644 index 00000000..0bc4088a --- /dev/null +++ b/libfprint/fpi-log.c @@ -0,0 +1,74 @@ +/* + * FpiLog - Internal logging functions + * Copyright (C) 2020 Benjamin Berg + * Copyright (C) 2025 Joshua Grisham + * Copyright (C) 2026 Marco Trevisan (Treviño) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "fpi-log.h" + +/** + * fpi_dbg_hex_dump_data: + * @buf: Bytes buffer to dump + * @len: Length of @buf to dump + * + * Prints hex dump of @buf to fp_dbg() + */ +void + (fpi_dbg_hex_dump_data) (const gchar *log_domain, + const guint8 *buf, + gsize len) +{ + g_autoptr(GString) line = NULL; + + if (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, log_domain)) + return; + + if (G_UNLIKELY (len == 0 || !buf)) + return; + + line = g_string_new (""); + + for (gint i = 0; i < len; i++) + { + g_string_append_printf (line, "%02x ", buf[i]); + if ((i + 1) % 16 == 0) + { + g_log (log_domain, G_LOG_LEVEL_DEBUG, "%s", line->str); + g_string_set_size (line, 0); + } + } + + if (line->len) + g_log (log_domain, G_LOG_LEVEL_DEBUG, "%s", line->str); +} + +/** + * fpi_dbg_hex_dump_bytes: + * @bytes: #GBytes to dump + * + * Prints hex dump of @bytes to fp_dbg() + */ +void + (fpi_dbg_hex_dump_bytes) (const gchar *log_domain, + GBytes *bytes) +{ + gsize length = 0; + const guint8 *data = g_bytes_get_data (bytes, &length); + + (fpi_dbg_hex_dump_data) (log_domain, data, length); +} diff --git a/libfprint/fpi-log.h b/libfprint/fpi-log.h index cd8f1bd5..cbc7026b 100644 --- a/libfprint/fpi-log.h +++ b/libfprint/fpi-log.h @@ -1,6 +1,7 @@ /* * Copyright (C) 2007-2008 Daniel Drake * Copyright (C) 2018 Bastien Nocera + * Copyright (C) 2026 Marco Trevisan (Treviño) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -96,3 +97,30 @@ * Same as BUG_ON() but is always true. */ #define BUG() BUG_ON (1) + +void (fpi_dbg_hex_dump_data) (const gchar *log_domain, + const guint8 *buf, + gsize len); + +/** + * fp_dbg_hex_dump_data: + * @buf: Bytes buffer to dump + * @len: Length of @buf to dump + * + * Prints hex dump of @buf to fp_dbg() + */ +#define fp_dbg_hex_dump_data(buf, len) \ + (fpi_dbg_hex_dump_data) (G_LOG_DOMAIN, (buf), (len)) + +void (fpi_dbg_hex_dump_bytes) (const gchar *log_domain, + GBytes *bytes); + +/** + * fp_dbg_hex_dump_bytes: + * @bytes: #GBytes to dump + * + * Prints hex dump of @bytes to fp_dbg() + */ +#define fp_dbg_hex_dump_bytes(bytes) \ + (fpi_dbg_hex_dump_bytes) \ + (G_LOG_DOMAIN, (bytes)) diff --git a/libfprint/fpi-spi-transfer.c b/libfprint/fpi-spi-transfer.c index f5d2617d..79931ae4 100644 --- a/libfprint/fpi-spi-transfer.c +++ b/libfprint/fpi-spi-transfer.c @@ -18,6 +18,7 @@ */ #include "fpi-spi-transfer.h" +#include "fpi-log.h" #include #include #include @@ -48,27 +49,6 @@ static gsize block_size = 0; G_DEFINE_BOXED_TYPE (FpiSpiTransfer, fpi_spi_transfer, fpi_spi_transfer_ref, fpi_spi_transfer_unref) -static void -dump_buffer (guchar *buf, gssize dump_len) -{ - g_autoptr(GString) line = NULL; - - line = g_string_new (""); - /* Dump the buffer. */ - for (gssize i = 0; i < dump_len; i++) - { - g_string_append_printf (line, "%02x ", buf[i]); - if ((i + 1) % 16 == 0) - { - g_debug ("%s", line->str); - g_string_set_size (line, 0); - } - } - - if (line->len) - g_debug ("%s", line->str); -} - static void log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error) { @@ -82,7 +62,7 @@ log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error) transfer->length_rd); if (transfer->buffer_wr) - dump_buffer (transfer->buffer_wr, transfer->length_wr); + fp_dbg_hex_dump_data (transfer->buffer_wr, transfer->length_wr); } else { @@ -98,7 +78,7 @@ log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error) transfer->length_wr, transfer->length_rd); if (transfer->buffer_rd) - dump_buffer (transfer->buffer_rd, transfer->length_rd); + fp_dbg_hex_dump_data (transfer->buffer_rd, transfer->length_rd); } } } diff --git a/libfprint/fpi-usb-transfer.c b/libfprint/fpi-usb-transfer.c index 448c5664..721a2510 100644 --- a/libfprint/fpi-usb-transfer.c +++ b/libfprint/fpi-usb-transfer.c @@ -65,25 +65,9 @@ log_transfer (FpiUsbTransfer *transfer, gboolean submit, GError *error) if (!submit == !!(transfer->endpoint & FPI_USB_ENDPOINT_IN)) { - g_autoptr(GString) line = NULL; - gssize dump_len; - - dump_len = (transfer->endpoint & FPI_USB_ENDPOINT_IN) ? transfer->actual_length : transfer->length; - - line = g_string_new (""); - /* Dump the buffer. */ - for (gint i = 0; i < dump_len; i++) - { - g_string_append_printf (line, "%02x ", transfer->buffer[i]); - if ((i + 1) % 16 == 0) - { - g_debug ("%s", line->str); - g_string_set_size (line, 0); - } - } - - if (line->len) - g_debug ("%s", line->str); + fp_dbg_hex_dump_data (transfer->buffer, + (transfer->endpoint & FPI_USB_ENDPOINT_IN) ? + transfer->actual_length : transfer->length); } } } diff --git a/libfprint/meson.build b/libfprint/meson.build index 352a3759..093b9328 100644 --- a/libfprint/meson.build +++ b/libfprint/meson.build @@ -21,6 +21,7 @@ libfprint_private_sources = [ 'fpi-device.c', 'fpi-image-device.c', 'fpi-image.c', + 'fpi-log.c', 'fpi-print.c', 'fpi-ssm.c', 'fpi-usb-transfer.c',