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
|
/*
* log.c:
*
* Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
* All rights reserved.
*
*/
static char rcsid[] = "$Id$";
/*
* $Log$
* Revision 1.1 2008/02/14 12:14:50 james
* *** empty log message ***
*
*/
#include "project.h"
typedef struct
{
LOG_SIGNATURE;
FILE *fp;
} File_Log;
static void
flog_log (Log * _l, char *buf)
{
File_Log *l = (File_Log *) _l;
struct timeval tv = { 0 };
struct tm *tm;
time_t t;
static const char *days[] = { "Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat"
};
static const char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"
};
if (!l->fp)
return;
gettimeofday (&tv, NULL);
t = tv.tv_sec;
tm = localtime (&t);
fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon],
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
fputs (buf, l->fp);
fputc ('\n', l->fp);
fflush (l->fp);
}
static void
flog_close (Log * _l)
{
File_Log *l = (File_Log *) _l;
if (l->fp)
fclose (l->fp);
free (l);
}
Log *
file_log_new (char *fn)
{
File_Log *l;
FILE *f;
f = fopen (fn, "a+");
if (!f)
return NULL;
l = malloc (sizeof (File_Log));
l->log = flog_log;
l->close = flog_close;
l->fp = f;
return (Log *) l;
}
|