2 * UCW Library -- Logging to Files
4 * (c) 1997--2015 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>
26 struct log_stream ls; // ls.name is the current name of the log file
29 char *orig_name; // Original name with strftime escapes
32 #define MAX_EXPAND 64 // Maximum size of expansion of strftime escapes
34 static int log_switch_nest;
35 static bool log_stderr_replaced;
38 do_log_reopen(struct file_stream *fs, const char *name)
40 int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
42 die("Unable to open log file %s: %m", name);
46 if (fs->flags & FF_FD2_FOLLOWS)
49 log_stderr_replaced = 1;
54 fs->ls.name = NULL; // We have to keep the stream consistent -- die() below can invoke logging
56 fs->ls.name = xstrdup(name);
60 do_log_switch(struct file_stream *fs, struct tm *tm)
62 if (!(fs->flags & FF_FORMAT_NAME))
68 do_log_reopen(fs, fs->orig_name);
73 int buflen = strlen(fs->orig_name) + MAX_EXPAND;
78 if (!log_switch_nest) // Avoid infinite loops if we die when switching logs
81 int l = strftime(name, buflen, fs->orig_name, tm);
82 if (l < 0 || l >= buflen)
83 die("Error formatting log file name: %m");
84 if (!fs->ls.name || strcmp(name, fs->ls.name))
86 do_log_reopen(fs, name);
96 file_close(struct log_stream *ls)
98 struct file_stream *fs = (struct file_stream *) ls;
99 if ((fs->flags & FF_CLOSE_FD) && fs->fd >= 0)
102 xfree(fs->orig_name);
106 file_handler(struct log_stream *ls, struct log_msg *m)
108 struct file_stream *fs = (struct file_stream *) ls;
109 if ((fs->flags & FF_FORMAT_NAME) && m->tm)
110 do_log_switch(fs, m->tm);
112 int r = write(fs->fd, m->m, m->m_len);
113 return ((r < 0) ? errno : 0);
117 log_new_fd(int fd, uint flags)
119 struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
120 struct file_stream *fs = (struct file_stream *) ls;
123 log_stderr_replaced = 1;
125 ls->msgfmt = LSFMT_DEFAULT;
126 ls->handler = file_handler;
127 ls->close = file_close;
128 ls->name = xmalloc(16);
129 snprintf(ls->name, 16, "fd%d", fd);
134 log_new_file(const char *path, uint flags)
136 struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
137 struct file_stream *fs = (struct file_stream *) ls;
139 fs->flags = FF_CLOSE_FD | flags;
140 fs->orig_name = xstrdup(path);
141 if (strchr(path, '%'))
142 fs->flags |= FF_FORMAT_NAME;
143 ls->msgfmt = LSFMT_DEFAULT;
144 ls->handler = file_handler;
145 ls->close = file_close;
147 time_t now = time(NULL);
148 struct tm *tm = localtime(&now);
150 do_log_switch(fs, tm); // die()'s on errors
157 time_t now = time(NULL);
158 struct tm *tm = localtime(&now);
162 for (int i=0; i < log_streams_after; i++)
163 if (log_streams.ptr[i]->handler == file_handler)
164 switched |= do_log_switch((struct file_stream *) log_streams.ptr[i], tm);
169 log_switch_disable(void)
175 log_switch_enable(void)
177 ASSERT(log_switch_nest);
182 log_file(const char *name)
187 log_set_default_stream(log_new_file(name, FF_FD2_FOLLOWS));
191 log_drop_stderr(void)
193 if (!log_stderr_replaced)
196 die("Cannot get rid of stderr: %m");
197 log_stderr_replaced = 1;
203 int main(int argc, char **argv)
206 log_file("/proc/self/fd/1");
207 // struct log_stream *ls = log_new_fd(1, 0);
208 // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S", 0);
209 for (int i=1; i<argc; i++)
210 msg(L_INFO, argv[i]);