GCC Code Coverage Report


Directory: src/
File: src/log.c
Date: 2023-08-28 07:33:56
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@gmail.com>
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 173557 static inline void log_prefix_timestamp(FILE *dest)
18 {
19 struct timeval tv;
20 struct tm tm;
21 char buf[64];
22 173557 size_t used = 0;
23
24
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 173557 times.
173557 if (gettimeofday(&tv, NULL) < 0)
25 return;
26
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 173557 times.
173557 if (!gmtime_r(&tv.tv_sec, &tm))
27 return;
28
29 173557 buf[0] = '\0';
30 173557 used += strftime(buf + used, sizeof(buf) - used, "%F %T", &tm);
31 173557 long long msec = (long long)tv.tv_usec / 1000;
32 173557 used += snprintf(buf + used, sizeof(buf) - used, ".%03lld | ", msec);
33 173557 fprintf(dest, "%s", buf);
34 }
35
36 173557 static inline void log_prefix_thread_id(FILE *dest)
37 {
38 173557 fprintf(dest, "%d | ", gettid());
39 173557 }
40
41 719830 int log_entry_start(int lvl, FILE *dest)
42 {
43
2/2
✓ Branch 0 taken 546273 times.
✓ Branch 1 taken 173557 times.
719830 if (lvl < g_log_lvl)
44 546273 return 0;
45 173557 flockfile(dest);
46 173557 log_prefix_timestamp(dest);
47 173557 log_prefix_thread_id(dest);
48 173557 return 1;
49 }
50
51 173557 void log_entry_end(FILE *dest)
52 {
53 173557 funlockfile(dest);
54 173557 }
55