-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
85 lines (67 loc) · 1.71 KB
/
logger.c
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
80
81
82
83
84
85
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "logger.h"
#define FILENAME "latest.log"
/*
* copyright cy-chomâge valérie 2024
*/
int createLog() { // TODO: renvoyer le nom du fichier log + date heure minute seconde
FILE* file = fopen(FILENAME, "w");
if (file == NULL) {
return -1;
}
fclose(file);
return 0;
}
int logMessage(int level, char* message) {
char instant[51];
char niveau[10];
time_t t = time(NULL);
tzset();
strftime(instant, sizeof(instant) - 1, "%a %b %d %T %Z %Y", localtime(&t));
FILE* file = fopen(FILENAME, "a");
if(file == NULL) {
return -1;
}
switch(level) {
case DEBUG:
sprintf(niveau, "DEBUG");
break;
case INFO:
sprintf(niveau, "INFO");
break;
case WARNING:
sprintf(niveau, "WARNING");
break;
case ERROR:
sprintf(niveau, "ERROR");
break;
case CRITICAL:
sprintf(niveau, "CRITICAL");
break;
default:
sprintf(niveau, "UNKNOWN");
break;
}
fprintf(file, "[%s] %s %s\n", instant, niveau, message);
fclose(file);
return 0;
}
void clearBuf(char * buf) {
strcpy(buf, "");
}
int moveLog() {
char name[50];
unsigned short int r;
time_t t = time(NULL);
tzset();
strftime(name, sizeof(name) - 1, "%d-%m-%Y-%H:%M:%S.log", localtime(&t));
logMessage(DEBUG, "deinit log");
logMessage(DEBUG, name);
r = rename(FILENAME, name);
if(r != 0) {
return -1;
}
return 0;
}