Skip to content

Commit 64e3e08

Browse files
committed
Change log handler to include file and line
Log handlers were previously required to acquire the file name and line number from the file member in the parser. Providing both to the handler is more intuitive. When multiple buffers become supported in a future release it will also be more straightforward to the developer that the file name is only correct while the log handler is executing.
1 parent f1f61a6 commit 64e3e08

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

include/zone.h

+4
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,14 @@ struct zone_parser;
342342
* By default messages are printed to stdout (info) and stderr (warnings,
343343
* errors). A custom log handler (callback) may be provided for better
344344
* integration of reporting.
345+
*
346+
* @note file maybe NULL if initial file does not exist.
345347
*/
346348
typedef void(*zone_log_t)(
347349
zone_parser_t *,
348350
uint32_t, // priority
351+
const char *, // file
352+
size_t, // line
349353
const char *, // message
350354
void *); // user data
351355

src/zone.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,12 @@ int32_t zone_parse_string(
483483
return code;
484484
}
485485

486-
zone_nonnull((1,3))
486+
zone_nonnull((1,5))
487487
static void print_message(
488488
zone_parser_t *parser,
489489
uint32_t priority,
490+
const char *file,
491+
size_t line,
490492
const char *message,
491493
void *user_data)
492494
{
@@ -495,8 +497,8 @@ static void print_message(
495497
assert(parser->file);
496498
FILE *output = priority == ZONE_INFO ? stdout : stderr;
497499

498-
if (parser->file->name)
499-
fprintf(output, "%s:%zu: %s\n", parser->file->name, parser->file->line, message);
500+
if (file)
501+
fprintf(output, "%s:%zu: %s\n", file, line, message);
500502
else
501503
fprintf(output, "%s\n", message);
502504
}
@@ -526,8 +528,10 @@ void zone_vlog(
526528
memcpy(message+(sizeof(message) - 4), "...", 3);
527529
if (parser->options.log.callback)
528530
callback = parser->options.log.callback;
529-
530-
callback(parser, priority, message, parser->user_data);
531+
assert(parser->file);
532+
const char *file = parser->file->name;
533+
const size_t line = parser->file->line;
534+
callback(parser, priority, file, line, message, parser->user_data);
531535
}
532536

533537
void zone_log(

tests/include.c

+11
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ void include_from_string(void **state)
245245

246246
typedef struct no_file_test no_file_test_t;
247247
struct no_file_test {
248+
bool have_file;
248249
size_t accept_count, log_count;
249250
};
250251

@@ -273,14 +274,18 @@ static int32_t no_such_file_accept(
273274
static void no_such_file_log(
274275
zone_parser_t *parser,
275276
uint32_t priority,
277+
const char *file,
278+
size_t line,
276279
const char *message,
277280
void *user_data)
278281
{
279282
(void)parser;
280283
(void)priority;
284+
(void)line;
281285
if (!strstr(message, "no such file"))
282286
return;
283287
no_file_test_t *test = (no_file_test_t*)user_data;
288+
test->have_file = file != NULL;
284289
test->log_count++;
285290
}
286291

@@ -313,6 +318,7 @@ void the_file_that_wasnt(void **state)
313318
code = zone_parse(&parser, &options, &buffers, non_file, &test);
314319
free(non_file);
315320
assert_int_equal(code, ZONE_NOT_A_FILE);
321+
assert_false(test.have_file);
316322
assert_true(test.log_count == 1);
317323
assert_true(test.accept_count == 0);
318324
}
@@ -351,6 +357,7 @@ void the_include_that_wasnt(void **state)
351357
free(include);
352358
free(non_include);
353359
assert_int_equal(code, ZONE_NOT_A_FILE);
360+
assert_true(test.have_file);
354361
assert_true(test.log_count == 1);
355362
assert_true(test.accept_count == 0);
356363
}
@@ -379,11 +386,15 @@ static int32_t in_too_deep_accept(
379386
static void in_too_deep_log(
380387
zone_parser_t *parser,
381388
uint32_t priority,
389+
const char *file,
390+
size_t line,
382391
const char *message,
383392
void *user_data)
384393
{
385394
(void)parser;
386395
(void)priority;
396+
(void)file;
397+
(void)line;
387398

388399
if (strstr(message, "nested too deeply"))
389400
*(size_t *)user_data |= 1u << 7;

0 commit comments

Comments
 (0)