1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include "log.h"
#include "main.h"
static FILE *logFile;
int log_init(char *name)
{
logFile = fopen(name, "a");
if (logFile == NULL) {
fprintf(stderr, _("[Error] Unable to open log file [%s]\n"), name);
return -1;
}
DEBUG_MSG(_("[Debug] Using log file [%s]\n"), name);
log_add(LOG_INF, "Logging started");
return 0;
}
void log_add(int type, char *msg)
{
char str[STR_LOG_SIZE];
struct tm *tm_struct;
time_t currentTime;
char *str_type;
if (logFile == NULL) {
return;
}
currentTime = time(NULL);
tm_struct = localtime(¤tTime);
switch (type) {
case LOG_INF:
str_type = "INFO";
break;
case LOG_DBG:
str_type = "DEBUG";
break;
case LOG_WRN:
str_type = "WARN";
break;
case LOG_ERR:
str_type = "ERROR";
break;
default:
assert(!_("[Error] Unknown type of the logging string"));
break;
}
sprintf(str, "[%02d-%02d-%02d %02d:%02d:%02d] [%s] %s\n",
1900 + tm_struct->tm_year, tm_struct->tm_mon, tm_struct->tm_mday,
tm_struct->tm_hour, tm_struct->tm_min, tm_struct->tm_sec,
str_type, msg);
fprintf(logFile, "%s", str);
fflush(logFile);
}
void log_quit()
{
if (logFile == NULL) {
return;
}
log_add(LOG_INF, "Logging finished");
fclose(logFile);
}
|