]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log-file.c
Configure: Implemented running of test programs
[libucw.git] / ucw / log-file.c
index 0b8ed66c3ce5534bf788e3473d86c908856f263a..efa0665f99bde467954390a085f267b6687e502c 100644 (file)
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
+#include <errno.h>
 
 struct file_stream {
   struct log_stream ls;                // ls.name is the current name of the log file
 
 struct file_stream {
   struct log_stream ls;                // ls.name is the current name of the log file
@@ -28,12 +29,6 @@ struct file_stream {
   char *orig_name;             // Original name with strftime escapes
 };
 
   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;
 #define MAX_EXPAND 64          // Maximum size of expansion of strftime escapes
 
 static int log_switch_nest;
@@ -93,7 +88,6 @@ do_log_switch(struct file_stream *fs, struct tm *tm)
   return switched;
 }
 
   return switched;
 }
 
-/* destructor for standard files */
 static void
 file_close(struct log_stream *ls)
 {
 static void
 file_close(struct log_stream *ls)
 {
@@ -104,7 +98,6 @@ file_close(struct log_stream *ls)
   xfree(fs->orig_name);
 }
 
   xfree(fs->orig_name);
 }
 
-/* handler for standard files */
 static int
 file_handler(struct log_stream *ls, struct log_msg *m)
 {
 static int
 file_handler(struct log_stream *ls, struct log_msg *m)
 {
@@ -113,18 +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);
     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);
 }
 
 }
 
-/* assign log to a file descriptor */
-/* initialize with the default formatting, does NOT close the descriptor */
 struct log_stream *
 struct log_stream *
-log_new_fd(int fd)
+log_new_fd(int fd, uns flags)
 {
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
   fs->fd = fd;
 {
   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;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
@@ -133,8 +124,8 @@ log_new_fd(int fd)
   return ls;
 }
 
   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, uns flags)
 {
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
 {
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
@@ -142,7 +133,7 @@ do_log_new_file(const char *path, uns more_flags)
   fs->orig_name = xstrdup(path);
   if (strchr(path, '%'))
     fs->flags = FF_FORMAT_NAME;
   fs->orig_name = xstrdup(path);
   if (strchr(path, '%'))
     fs->flags = FF_FORMAT_NAME;
-  fs->flags |= FF_CLOSE_FD | more_flags;
+  fs->flags |= FF_CLOSE_FD | flags;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
@@ -154,14 +145,6 @@ do_log_new_file(const char *path, uns more_flags)
   return ls;
 }
 
   return ls;
 }
 
-/* open() a file (append mode) */
-/* initialize with the default formatting */
-struct log_stream *
-log_new_file(const char *path)
-{
-  return do_log_new_file(path, 0);
-}
-
 int
 log_switch(void)
 {
 int
 log_switch(void)
 {
@@ -189,17 +172,17 @@ log_switch_enable(void)
   log_switch_nest--;
 }
 
   log_switch_nest--;
 }
 
-/* Emulate the old single-file interface: close the existing log file and open a new one. */
 void
 log_file(const char *name)
 {
   if (!name)
     return;
 
 void
 log_file(const char *name)
 {
   if (!name)
     return;
 
-  struct log_stream *ls = do_log_new_file(name, FF_FD2_FOLLOWS);
+  struct log_stream *ls = 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);
   struct log_stream *def = log_stream_by_flags(0);
   log_rm_substream(def, NULL);
   log_add_substream(def, ls);
+  log_close_stream(ls);
 }
 
 #ifdef TEST
 }
 
 #ifdef TEST
@@ -208,10 +191,11 @@ int main(int argc, char **argv)
 {
   log_init(argv[0]);
   log_file("/proc/self/fd/1");
 {
   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<argc; i++)
     msg(L_INFO, argv[i]);
   for (int i=1; i<argc; i++)
     msg(L_INFO, argv[i]);
+  log_close_all();
   return 0;
 }
 
   return 0;
 }