]> mj.ucw.cz Git - libucw.git/blob - ucw/log-file.c
65e9eb85df41541ccba86c0d0bf3c5d58d80f8bd
[libucw.git] / ucw / log-file.c
1 /*
2  *      UCW Library -- Logging to Files
3  *
4  *      (c) 1997--2009 Martin Mares <mj@ucw.cz>
5  *      (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/log.h"
13 #include "ucw/lfs.h"
14 #include "ucw/threads.h"
15 #include "ucw/simple-lists.h"
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <time.h>
22
23 struct file_stream {
24   struct log_stream ls;         // ls.name is the current name of the log file
25   int fd;
26   uns flags;                    // FF_xxx
27   char *orig_name;              // Original name with strftime escapes
28 };
29
30 enum log_file_flag {
31   FF_FORMAT_NAME = 1,           // Name contains strftime escapes
32   FF_CLOSE_FD = 2,              // Close the fd with the stream
33 };
34
35 #define MAX_EXPAND 64           // Maximum size of expansion of strftime escapes
36
37 static int log_switch_nest;
38
39 static void
40 do_log_reopen(struct file_stream *fs, const char *name)
41 {
42   int fd = ucw_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
43   if (fd < 0)
44     die("Unable to open log file %s: %m", name);
45   if (fs->fd < 0)
46     fs->fd = fd;
47   else
48     {
49       dup2(fd, fs->fd);
50       close(fd);
51     }
52   if (fs->ls.name)
53     {
54       xfree(fs->ls.name);
55       fs->ls.name = NULL;       // We have to keep this consistent, die() below can invoke logging
56     }
57   fs->ls.name = xstrdup(name);
58 }
59
60 static int
61 do_log_switch(struct file_stream *fs, struct tm *tm)
62 {
63   if (!(fs->flags & FF_FORMAT_NAME))
64     {
65       if (fs->fd >= 0)
66         return 1;
67       else
68         {
69           do_log_reopen(fs, fs->orig_name);
70           return 1;
71         }
72     }
73
74   int buflen = strlen(fs->orig_name) + MAX_EXPAND;
75   char name[buflen];
76   int switched = 0;
77
78   ucwlib_lock();
79   if (!log_switch_nest)         // Avoid infinite loops if we die when switching logs
80     {
81       log_switch_nest++;
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))
86         {
87           do_log_reopen(fs, name);
88           switched = 1;
89         }
90       log_switch_nest--;
91     }
92   ucwlib_unlock();
93   return switched;
94 }
95
96 /* Emulate the old single-file interface: close the existing log file and open a new one. */
97 void
98 log_file(const char *name)
99 {
100   if (!name)
101     return;
102
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
108 }
109
110 /* destructor for standard files */
111 static void
112 file_close(struct log_stream *ls)
113 {
114   struct file_stream *fs = (struct file_stream *) ls;
115   if ((fs->flags & FF_CLOSE_FD) && fs->fd >= 0)
116     close(fs->fd);
117   xfree(fs->ls.name);
118   xfree(fs->orig_name);
119 }
120
121 /* handler for standard files */
122 static int
123 file_handler(struct log_stream *ls, struct log_msg *m)
124 {
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);
128
129   int r = write(fs->fd, m->m, m->m_len);
130   /* FIXME: check for errors here? */
131   return 0;
132 }
133
134 /* assign log to a file descriptor */
135 /* initialize with the default formatting, does NOT close the descriptor */
136 struct log_stream *
137 log_new_fd(int fd)
138 {
139   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
140   struct file_stream *fs = (struct file_stream *) ls;
141   fs->fd = fd;
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);
147   return ls;
148 }
149
150 /* open() a file (append mode) */
151 /* initialize with the default formatting */
152 struct log_stream *
153 log_new_file(const char *path)
154 {
155   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
156   struct file_stream *fs = (struct file_stream *) ls;
157   fs->fd = -1;
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;
165
166   time_t now = time(NULL);
167   struct tm *tm = localtime(&now);
168   ASSERT(tm);
169   do_log_switch(fs, tm);                // die()'s on errors
170   return ls;
171 }
172
173 int
174 log_switch(void)
175 {
176   time_t now = time(NULL);
177   struct tm *tm = localtime(&now);
178   ASSERT(tm);
179
180   int switched = 0;
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);
184   return switched;
185 }
186
187 void
188 log_switch_disable(void)
189 {
190   log_switch_nest++;
191 }
192
193 void
194 log_switch_enable(void)
195 {
196   ASSERT(log_switch_nest);
197   log_switch_nest--;
198 }
199
200 #ifdef TEST
201
202 int main(int argc, char **argv)
203 {
204   log_init(argv[0]);
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]);
210   return 0;
211 }
212
213 #endif