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 struct log_stream ls; // ls.name is the current name of the log file
27 char *orig_name; // Original name with strftime escapes
31 FF_FORMAT_NAME = 1, // Name contains strftime escapes
32 FF_CLOSE_FD = 2, // Close the fd with the stream
35 #define MAX_EXPAND 64 // Maximum size of expansion of strftime escapes
37 static int log_switch_nest;
40 do_log_reopen(struct file_stream *fs, const char *name)
42 int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
44 die("Unable to open log file %s: %m", name);
55 fs->ls.name = NULL; // We have to keep this 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);
96 /* Emulate the old single-file interface: close the existing log file and open a new one. */
98 log_file(const char *name)
103 struct log_stream *ls = log_new_file(name);
104 struct log_stream *def = log_stream_by_flags(0);
105 log_rm_substream(def, NULL);
106 log_add_substream(def, ls);
107 dup2(((struct file_stream *)ls)->fd, 2); // Let fd2 be an alias for the log file
110 /* destructor for standard files */
112 file_close(struct log_stream *ls)
114 struct file_stream *fs = (struct file_stream *) ls;
115 if ((fs->flags & FF_CLOSE_FD) && fs->fd >= 0)
118 xfree(fs->orig_name);
121 /* handler for standard files */
123 file_handler(struct log_stream *ls, struct log_msg *m)
125 struct file_stream *fs = (struct file_stream *) ls;
126 if ((fs->flags & FF_FORMAT_NAME) && m->tm)
127 do_log_switch(fs, m->tm);
129 int r = write(fs->fd, m->m, m->m_len);
130 /* FIXME: check for errors here? */
134 /* assign log to a file descriptor */
135 /* initialize with the default formatting, does NOT close the descriptor */
139 struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
140 struct file_stream *fs = (struct file_stream *) ls;
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(sizeof(struct file_stream));
156 struct file_stream *fs = (struct file_stream *) ls;
158 fs->orig_name = xstrdup(path);
159 if (strchr(path, '%'))
160 fs->flags = FF_FORMAT_NAME;
161 fs->flags |= FF_CLOSE_FD;
162 ls->msgfmt = LSFMT_DEFAULT;
163 ls->handler = file_handler;
164 ls->close = file_close;
166 time_t now = time(NULL);
167 struct tm *tm = localtime(&now);
169 do_log_switch(fs, tm); // die()'s on errors
176 time_t now = time(NULL);
177 struct tm *tm = localtime(&now);
181 for (int i=0; i < log_streams_after; i++)
182 if (log_streams.ptr[i]->handler == file_handler)
183 switched |= do_log_switch((struct file_stream *) log_streams.ptr[i], tm);
188 log_switch_disable(void)
194 log_switch_enable(void)
196 ASSERT(log_switch_nest);
202 int main(int argc, char **argv)
205 log_file("/proc/self/fd/1");
206 // struct log_stream *ls = log_new_fd(1);
207 // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S");
208 for (int i=1; i<argc; i++)
209 msg(L_INFO, argv[i]);