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);
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
113 /* destructor for standard files */
115 file_close(struct log_stream *ls)
117 if ((ls->udata & FF_CLOSE_FD) && ls->idata >= 0)
123 /* handler for standard files */
125 file_handler(struct log_stream *ls, struct log_msg *m)
127 if ((ls->udata & FF_FORMAT_NAME) && m->tm)
128 do_log_switch(ls, m->tm);
130 int r = write(ls->idata, m->m, m->m_len);
131 /* FIXME: check for errors here? */
135 /* assign log to a file descriptor */
136 /* initialize with the default formatting, does NOT close the descriptor */
140 struct log_stream *ls = log_new_stream();
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);
150 /* open() a file (append mode) */
151 /* initialize with the default formatting */
153 log_new_file(const char *path)
155 struct log_stream *ls = log_new_stream();
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;
165 time_t now = time(NULL);
166 struct tm *tm = localtime(&now);
168 do_log_switch(ls, tm); // die()'s on errors
175 time_t now = time(NULL);
176 struct tm *tm = localtime(&now);
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);
187 log_switch_disable(void)
193 log_switch_enable(void)
195 ASSERT(log_switch_nest);
201 int main(int argc, char **argv)
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]);