/src/fwupd/libfwupdplugin/fu-dump.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2017 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuCommon" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include "fu-dump.h" |
12 | | |
13 | | /** |
14 | | * fu_dump_full: |
15 | | * @log_domain: (nullable): optional log domain, typically %G_LOG_DOMAIN |
16 | | * @title: (nullable): optional prefix title |
17 | | * @data: buffer to print |
18 | | * @len: the size of @data |
19 | | * @columns: break new lines after this many bytes |
20 | | * @flags: dump flags, e.g. %FU_DUMP_FLAG_SHOW_ASCII |
21 | | * |
22 | | * Dumps a raw buffer to the screen. |
23 | | * |
24 | | * Since: 1.8.2 |
25 | | **/ |
26 | | void |
27 | | fu_dump_full(const gchar *log_domain, |
28 | | const gchar *title, |
29 | | const guint8 *data, |
30 | | gsize len, |
31 | | guint columns, |
32 | | FuDumpFlags flags) |
33 | 380 | { |
34 | 380 | g_autoptr(GString) str = g_string_new(NULL); |
35 | | |
36 | 380 | g_return_if_fail(columns > 0); |
37 | | |
38 | | /* this is CPU intensive enough to pre-filter here rather than building |
39 | | * the string and handling in a GLogFunc */ |
40 | 380 | if (!g_log_get_debug_enabled()) |
41 | 380 | return; |
42 | | |
43 | | /* optional */ |
44 | 0 | if (title != NULL) |
45 | 0 | g_string_append_printf(str, "%s:", title); |
46 | | |
47 | | /* if more than can fit on one line then start afresh */ |
48 | 0 | if (len > columns || flags & FU_DUMP_FLAG_SHOW_ADDRESSES) { |
49 | 0 | g_string_append(str, "\n"); |
50 | 0 | } else { |
51 | 0 | for (gsize i = str->len; i < 16; i++) |
52 | 0 | g_string_append(str, " "); |
53 | 0 | } |
54 | | |
55 | | /* offset line */ |
56 | 0 | if (flags & FU_DUMP_FLAG_SHOW_ADDRESSES) { |
57 | 0 | g_string_append(str, " │ "); |
58 | 0 | for (gsize i = 0; i < columns; i++) { |
59 | 0 | g_string_append_printf(str, "%02x ", (guint)i); |
60 | 0 | if (flags & FU_DUMP_FLAG_SHOW_ASCII) |
61 | 0 | g_string_append(str, " "); |
62 | 0 | } |
63 | 0 | g_string_append(str, "\n───────┼"); |
64 | 0 | for (gsize i = 0; i < columns; i++) { |
65 | 0 | g_string_append(str, "───"); |
66 | 0 | if (flags & FU_DUMP_FLAG_SHOW_ASCII) |
67 | 0 | g_string_append(str, "────"); |
68 | 0 | } |
69 | 0 | g_string_append_printf(str, "\n0x%04x │ ", (guint)0); |
70 | 0 | } |
71 | | |
72 | | /* print each row */ |
73 | 0 | for (gsize i = 0; i < len; i++) { |
74 | 0 | g_string_append_printf(str, "%02x ", data[i]); |
75 | | |
76 | | /* optionally print ASCII char */ |
77 | 0 | if (flags & FU_DUMP_FLAG_SHOW_ASCII) { |
78 | 0 | if (g_ascii_isprint(data[i])) |
79 | 0 | g_string_append_printf(str, "[%c] ", data[i]); |
80 | 0 | else |
81 | 0 | g_string_append(str, "[?] "); |
82 | 0 | } |
83 | | |
84 | | /* new row required */ |
85 | 0 | if (i > 0 && i != len - 1 && (i + 1) % columns == 0) { |
86 | 0 | g_string_append(str, "\n"); |
87 | 0 | if (flags & FU_DUMP_FLAG_SHOW_ADDRESSES) |
88 | 0 | g_string_append_printf(str, "0x%04x │ ", (guint)i + 1); |
89 | 0 | } |
90 | 0 | } |
91 | 0 | g_log(log_domain, G_LOG_LEVEL_DEBUG, "%s", str->str); |
92 | 0 | } |
93 | | |
94 | | /** |
95 | | * fu_dump_raw: |
96 | | * @log_domain: (nullable): optional log domain, typically %G_LOG_DOMAIN |
97 | | * @title: (nullable): optional prefix title |
98 | | * @data: buffer to print |
99 | | * @len: the size of @data |
100 | | * |
101 | | * Dumps a raw buffer to the screen. |
102 | | * |
103 | | * Since: 1.8.2 |
104 | | **/ |
105 | | void |
106 | | fu_dump_raw(const gchar *log_domain, const gchar *title, const guint8 *data, gsize len) |
107 | 380 | { |
108 | 380 | FuDumpFlags flags = FU_DUMP_FLAG_NONE; |
109 | 380 | if (len > 64) |
110 | 115 | flags |= FU_DUMP_FLAG_SHOW_ADDRESSES; |
111 | 380 | fu_dump_full(log_domain, title, data, len, 32, flags); |
112 | 380 | } |
113 | | |
114 | | /** |
115 | | * fu_dump_bytes: |
116 | | * @log_domain: (nullable): optional log domain, typically %G_LOG_DOMAIN |
117 | | * @title: (nullable): optional prefix title |
118 | | * @bytes: data blob |
119 | | * |
120 | | * Dumps a byte buffer to the screen. |
121 | | * |
122 | | * Since: 1.8.2 |
123 | | **/ |
124 | | void |
125 | | fu_dump_bytes(const gchar *log_domain, const gchar *title, GBytes *bytes) |
126 | 55 | { |
127 | 55 | gsize len = 0; |
128 | 55 | const guint8 *data = g_bytes_get_data(bytes, &len); |
129 | 55 | fu_dump_raw(log_domain, title, data, len); |
130 | 55 | } |