GCC Code Coverage Report


Directory: ./
File: log.c
Date: 2025-02-26 18:18:17
Exec Total Coverage
Lines: 15 25 60.0%
Functions: 4 6 66.7%
Branches: 2 4 50.0%

Line Branch Exec Source
1 #include <errno.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "log.h"
5
6 static enum liftoff_log_priority log_priority = LIFTOFF_ERROR;
7
8 static void
9 3336 log_stderr(enum liftoff_log_priority priority, const char *fmt, va_list args)
10 {
11 3336 vfprintf(stderr, fmt, args);
12 3336 fprintf(stderr, "\n");
13 3336 }
14
15 static liftoff_log_handler log_handler = log_stderr;
16
17 void
18 57 liftoff_log_set_priority(enum liftoff_log_priority priority)
19 {
20 57 log_priority = priority;
21 57 }
22
23 void
24 liftoff_log_set_handler(liftoff_log_handler handler) {
25 if (handler) {
26 log_handler = handler;
27 } else {
28 log_handler = log_stderr;
29 }
30 }
31
32 bool
33 3452 log_has(enum liftoff_log_priority priority)
34 {
35 3452 return priority <= log_priority;
36 }
37
38 void
39 3373 liftoff_log(enum liftoff_log_priority priority, const char *fmt, ...)
40 {
41 va_list args;
42
43
2/2
✓ Branch 0 (3→4) taken 37 times.
✓ Branch 1 (3→5) taken 3336 times.
3373 if (!log_has(priority)) {
44 37 return;
45 }
46
47 3336 va_start(args, fmt);
48 3336 log_handler(priority, fmt, args);
49 3336 va_end(args);
50 }
51
52 void
53 liftoff_log_errno(enum liftoff_log_priority priority, const char *msg)
54 {
55 // Ensure errno is still set to its original value when we return
56 int prev_errno = errno;
57 liftoff_log(priority, "%s: %s", msg, strerror(errno));
58 errno = prev_errno;
59 }
60