GCC Code Coverage Report


Directory: source/
File: source/world_logger.c
Date: 2023-12-18 09:27:49
Exec Total Coverage
Lines: 63 76 82.9%
Branches: 23 34 67.6%

Line Branch Exec Source
1 /*
2 * Filename: world_logger.c
3 * Path: source
4 * Created Date: Monday, April 17th 2023, 11:32:33 pm
5 * Author: osvegn
6 *
7 * Copyright (c) 2023 ECS
8 */
9
10 #include "world_logger.h"
11 #include <stdarg.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <time.h>
15 #ifdef __linux__
16 #include <unistd.h>
17 #endif
18
19 FILE *file = NULL;
20 static enum world_log_level level = WORLD_LOG_LEVEL_ERROR;
21
22 50 int world_log_init(FILE *f)
23 {
24 50 char buffer[26] = {0};
25 50 time_t now = time(NULL);
26 50 struct tm *t = localtime(&now);
27 50 char filename[40] = {0};
28
29
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5 times.
50 if (f != NULL) {
30 45 file = f;
31 }
32 else {
33 #ifdef __linux__
34
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 if (access("log", F_OK) == -1) {
35
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (mkdir("log", 0777) == -1) {
36 return -1;
37 }
38 }
39 5 strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", t);
40 5 sprintf(filename, "log/world-%s.log", buffer);
41 5 file = fopen(filename, "a");
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (file == NULL)
43 return -1;
44 #else
45 return -1;
46 #endif
47 }
48 50 return 0;
49 }
50
51 3 static int print_log_time(void)
52 {
53 3 char buffer[26] = {0};
54 3 time_t now = time(NULL);
55 3 struct tm *t = localtime(&now);
56
57 3 strftime(buffer, 26, "%Y-%m-%d %H:%M:%S\t", t);
58 3 fprintf(file, "%s", buffer);
59 3 fflush(file);
60 3 return 0;
61 }
62
63 3 static int print_log_level(enum world_log_level level)
64 {
65
3/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
3 switch (level) {
66 case WORLD_LOG_LEVEL_DEBUG:
67 fprintf(file, "DEBUG\t");
68 break;
69 case WORLD_LOG_LEVEL_INFO:
70 fprintf(file, "INFO\t");
71 break;
72 case WORLD_LOG_LEVEL_WARN:
73 fprintf(file, "WARN\t");
74 break;
75 1 case WORLD_LOG_LEVEL_ERROR:
76 1 fprintf(file, "ERROR\t");
77 1 break;
78 1 case WORLD_LOG_LEVEL_FATAL:
79 1 fprintf(file, "FATAL\t");
80 1 break;
81 1 default:
82 1 fprintf(file, "UNKNOWN\t");
83 1 break;
84 }
85 3 fflush(file);
86 3 return 0;
87 }
88
89 297 int world_log(enum world_log_level level, const char *filename, int line, const char *fmt, ...)
90 {
91 va_list args;
92 297 char *buf = NULL;
93
94
4/6
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 138 times.
297 if (file == NULL || filename == NULL || fmt == NULL)
95 159 return -1;
96
2/2
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 3 times.
138 if (level < get_world_log_level())
97 135 return 0;
98 3 print_log_time();
99 3 print_log_level(level);
100 /// memory leak near
101 #ifdef __linux__
102 3 buf = getcwd(NULL, 0);
103 #endif
104
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (buf != NULL) {
105 3 buf = strstr(filename, buf);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (buf != NULL)
107 fprintf(file, "{cwd}%s:%d\t", buf + strlen(buf), line);
108 else
109 3 fprintf(file, "%s:%d\t", filename, line);
110 }
111 else {
112 fprintf(file, "%s:%d\t", filename, line);
113 }
114 3 va_start(args, fmt);
115 3 vfprintf(file, fmt, args);
116 3 fprintf(file, "\n");
117 3 va_end(args);
118 3 fflush(file);
119 3 return 0;
120 }
121
122 23 void world_log_destroy(void)
123 {
124
6/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 5 times.
23 if (file && file != stdout && file != stdin && file != stderr)
125 7 fclose(file);
126 23 }
127
128 1 void set_world_log_level(enum world_log_level l)
129 {
130 1 level = l;
131 1 }
132
133 140 enum world_log_level get_world_log_level(void)
134 {
135 140 return level;
136 }
137