X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ucw%2Flog-file.c;h=40f1f3964e487e21e7d85834f194d3b3bdd67d39;hb=564be9c0adf9f5796b60f2727cecc9c7274f86ff;hp=cd0d037c478e0e09ffe7a58c90ae9b35055d6c0c;hpb=d22dba5a3ad6cd0e0be4c07527218c6b48fd5bd3;p=libucw.git diff --git a/ucw/log-file.c b/ucw/log-file.c index cd0d037c..40f1f396 100644 --- a/ucw/log-file.c +++ b/ucw/log-file.c @@ -8,32 +8,27 @@ * of the GNU Lesser General Public License. */ -#include "ucw/lib.h" -#include "ucw/log.h" -#include "ucw/log-internal.h" -#include "ucw/lfs.h" -#include "ucw/threads.h" -#include "ucw/simple-lists.h" +#include +#include +#include +#include +#include +#include #include #include #include #include #include +#include struct file_stream { struct log_stream ls; // ls.name is the current name of the log file int fd; - uns flags; // FF_xxx + uint flags; // FF_xxx char *orig_name; // Original name with strftime escapes }; -enum log_file_flag { - FF_FORMAT_NAME = 1, // Name contains strftime escapes - FF_CLOSE_FD = 2, // Close the fd with the stream - FF_FD2_FOLLOWS = 4, // Maintain stderr as a clone of this stream -}; - #define MAX_EXPAND 64 // Maximum size of expansion of strftime escapes static int log_switch_nest; @@ -111,16 +106,16 @@ file_handler(struct log_stream *ls, struct log_msg *m) do_log_switch(fs, m->tm); int r = write(fs->fd, m->m, m->m_len); - /* FIXME: check for errors here? */ - return 0; + return ((r < 0) ? errno : 0); } struct log_stream * -log_new_fd(int fd) +log_new_fd(int fd, uint flags) { struct log_stream *ls = log_new_stream(sizeof(struct file_stream)); struct file_stream *fs = (struct file_stream *) ls; fs->fd = fd; + fs->flags = flags; ls->msgfmt = LSFMT_DEFAULT; ls->handler = file_handler; ls->close = file_close; @@ -129,16 +124,16 @@ log_new_fd(int fd) return ls; } -static struct log_stream * -do_log_new_file(const char *path, uns more_flags) +struct log_stream * +log_new_file(const char *path, uint flags) { struct log_stream *ls = log_new_stream(sizeof(struct file_stream)); struct file_stream *fs = (struct file_stream *) ls; fs->fd = -1; + fs->flags = FF_CLOSE_FD | flags; fs->orig_name = xstrdup(path); if (strchr(path, '%')) - fs->flags = FF_FORMAT_NAME; - fs->flags |= FF_CLOSE_FD | more_flags; + fs->flags |= FF_FORMAT_NAME; ls->msgfmt = LSFMT_DEFAULT; ls->handler = file_handler; ls->close = file_close; @@ -150,12 +145,6 @@ do_log_new_file(const char *path, uns more_flags) return ls; } -struct log_stream * -log_new_file(const char *path) -{ - return do_log_new_file(path, 0); -} - int log_switch(void) { @@ -189,10 +178,7 @@ log_file(const char *name) if (!name) return; - struct log_stream *ls = do_log_new_file(name, FF_FD2_FOLLOWS); - struct log_stream *def = log_stream_by_flags(0); - log_rm_substream(def, NULL); - log_add_substream(def, ls); + log_set_default_stream(log_new_file(name, FF_FD2_FOLLOWS)); } #ifdef TEST @@ -201,10 +187,11 @@ int main(int argc, char **argv) { log_init(argv[0]); log_file("/proc/self/fd/1"); - // struct log_stream *ls = log_new_fd(1); - // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S"); + // struct log_stream *ls = log_new_fd(1, 0); + // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S", 0); for (int i=1; i