]> mj.ucw.cz Git - libucw.git/blob - ucw/log-file.c
52a010876d76540d6aec4dbb0d5ae39a4a7809dd
[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   log_rm_substream(def, NULL);
109   log_add_substream(def, ls);
110   dup2(ls->idata, 2);                   // Let fd2 be an alias for the log file
111 }
112
113 /* destructor for standard files */
114 static void
115 file_close(struct log_stream *ls)
116 {
117   if ((ls->udata & FF_CLOSE_FD) && ls->idata >= 0)
118     close(ls->idata);
119   xfree(ls->name);
120   xfree(ls->pdata);
121 }
122
123 /* handler for standard files */
124 static int
125 file_handler(struct log_stream *ls, struct log_msg *m)
126 {
127   if ((ls->udata & FF_FORMAT_NAME) && m->tm)
128     do_log_switch(ls, m->tm);
129
130   int r = write(ls->idata, m->m, m->m_len);
131   /* FIXME: check for errors here? */
132   return 0;
133 }
134
135 /* assign log to a file descriptor */
136 /* initialize with the default formatting, does NOT close the descriptor */
137 struct log_stream *
138 log_new_fd(int fd)
139 {
140   struct log_stream *ls = log_new_stream();
141   ls->idata = fd;
142   ls->msgfmt = LSFMT_DEFAULT;
143   ls->handler = file_handler;
144   ls->close = file_close;
145   ls->name = xmalloc(16);
146   snprintf(ls->name, 16, "fd%d", fd);
147   return ls;
148 }
149
150 /* open() a file (append mode) */
151 /* initialize with the default formatting */
152 struct log_stream *
153 log_new_file(const char *path)
154 {
155   struct log_stream *ls = log_new_stream();
156   ls->idata = -1;
157   ls->pdata = xstrdup(path);
158   if (strchr(path, '%'))
159     ls->udata = FF_FORMAT_NAME;
160   ls->udata |= FF_CLOSE_FD;
161   ls->msgfmt = LSFMT_DEFAULT;
162   ls->handler = file_handler;
163   ls->close = file_close;
164
165   time_t now = time(NULL);
166   struct tm *tm = localtime(&now);
167   ASSERT(tm);
168   do_log_switch(ls, tm);                // die()'s on errors
169   return ls;
170 }
171
172 int
173 log_switch(void)
174 {
175   time_t now = time(NULL);
176   struct tm *tm = localtime(&now);
177   ASSERT(tm);
178
179   int switched = 0;
180   for (int i=0; i < log_streams_after; i++)
181     if (log_streams.ptr[i]->handler == file_handler)
182       switched |= do_log_switch(log_streams.ptr[i], tm);
183   return switched;
184 }
185
186 void
187 log_switch_disable(void)
188 {
189   log_switch_nest++;
190 }
191
192 void
193 log_switch_enable(void)
194 {
195   ASSERT(log_switch_nest);
196   log_switch_nest--;
197 }
198
199 #ifdef TEST
200
201 int main(int argc, char **argv)
202 {
203   log_init(argv[0]);
204   log_file("/proc/self/fd/1");
205   // struct log_stream *ls = log_new_fd(1);
206   // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S");
207   for (int i=1; i<argc; i++)
208     msg(L_INFO, argv[i]);
209   return 0;
210 }
211
212 #endif