/*
* UCW Library -- Keeping of Log Files
*
- * (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ * (c) 1997--2006 Martin Mares <mj@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
#include "lib/lib.h"
+#include "lib/lfs.h"
+#include "lib/threads.h"
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
static char *log_name_patt;
static int log_params;
static int log_filename_size;
-int log_switch_nest;
+static int log_switch_nest;
-static void
+static int
do_log_switch(struct tm *tm)
{
int fd, l;
char name[log_filename_size];
+ int switched = 0;
if (!log_name_patt ||
log_filename[0] && !log_params)
- return;
+ return 0;
+ ucwlib_lock();
log_switch_nest++;
l = strftime(name, log_filename_size, log_name_patt, tm);
if (l < 0 || l >= log_filename_size)
if (strcmp(name, log_filename))
{
strcpy(log_filename, name);
- fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ fd = sh_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
if (fd < 0)
die("Unable to open log file %s: %m", name);
- close(2);
- dup(fd);
+ dup2(fd, 2);
close(fd);
- close(1);
- dup(2);
+ switched = 1;
}
log_switch_nest--;
+ ucwlib_unlock();
+ return switched;
}
-void
+int
log_switch(void)
{
time_t tim = time(NULL);
- do_log_switch(localtime(&tim));
+ return do_log_switch(localtime(&tim));
}
static void
}
void
-log_file(byte *name)
+log_file(const char *name)
{
if (name)
{
log_filename[0] = 0;
log_switch();
log_switch_hook = internal_log_switch;
- close(0);
- open("/dev/null", O_RDWR, 0);
}
}
log_pid = getpid();
}
+void
+log_switch_disable(void)
+{
+ log_switch_nest++;
+}
+
+void
+log_switch_enable(void)
+{
+ ASSERT(log_switch_nest);
+ log_switch_nest--;
+}
+
#ifdef TEST
int main(int argc, char **argv)