Skip to content

Commit 7357f61

Browse files
ideasman42phkaeser
authored andcommitted
Apps: use bs_log for error reporting in graph dock-apps.
- Replace fprintf(stderr, ...) with bs_log() for consistent logging. - Remove panic_fn callback in favor of direct bs_log. - Use BS_MIN/BS_MAX from libbase instead of local MIN2/MAX2 macros. - Remove apps/wlm_graph_utildefines.h
1 parent 9ccc540 commit 7357f61

File tree

6 files changed

+46
-118
lines changed

6 files changed

+46
-118
lines changed

apps/wlm_graph_shared.c

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <string.h>
3434

3535
#include "libwlclient/icon.h"
36-
#include "wlm_graph_utildefines.h"
3736

3837
/* == Internal definitions ================================================= */
3938

@@ -171,10 +170,6 @@ typedef struct {
171170

172171
/* == Forward declarations ================================================= */
173172

174-
#ifdef __GNUC__
175-
__attribute__((noreturn))
176-
#endif
177-
static void _wlm_graph_panic(const char *msg);
178173
static void _wlm_graph_sample_values_free(wlm_graph_sample_t *samples, const uint32_t count);
179174
static void _wlm_graph_samples_init(wlm_graph_sample_t *samples, const uint32_t count);
180175
static void _wlm_graph_sample_compute_peak(
@@ -246,8 +241,8 @@ static bool _wlm_graph_arg_parse_i32(
246241
errno = 0;
247242
const long val = strtol(str, &endptr, 10);
248243
if ('\0' != *endptr || 0 != errno || val < val_min || val > val_max) {
249-
fprintf(stderr, "Error: %s value '%s' must be %d-%d\n",
250-
opt_name, str, val_min, val_max);
244+
bs_log(BS_ERROR, "Error: %s value '%s' must be %d-%d\n",
245+
opt_name, str, val_min, val_max);
251246
return false;
252247
}
253248
*result_ptr = (int32_t)val;
@@ -277,8 +272,7 @@ static bool _wlm_graph_arg_parse_f64(
277272
errno = 0;
278273
const double val = strtod(str, &endptr);
279274
if ('\0' != *endptr || 0 != errno || val < val_min || val > val_max) {
280-
fprintf(stderr, "Error: %s value '%s' must be %g-%g\n",
281-
opt_name, str, val_min, val_max);
275+
bs_log(BS_ERROR, "%s value '%s' must be %g-%g", opt_name, str, val_min, val_max);
282276
return false;
283277
}
284278
*result_ptr = val;
@@ -334,7 +328,7 @@ static bool _wlm_graph_arg_parse_font(
334328
// Extract font name (everything before first colon, or entire string).
335329
const size_t name_len = (NULL != colon) ? (size_t)(colon - str) : strlen(str);
336330
if (0 == name_len || name_len >= WLM_GRAPH_FONT_FACE_MAX) {
337-
fprintf(stderr, "Error: %s font name too long or empty\n", opt_name);
331+
bs_log(BS_ERROR, "%s font name too long or empty", opt_name);
338332
return false;
339333
}
340334
memcpy(font->face, str, name_len);
@@ -349,7 +343,7 @@ static bool _wlm_graph_arg_parse_font(
349343
if (_str_match_prefix(&pos, &len, "size=")) {
350344
char size_buf[16];
351345
if (len >= sizeof(size_buf)) {
352-
fprintf(stderr, "Error: %s size value too long\n", opt_name);
346+
bs_log(BS_ERROR, "%s size value too long", opt_name);
353347
return false;
354348
}
355349
memcpy(size_buf, pos, len);
@@ -366,7 +360,7 @@ static bool _wlm_graph_arg_parse_font(
366360
} else if (_str_match_prefix(&pos, &len, "bold") && 0 == len) {
367361
font->weight = CAIRO_FONT_WEIGHT_BOLD;
368362
} else {
369-
fprintf(stderr, "Error: %s weight must be 'normal' or 'bold'\n", opt_name);
363+
bs_log(BS_ERROR, "%s weight must be 'normal' or 'bold'", opt_name);
370364
return false;
371365
}
372366
} else if (_str_match_prefix(&pos, &len, "slant=")) {
@@ -377,11 +371,11 @@ static bool _wlm_graph_arg_parse_font(
377371
} else if (_str_match_prefix(&pos, &len, "oblique") && 0 == len) {
378372
font->slant = CAIRO_FONT_SLANT_OBLIQUE;
379373
} else {
380-
fprintf(stderr, "Error: %s slant must be 'normal', 'italic', or 'oblique'\n", opt_name);
374+
bs_log(BS_ERROR, "%s slant must be 'normal', 'italic', or 'oblique'", opt_name);
381375
return false;
382376
}
383377
} else if (len > 0) {
384-
fprintf(stderr, "Error: Unknown %s option starting with '%.20s'\n", opt_name, pos);
378+
bs_log(BS_ERROR, "Unknown %s option starting with '%.20s'", opt_name, pos);
385379
return false;
386380
}
387381
}
@@ -456,15 +450,13 @@ static void _wlm_graph_pixel_lut_init(
456450
* @param graph_state Graph state to resize.
457451
* @param size New icon dimensions [width, height].
458452
* @param margin_logical_px Margin in logical pixels (at base icon size).
459-
* @param panic_fn Function to call on allocation failure.
460453
*
461454
* @return true if buffers were resized, false if dimensions are too small.
462455
*/
463456
static bool _wlm_graph_buffers_resize(
464457
wlm_graph_state_t *graph_state,
465458
const uint32_t size[2],
466-
const uint32_t margin_logical_px,
467-
void (*panic_fn)(const char *msg))
459+
const uint32_t margin_logical_px)
468460
{
469461
// Calculate inner dimensions (graph area inside bezel).
470462
const uint32_t margin_px = (margin_logical_px * size[0]) / WLM_GRAPH_BASE_ICON_SIZE;
@@ -493,7 +485,7 @@ static bool _wlm_graph_buffers_resize(
493485
// Allocate sample buffer (values allocated lazily during capture).
494486
graph_state->samples_alloc = calloc(inner_size[0], sizeof(wlm_graph_sample_t));
495487
if (NULL == graph_state->samples_alloc) {
496-
panic_fn("Failed to allocate sample buffer");
488+
bs_log(BS_FATAL | BS_ERRNO, "Failed to allocate sample buffer");
497489
}
498490

499491
// Initialize circular doubly-linked list.
@@ -511,13 +503,13 @@ static bool _wlm_graph_buffers_resize(
511503
// Allocate graph data buffer.
512504
graph_state->graph_pixels = malloc(sizeof(uint32_t) * inner_size[0] * inner_size[1]);
513505
if (NULL == graph_state->graph_pixels) {
514-
panic_fn("Failed to allocate graph data buffer");
506+
bs_log(BS_FATAL | BS_ERRNO, "Failed to allocate graph data buffer");
515507
}
516508

517509
// Allocate row counts scratch buffer.
518510
graph_state->row_counts = malloc(sizeof(uint32_t) * inner_size[1]);
519511
if (NULL == graph_state->row_counts) {
520-
panic_fn("Failed to allocate row counts buffer");
512+
bs_log(BS_FATAL | BS_ERRNO, "Failed to allocate row counts buffer");
521513
}
522514

523515
graph_state->graph_size[0] = inner_size[0];
@@ -665,7 +657,7 @@ static uint32_t _wlm_graph_column_render(
665657
if (0 == usage) continue;
666658

667659
cumulative += usage;
668-
const uint8_t level = (uint8_t)MIN2(cumulative, 255);
660+
const uint8_t level = (uint8_t)BS_MIN(cumulative, 255U);
669661

670662
// Calculate topmost line from cumulative usage.
671663
const uint32_t y_top = _wlm_graph_usage_to_y(level, graph_size[1]);
@@ -882,7 +874,7 @@ static bool _wlm_graph_bezel_draw(bs_gfxbuf_t *gfxbuf_ptr, const uint32_t margin
882874

883875
// Offset from edge to bezel position (scales with icon size).
884876
const uint32_t bezel_offset = ((margin_logical_px - 1) * size[0]) / WLM_GRAPH_BASE_ICON_SIZE;
885-
const uint32_t bezel_line_width = MAX2(size[0] / WLM_GRAPH_BASE_ICON_SIZE, 1);
877+
const uint32_t bezel_line_width = BS_MAX(size[0] / WLM_GRAPH_BASE_ICON_SIZE, 1U);
886878

887879
wlm_primitives_draw_bezel_at(
888880
cairo_ptr,
@@ -1041,12 +1033,12 @@ static void _wlm_graph_sample_compute_peak(
10411033
for (uint32_t i = 0; i < values_num; i++) {
10421034
total += values[i];
10431035
}
1044-
sample->value_peak = (uint8_t)MIN2(total, 255);
1036+
sample->value_peak = (uint8_t)BS_MIN(total, 255U);
10451037
} else {
10461038
// Independent: peak is maximum of all values.
10471039
uint8_t max_val = 0;
10481040
for (uint32_t i = 0; i < values_num; i++) {
1049-
max_val = MAX2(max_val, values[i]);
1041+
max_val = BS_MAX(max_val, values[i]);
10501042
}
10511043
sample->value_peak = max_val;
10521044
}
@@ -1184,8 +1176,8 @@ static int _wlm_graph_args_parse(
11841176
} else if (0 == strcmp(argv[i + 1], "heat")) {
11851177
prefs->color_mode = WLM_GRAPH_COLOR_MODE_HEAT;
11861178
} else {
1187-
fprintf(stderr, "Error: %s value '%s' must be 'alpha' or 'heat'\n",
1188-
argv[i], argv[i + 1]);
1179+
bs_log(BS_ERROR, "%s value '%s' must be 'alpha' or 'heat'",
1180+
argv[i], argv[i + 1]);
11891181
return -1;
11901182
}
11911183
i++;
@@ -1213,8 +1205,8 @@ static int _wlm_graph_args_parse(
12131205
printf(" --help, -h Show this help\n");
12141206
return 1;
12151207
} else {
1216-
fprintf(stderr, "Error: Unknown argument '%s'\n", argv[i]);
1217-
fprintf(stderr, "Try '%s --help' for usage.\n", app_name);
1208+
bs_log(BS_ERROR, "Unknown argument '%s'", argv[i]);
1209+
bs_log(BS_ERROR, "Try '%s --help' for usage.", app_name);
12181210
return -1;
12191211
}
12201212
}
@@ -1246,7 +1238,7 @@ static bool _wlm_graph_icon_render_callback(bs_gfxbuf_t *gfxbuf_ptr, void *ud_pt
12461238

12471239
// Reset graph buffers if icon size changed.
12481240
if (size_changed) {
1249-
if (!_wlm_graph_buffers_resize(gs, size, margin_logical_px, _wlm_graph_panic)) {
1241+
if (!_wlm_graph_buffers_resize(gs, size, margin_logical_px)) {
12501242
bs_log(BS_ERROR, "Failed to reset graph buffers for %ux%u icon",
12511243
size[0], size[1]);
12521244
return false;
@@ -1381,21 +1373,6 @@ static void _wlm_graph_timer_callback(wlclient_t *client_ptr, void *ud_ptr)
13811373

13821374
/* == Internal state management ============================================ */
13831375

1384-
/* ------------------------------------------------------------------------- */
1385-
/**
1386-
* Prints error message to stderr and terminates the application.
1387-
*
1388-
* @param msg Error message to display.
1389-
*/
1390-
#ifdef __GNUC__
1391-
__attribute__((noreturn))
1392-
#endif
1393-
static void _wlm_graph_panic(const char *msg)
1394-
{
1395-
fprintf(stderr, "wlm_graph: %s\n", msg);
1396-
exit(EXIT_FAILURE);
1397-
}
1398-
13991376
/* ------------------------------------------------------------------------- */
14001377
/**
14011378
* Frees all resources associated with graph state.
@@ -1437,7 +1414,7 @@ int wlm_graph_app_run(
14371414
// Allocate graph state.
14381415
wlm_graph_state_t *graph_state = calloc(1, sizeof(wlm_graph_state_t));
14391416
if (NULL == graph_state) {
1440-
fprintf(stderr, "%s: Failed to allocate graph state\n", config->app_name);
1417+
bs_log(BS_ERROR, "%s: Failed to allocate graph state", config->app_name);
14411418
return EXIT_FAILURE;
14421419
}
14431420

@@ -1451,8 +1428,8 @@ int wlm_graph_app_run(
14511428

14521429
// Initialize graph buffers with default icon size.
14531430
const uint32_t size_default[2] = {WLM_GRAPH_BASE_ICON_SIZE, WLM_GRAPH_BASE_ICON_SIZE};
1454-
if (!_wlm_graph_buffers_resize(graph_state, size_default, prefs.margin_logical_px, _wlm_graph_panic)) {
1455-
fprintf(stderr, "Error: Icon dimensions too small for graph\n");
1431+
if (!_wlm_graph_buffers_resize(graph_state, size_default, prefs.margin_logical_px)) {
1432+
bs_log(BS_ERROR, "Icon dimensions too small for graph");
14561433
_wlm_graph_state_free(graph_state);
14571434
return EXIT_FAILURE;
14581435
}

apps/wlm_graph_shared.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ typedef struct {
133133
void (*state_free_fn)(void *app_state);
134134

135135
/**
136-
* Optional custom pixel lookup table (256 entries, ARGB format).
136+
* Optional custom pixel lookup table (256 entries, ARGB32 format).
137137
*
138138
* If non-NULL, overrides the default heat-map LUT. Index 0 maps to lowest
139139
* intensity (single value active), index 255 to highest (all values active).

apps/wlm_graph_utildefines.h

Lines changed: 0 additions & 50 deletions
This file was deleted.

apps/wlmcpugraph.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222

2323
#include "wlm_graph_shared.h"
2424

25+
#include <ctype.h>
2526
#include <stdbool.h>
2627
#include <stdint.h>
2728
#include <stdio.h>
2829
#include <stdlib.h>
2930
#include <string.h>
3031

31-
#include "wlm_graph_utildefines.h"
32+
#include <libbase/libbase.h>
3233

3334
/** Application name. */
3435
static const char _app_name[] = "wlmcpugraph";
@@ -147,7 +148,7 @@ static wlm_graph_read_result_t _stats_read_fn(void *app_state, wlm_graph_values_
147148
// First pass: count CPUs.
148149
while (NULL != fgets(line, sizeof(line), fp)) {
149150
if (0 != strncmp(line, "cpu", 3)) continue;
150-
if (ISDIGIT(line[3])) {
151+
if (isdigit(line[3])) {
151152
cpu_count++;
152153
}
153154
}
@@ -179,7 +180,7 @@ static wlm_graph_read_result_t _stats_read_fn(void *app_state, wlm_graph_values_
179180
while (NULL != fgets(line, sizeof(line), fp) && read_count < cpu_count) {
180181
// Skip the aggregate "cpu" line, look for "cpu0", "cpu1", etc.
181182
if (0 != strncmp(line, "cpu", 3)) continue;
182-
if (!ISDIGIT(line[3])) continue;
183+
if (!isdigit(line[3])) continue;
183184

184185
unsigned long user, nice, system, idle, iowait, irq, softirq;
185186
const int fields_parsed = sscanf(
@@ -197,7 +198,7 @@ static wlm_graph_read_result_t _stats_read_fn(void *app_state, wlm_graph_values_
197198
if (0 != prev->total &&
198199
abs_total > prev->total && abs_idle >= prev->idle) {
199200
const unsigned long total_diff = abs_total - prev->total;
200-
const unsigned long idle_diff = MIN2(abs_idle - prev->idle, total_diff);
201+
const unsigned long idle_diff = BS_MIN(abs_idle - prev->idle, total_diff);
201202
usage = (uint8_t)(((total_diff - idle_diff) * 255) / total_diff);
202203
}
203204

@@ -219,17 +220,17 @@ static wlm_graph_read_result_t _stats_read_fn(void *app_state, wlm_graph_values_
219220
/** Main program. */
220221
int main(const int argc, const char **argv)
221222
{
222-
cpugraph_state_t state = { 0 };
223+
cpugraph_state_t state = {};
223224

224225
state.proc_fp = fopen("/proc/stat", "r");
225226
if (NULL == state.proc_fp) {
226-
fprintf(stderr, "%s: Failed to open /proc/stat\n", _app_name);
227+
bs_log(BS_ERROR | BS_ERRNO, "Failed to open /proc/stat");
227228
return EXIT_FAILURE;
228229
}
229230

230231
// Prime prev values so first real sample computes proper delta.
231232
{
232-
wlm_graph_values_t values = { 0 };
233+
wlm_graph_values_t values = {};
233234
_stats_read_fn(&state, &values);
234235
free(values.data);
235236
}

apps/wlmmemgraph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ static wlm_graph_read_result_t _stats_read_fn(void *app_state, wlm_graph_values_
287287
/** Main program. */
288288
int main(const int argc, const char **argv)
289289
{
290-
memgraph_state_t state = { 0 };
290+
memgraph_state_t state = {};
291291

292292
state.proc_fp = fopen("/proc/meminfo", "r");
293293
if (NULL == state.proc_fp) {
294-
fprintf(stderr, "%s: Failed to open /proc/meminfo\n", _app_name);
294+
bs_log(BS_ERROR | BS_ERRNO, "Failed to open /proc/meminfo");
295295
return EXIT_FAILURE;
296296
}
297297

0 commit comments

Comments
 (0)