GCC Code Coverage Report


Directory: src/
File: src/log.c
Date: 2024-04-25 03:45:42
Exec Total Coverage
Lines: 22 24 91.7%
Branches: 4 6 66.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2023 Egor Tensin <egor@tensin.name>
3 * This file is part of the "cimple" project.
4 * For details, see https://github.com/egor-tensin/cimple.
5 * Distributed under the MIT License.
6 */
7
8 #include "log.h"
9
10 #include <stdio.h>
11 #include <sys/time.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 int g_log_lvl = LOG_LVL_INFO;
16
17 182861 static inline void log_prefix_timestamp(FILE *dest)
18 {
19 struct timeval tv;
20 struct tm tm;
21 char buf[64];
22 182861 size_t used = 0;
23
24
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182861 times.
182861 if (gettimeofday(&tv, NULL) < 0)
25 return;
26
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182861 times.
182861 if (!gmtime_r(&tv.tv_sec, &tm))
27 return;
28
29 182861 buf[0] = '\0';
30 182861 used += strftime(buf + used, sizeof(buf) - used, "%F %T", &tm);
31 182861 long long msec = (long long)tv.tv_usec / 1000;
32 182861 used += snprintf(buf + used, sizeof(buf) - used, ".%03lld | ", msec);
33 182861 fprintf(dest, "%s", buf);
34 }
35
36 182861 static inline void log_prefix_thread_id(FILE *dest)
37 {
38 182861 fprintf(dest, "%d | ", gettid());
39 182861 }
40
41 708199 int log_entry_start(int lvl, FILE *dest)
42 {
43
2/2
✓ Branch 0 taken 525338 times.
✓ Branch 1 taken 182861 times.
708199 if (lvl < g_log_lvl)
44 525338 return 0;
45 182861 flockfile(dest);
46 182861 log_prefix_timestamp(dest);
47 182861 log_prefix_thread_id(dest);
48 182861 return 1;
49 }
50
51 182861 void log_entry_end(FILE *dest)
52 {
53 182861 funlockfile(dest);
54 182861 }
55