aboutsummaryrefslogtreecommitdiffstats
path: root/src/log.c
blob: 2f419515a76b46470442b34d5b9537a39f7b7930 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 
 * log.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/* 
 * $Log$
 * Revision 1.10  2008/03/07 12:37:04  james
 * *** empty log message ***
 *
 * Revision 1.9  2008/03/03 06:20:14  james
 * *** empty log message ***
 *
 * Revision 1.8  2008/03/03 06:04:42  james
 * *** empty log message ***
 *
 * Revision 1.7  2008/03/03 06:04:18  james
 * *** empty log message ***
 *
 * Revision 1.6  2008/03/02 10:37:56  james
 * *** empty log message ***
 *
 * Revision 1.5  2008/02/27 01:31:14  james
 * *** empty log message ***
 *
 * Revision 1.4  2008/02/27 00:54:16  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/23 11:48:37  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/22 14:51:54  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/14 12:14:50  james
 * *** empty log message ***
 *
 */

#include "project.h"

typedef struct
{
  LOG_SIGNATURE;
  int do_close;
  int rotate;
  FILE *fp;
  char *filename;
} 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);

  if (l->rotate && rotate_check (l->filename)) {
    fclose (l->fp);
    rotate (l->filename);
    l->fp = fopen (l->filename, "a+");
  }
}




static void
flog_close (Log * _l)
{
  File_Log *l = (File_Log *) _l;
  if (l->fp && l->do_close)
    fclose (l->fp);
  if (l->filename)
    free (l->filename);
  free (l);
}

Log *
file_log_new (char *fn, int rotate)
{
  File_Log *l;
  FILE *f;
  int dc = 1;

  if (fn && strcmp (fn, "-")) {
    f = fopen (fn, "a+");
    if (!f)
      return NULL;
  } else {
    f = stderr;
    dc = 0;
  }

  l = malloc (sizeof (File_Log));

  l->log = flog_log;
  l->close = flog_close;
  l->fp = f;
  l->do_close = dc;
  l->rotate = rotate;
  l->filename = strdup (fn);

  fput_cp (f, 0xffef);

  return (Log *) l;
}

void
log_f (Log * log, char *fmt, ...)
{

  int n;
  static char *buf;
  va_list ap;
  static int size;

  if (!log)
    return;

  if (!size) {
    size = 128;
    buf = malloc (size);
  }

  if (!buf)
    return;

  while (1) {
    va_start (ap, fmt);
    n = vsnprintf (buf, size, fmt, ap);
    va_end (ap);

    if (n > -1 && n < size) {
      log->log (log, buf);
      return;
    }

    if (n > -1)                 /* glibc 2.1 */
      size = n + 1;
    else                        /* glibc 2.0 */
      size *= 2;                /* twice the old size */

    buf = realloc (buf, size);

    if (!buf)
      return;
  }
}