chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / log.c
1 /*
2  * log.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.1  2008/02/14 12:14:50  james
14  * *** empty log message ***
15  *
16  */
17
18 #include "project.h"
19
20 typedef struct
21 {
22   LOG_SIGNATURE;
23   FILE *fp;
24 } File_Log;
25
26 static void
27 flog_log (Log * _l, char *buf)
28 {
29   File_Log *l = (File_Log *) _l;
30   struct timeval tv = { 0 };
31   struct tm *tm;
32   time_t t;
33   static const char *days[] = { "Sun", "Mon", "Tue",
34     "Wed", "Thu", "Fri", "Sat"
35   };
36   static const char *months[] = {
37     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
38     "Nov", "Dec"
39   };
40
41   if (!l->fp)
42     return;
43
44   gettimeofday (&tv, NULL);
45   t = tv.tv_sec;
46   tm = localtime (&t);
47
48   fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon],
49            tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
50
51   fputs (buf, l->fp);
52   fputc ('\n', l->fp);
53   fflush (l->fp);
54 }
55
56 static void
57 flog_close (Log * _l)
58 {
59   File_Log *l = (File_Log *) _l;
60   if (l->fp)
61     fclose (l->fp);
62   free (l);
63 }
64
65 Log *
66 file_log_new (char *fn)
67 {
68   File_Log *l;
69   FILE *f;
70
71   f = fopen (fn, "a+");
72   if (!f)
73     return NULL;
74
75   l = malloc (sizeof (File_Log));
76
77   l->log = flog_log;
78   l->close = flog_close;
79   l->fp = f;
80
81   return (Log *) l;
82 }