-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
65 lines (52 loc) · 1.36 KB
/
util.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
//
// Created by Administrator on 2023-10-05.
//
#include "util.h"
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
FILE *log_file;
pthread_mutex_t lock;
void log_output(FILE *file) {
log_file = file;
}
void log_info(const char *message, ...) {
char vbuffer[255];
va_list args;
va_start(args, message);
vsnprintf(vbuffer, ARRAY_SIZE(vbuffer), message, args);
va_end(args);
time_t now;
time(&now);
char *date = ctime(&now);
date[strlen(date) - 1] = '\0';
pthread_t self = pthread_self();
fprintf(log_file, "[%s][%lu] Info: %s\n", date, self, vbuffer);
fflush(log_file);
}
void log_message(const char *message, ...) {
char vbuffer[255];
va_list args;
va_start(args, message);
vsnprintf(vbuffer, ARRAY_SIZE(vbuffer), message, args);
va_end(args);
time_t now;
time(&now);
char *date = ctime(&now);
date[strlen(date) - 1] = '\0';
pthread_t self = pthread_self();
if (errno != 0) {
pthread_mutex_lock(&lock);
fprintf(log_file, "[%s][%lu] Critical: %s - %s\n", date, self,
vbuffer, strerror(errno));
errno = 0;
pthread_mutex_unlock(&lock);
} else {
fprintf(log_file, "[%s][%lu] Info: %s\n", date, self, vbuffer);
}
fflush(log_file);
}