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.
13 #include "ucw/log-internal.h"
15 #include "ucw/threads.h"
16 #include "ucw/simple-lists.h"
25 struct log_stream ls; // ls.name is the current name of the log file
28 char *orig_name; // Original name with strftime escapes
32 FF_FORMAT_NAME = 1, // Name contains strftime escapes
33 FF_CLOSE_FD = 2, // Close the fd with the stream
34 FF_FD2_FOLLOWS = 4, // Maintain stderr as a clone of this stream
37 #define MAX_EXPAND 64 // Maximum size of expansion of strftime escapes
39 static int log_switch_nest;
42 do_log_reopen(struct file_stream *fs, const char *name)
44 int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
46 die("Unable to open log file %s: %m", name);
50 if (fs->flags & FF_FD2_FOLLOWS)
55 fs->ls.name = NULL; // We have to keep the stream consistent -- die() below can invoke logging
57 fs->ls.name = xstrdup(name);
61 do_log_switch(struct file_stream *fs, struct tm *tm)
63 if (!(fs->flags & FF_FORMAT_NAME))
69 do_log_reopen(fs, fs->orig_name);
74 int buflen = strlen(fs->orig_name) + MAX_EXPAND;
79 if (!log_switch_nest) // Avoid infinite loops if we die when switching logs
82 int l = strftime(name, buflen, fs->orig_name, tm);
83 if (l < 0 || l >= buflen)
84 die("Error formatting log file name: %m");
85 if (!fs->ls.name || strcmp(name, fs->ls.name))
87 do_log_reopen(fs, name);
97 file_close(struct log_stream *ls)
99 struct file_stream *fs = (struct file_stream *) ls;
100 if ((fs->flags & FF_CLOSE_FD) && fs->fd >= 0)
103 xfree(fs->orig_name);
107 file_handler(struct log_stream *ls, struct log_msg *m)
109 struct file_stream *fs = (struct file_stream *) ls;
110 if ((fs->flags & FF_FORMAT_NAME) && m->tm)
111 do_log_switch(fs, m->tm);
113 int r = write(fs->fd, m->m, m->m_len);
114 /* FIXME: check for errors here? */
121 struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
122 struct file_stream *fs = (struct file_stream *) ls;
124 ls->msgfmt = LSFMT_DEFAULT;
125 ls->handler = file_handler;
126 ls->close = file_close;
127 ls->name = xmalloc(16);
128 snprintf(ls->name, 16, "fd%d", fd);
132 static struct log_stream *
133 do_log_new_file(const char *path, uns more_flags)
135 struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
136 struct file_stream *fs = (struct file_stream *) ls;
138 fs->orig_name = xstrdup(path);
139 if (strchr(path, '%'))
140 fs->flags = FF_FORMAT_NAME;
141 fs->flags |= FF_CLOSE_FD | more_flags;
142 ls->msgfmt = LSFMT_DEFAULT;
143 ls->handler = file_handler;
144 ls->close = file_close;
146 time_t now = time(NULL);
147 struct tm *tm = localtime(&now);
149 do_log_switch(fs, tm); // die()'s on errors
154 log_new_file(const char *path)
156 return do_log_new_file(path, 0);
162 time_t now = time(NULL);
163 struct tm *tm = localtime(&now);
167 for (int i=0; i < log_streams_after; i++)
168 if (log_streams.ptr[i]->handler == file_handler)
169 switched |= do_log_switch((struct file_stream *) log_streams.ptr[i], tm);
174 log_switch_disable(void)
180 log_switch_enable(void)
182 ASSERT(log_switch_nest);
187 log_file(const char *name)
192 struct log_stream *ls = do_log_new_file(name, FF_FD2_FOLLOWS);
193 struct log_stream *def = log_stream_by_flags(0);
194 log_rm_substream(def, NULL);
195 log_add_substream(def, ls);
200 int main(int argc, char **argv)
203 log_file("/proc/self/fd/1");
204 // struct log_stream *ls = log_new_fd(1);
205 // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S");
206 for (int i=1; i<argc; i++)
207 msg(L_INFO, argv[i]);