-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogger.c
70 lines (59 loc) · 1.29 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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <ctype.h>
#include "logger.h"
static FILE *logfd;
extern int debugged;
int logger_init(const char *filename)
{
FILE *fd;
if(filename) {
if((fd = fopen(filename, "a"))) {
logfd = fd;
return(1);
}
} else {
logfd = stdout;
return(1);
}
return(0);
}
void logger(int type, const char *fmt, ...)
{
va_list ap;
char tfmt[64], tbuf[64];
struct tm *tm;
struct timeval tv;
if(type == DBG && !debugged) {
return;
}
//tm = malloc(sizeof(struct tm));
gettimeofday(&tv, NULL);
if((tm = localtime(&tv.tv_sec)) != NULL) {
strftime(tfmt, sizeof(tfmt), "%Y-%m-%d %H:%M:%S.%%06u %z", tm);
snprintf(tbuf, sizeof(tbuf), tfmt, tv.tv_usec);
}
va_start(ap, fmt);
switch(type) {
case WARN:
(void)fprintf(logfd, "[!] ");
break;
case FATAL:
(void)fprintf(logfd, "[-] ");
break;
}
(void)fprintf(logfd, "[%s] ", tbuf);
if (fmt != NULL) {
(void)vfprintf(logfd, fmt, ap);
}
(void)fprintf(logfd, "\n");
fflush(logfd);
va_end(ap);
}