]> mj.ucw.cz Git - libucw.git/blob - ucw/log-file.c
015b8df2075700621e46b6ee2efb8144f3224317
[libucw.git] / ucw / log-file.c
1 /*
2  *      UCW Library -- Logging to Files
3  *
4  *      (c) 1997--2009 Martin Mares <mj@ucw.cz>
5  *      (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/log.h"
13 #include "ucw/lfs.h"
14 #include "ucw/threads.h"
15 #include "ucw/simple-lists.h"
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <time.h>
22
23 /*
24  *  Use of the private fields of struct log_stream:
25  *
26  *      idata   file descriptor
27  *      udata   various flags (FF_xxx)
28  *      pdata   original name with strftime escapes
29  *      name    current name of the log file
30  *              (a dynamically allocated buffer)
31  */
32
33 enum log_file_flag {
34   FF_FORMAT_NAME = 1,           // Name contains strftime escapes
35   FF_CLOSE_FD = 2,              // Close the fd with the stream
36 };
37
38 #define MAX_EXPAND 64           // Maximum size of expansion of strftime escapes
39
40 static int log_switch_nest;
41
42 static void
43 do_log_reopen(struct log_stream *ls, const char *name)
44 {
45   int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
46   if (fd < 0)
47     die("Unable to open log file %s: %m", name);
48   if (ls->idata < 0)
49     ls->idata = fd;
50   else
51     {
52       dup2(fd, ls->idata);
53       close(fd);
54     }
55   if (ls->name)
56     {
57       xfree(ls->name);
58       ls->name = NULL;          // We have to keep this consistent, die() below can invoke logging
59     }
60   ls->name = xstrdup(name);
61 }
62
63 static int
64 do_log_switch(struct log_stream *ls, struct tm *tm)
65 {
66   if (!(ls->udata & FF_FORMAT_NAME))
67     {
68       if (ls->idata >= 0)
69         return 1;
70       else
71         {
72           do_log_reopen(ls, ls->pdata);
73           return 1;
74         }
75     }
76
77   int buflen = strlen(ls->pdata) + MAX_EXPAND;
78   char name[buflen];
79   int switched = 0;
80
81   ucwlib_lock();
82   if (!log_switch_nest)         // Avoid infinite loops if we die when switching logs
83     {
84       log_switch_nest++;
85       int l = strftime(name, buflen, ls->pdata, tm);
86       if (l < 0 || l >= buflen)
87         die("Error formatting log file name: %m");
88       if (!ls->name || strcmp(name, ls->name))
89         {
90           do_log_reopen(ls, name);
91           switched = 1;
92         }
93       log_switch_nest--;
94     }
95   ucwlib_unlock();
96   return switched;
97 }
98
99 /* Emulate the old single-file interface: close the existing log file and open a new one. */
100 void
101 log_file(const char *name)
102 {
103   if (!name)
104     return;
105
106   struct log_stream *ls = log_new_file(name);
107   struct log_stream *def = log_stream_by_flags(0);
108   simp_node *s;
109   while (s = clist_head(&def->substreams))
110     {
111       struct log_stream *old = s->p;
112       log_rm_substream(def, old);
113       if (old != (struct log_stream *) &log_stream_default)
114         log_close_stream(old);
115     }
116   dup2(ls->idata, 2);                   // Let fd2 be an alias for the log file
117   log_add_substream(def, ls);
118 }
119
120 int
121 log_switch(void)
122 {
123 #if 0 // FIXME
124   time_t tim = time(NULL);
125   return do_log_switch(localtime(&tim));
126 #else
127   return 0;
128 #endif
129 }
130
131 void
132 log_switch_disable(void)
133 {
134   log_switch_nest++;
135 }
136
137 void
138 log_switch_enable(void)
139 {
140   ASSERT(log_switch_nest);
141   log_switch_nest--;
142 }
143
144 /* destructor for standard files */
145 static void
146 file_close(struct log_stream *ls)
147 {
148   if ((ls->udata & FF_CLOSE_FD) && ls->idata >= 0)
149     close(ls->idata);
150   xfree(ls->name);
151 }
152
153 /* handler for standard files */
154 static int
155 file_handler(struct log_stream *ls, const char *m, uns cat UNUSED)
156 {
157   if (ls->udata & FF_FORMAT_NAME)
158     {
159       // FIXME: pass the time
160       time_t now = time(NULL);
161       struct tm *tm = localtime(&now);
162       do_log_switch(ls, tm);
163     }
164
165   int len = strlen(m);
166   int r = write(ls->idata, m, len);
167   /* FIXME: check for errors here? */
168   return 0;
169 }
170
171 /* assign log to a file descriptor */
172 /* initialize with the default formatting, does NOT close the descriptor */
173 struct log_stream *
174 log_new_fd(int fd)
175 {
176   struct log_stream *ls = log_new_stream();
177   ls->idata = fd;
178   ls->msgfmt = LSFMT_DEFAULT;
179   ls->handler = file_handler;
180   ls->close = file_close;
181   ls->name = xmalloc(16);
182   snprintf(ls->name, 16, "fd%d", fd);
183   return ls;
184 }
185
186 /* open() a file (append mode) */
187 /* initialize with the default formatting */
188 struct log_stream *
189 log_new_file(const char *path)
190 {
191   struct log_stream *ls = log_new_stream();
192   ls->idata = -1;
193   ls->pdata = (void *) path;
194   if (strchr(path, '%'))
195     ls->udata = FF_FORMAT_NAME;
196   ls->udata |= FF_CLOSE_FD;
197   ls->msgfmt = LSFMT_DEFAULT;
198   ls->handler = file_handler;
199   ls->close = file_close;
200
201   time_t now = time(NULL);
202   struct tm *tm = localtime(&now);
203   do_log_switch(ls, tm);                // die()'s on errors
204   return ls;
205 }
206
207 #ifdef TEST
208
209 int main(int argc, char **argv)
210 {
211   log_init(argv[0]);
212   log_file("/proc/self/fd/1");
213   // struct log_stream *ls = log_new_fd(1);
214   // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S");
215   for (int i=1; i<argc; i++)
216     msg(L_INFO, argv[i]);
217   return 0;
218 }
219
220 #endif