2 * UCW Library -- Logging to Files
4 * (c) 1997--2009 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "ucw/threads.h"
15 #include "ucw/simple-lists.h"
24 * Use of the private fields of struct log_stream:
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)
34 FF_FORMAT_NAME = 1, // Name contains strftime escapes
35 FF_CLOSE_FD = 2, // Close the fd with the stream
38 #define MAX_EXPAND 64 // Maximum size of expansion of strftime escapes
40 static int log_switch_nest;
43 do_log_reopen(struct log_stream *ls, const char *name)
45 int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
47 die("Unable to open log file %s: %m", name);
58 ls->name = NULL; // We have to keep this consistent, die() below can invoke logging
60 ls->name = xstrdup(name);
64 do_log_switch(struct log_stream *ls, struct tm *tm)
66 if (!(ls->udata & FF_FORMAT_NAME))
72 do_log_reopen(ls, ls->pdata);
77 int buflen = strlen(ls->pdata) + MAX_EXPAND;
82 if (!log_switch_nest) // Avoid infinite loops if we die when switching logs
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))
90 do_log_reopen(ls, name);
99 /* Emulate the old single-file interface: close the existing log file and open a new one. */
101 log_file(const char *name)
106 struct log_stream *ls = log_new_file(name);
107 struct log_stream *def = log_stream_by_flags(0);
109 while (s = clist_head(&def->substreams))
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);
116 dup2(ls->idata, 2); // Let fd2 be an alias for the log file
117 log_add_substream(def, ls);
124 time_t tim = time(NULL);
125 return do_log_switch(localtime(&tim));
132 log_switch_disable(void)
138 log_switch_enable(void)
140 ASSERT(log_switch_nest);
144 /* destructor for standard files */
146 file_close(struct log_stream *ls)
148 if ((ls->udata & FF_CLOSE_FD) && ls->idata >= 0)
153 /* handler for standard files */
155 file_handler(struct log_stream *ls, const char *m, uns cat UNUSED)
157 if (ls->udata & FF_FORMAT_NAME)
159 // FIXME: pass the time
160 time_t now = time(NULL);
161 struct tm *tm = localtime(&now);
162 do_log_switch(ls, tm);
166 int r = write(ls->idata, m, len);
167 /* FIXME: check for errors here? */
171 /* assign log to a file descriptor */
172 /* initialize with the default formatting, does NOT close the descriptor */
176 struct log_stream *ls = log_new_stream();
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);
186 /* open() a file (append mode) */
187 /* initialize with the default formatting */
189 log_new_file(const char *path)
191 struct log_stream *ls = log_new_stream();
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;
201 time_t now = time(NULL);
202 struct tm *tm = localtime(&now);
203 do_log_switch(ls, tm); // die()'s on errors
209 int main(int argc, char **argv)
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]);