aboutsummaryrefslogtreecommitdiffstats
path: root/src/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c153
1 files changed, 88 insertions, 65 deletions
diff --git a/src/log.c b/src/log.c
index 0eba2f1..3e94f30 100644
--- a/src/log.c
+++ b/src/log.c
@@ -10,6 +10,9 @@ static char rcsid[] = "$Id$";
/*
* $Log$
+ * Revision 1.18 2012/06/22 10:22:24 james
+ * *** empty log message ***
+ *
* Revision 1.17 2011/02/04 16:14:16 james
* *** empty log message ***
*
@@ -65,7 +68,8 @@ static char rcsid[] = "$Id$";
#include "project.h"
-typedef struct {
+typedef struct
+{
LOG_SIGNATURE;
int do_close;
int rotate;
@@ -83,17 +87,18 @@ sighup (int dummy)
{
Log *l;
- for (l = loggers; l; l = l->next) {
- if (l->sighup)
- l->sighup (l);
- }
+ for (l = loggers; l; l = l->next)
+ {
+ if (l->sighup)
+ l->sighup (l);
+ }
}
void
log_register_handlers (void)
{
- struct sigaction sa = { 0 };
+ struct sigaction sa = { {0} };
sa.sa_handler = sighup;
sa.sa_flags = SA_RESTART;
@@ -123,13 +128,15 @@ log_remove (Log * l)
*ptr = l->next;
}
-static void flog_newline(Log *_l,int force)
+static void
+flog_newline (Log * _l, int force)
{
File_Log *l = (File_Log *) _l;
- if (force || !l->needs_newline) return;
+ if (force || !l->needs_newline)
+ return;
- l->needs_newline=0;
+ l->needs_newline = 0;
fputc ('\n', l->fp);
fflush (l->fp);
@@ -150,16 +157,18 @@ flog_sighup (Log * _l)
log_f (_l, "<sighup received - opening log file>");
}
-static void
-flog_emit_stamp(Log *_l)
+static void
+flog_emit_stamp (Log * _l)
{
File_Log *l = (File_Log *) _l;
struct timeval tv = { 0 };
struct tm *tm;
time_t t;
+#if 0
static const char *days[] = { "Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat"
};
+#endif
static const char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"
@@ -168,49 +177,56 @@ flog_emit_stamp(Log *_l)
if (!l->fp)
return;
- flog_newline(_l,0);
+ flog_newline (_l, 0);
gettimeofday (&tv, NULL);
t = tv.tv_sec;
tm = localtime (&t);
fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon],
- tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,(int) tv.tv_usec);
+ tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
+ (int) tv.tv_usec);
}
-static void flog_check_rotate(Log *_l)
+static void
+flog_check_rotate (Log * _l)
{
File_Log *l = (File_Log *) _l;
- if (l->rotate && rotate_check (l->filename)) {
- fclose (l->fp);
- rotate (l->filename);
- l->fp = fopen (l->filename, "a+");
- }
+ if (l->rotate && rotate_check (l->filename))
+ {
+ fclose (l->fp);
+ rotate (l->filename);
+ l->fp = fopen (l->filename, "a+");
+ }
}
static void
-flog_log_bytes (Log * _l, void *_buf,int len)
+flog_log_bytes (Log * _l, void *_buf, int len)
{
File_Log *l = (File_Log *) _l;
- uint8_t *buf=(uint8_t *) _buf;
+ uint8_t *buf = (uint8_t *) _buf;
if (!l->fp)
return;
- while (len--) {
- if (*buf=='\n') {
- flog_newline(_l,1);
- flog_check_rotate(_l);
- flog_emit_stamp(_l);
- } else {
- l->needs_newline++;
- fputc (*buf, l->fp);
- }
- buf++;
- }
+ while (len--)
+ {
+ if (*buf == '\n')
+ {
+ flog_newline (_l, 1);
+ flog_check_rotate (_l);
+ flog_emit_stamp (_l);
+ }
+ else
+ {
+ l->needs_newline++;
+ fputc (*buf, l->fp);
+ }
+ buf++;
+ }
}
static void
@@ -221,13 +237,13 @@ flog_log (Log * _l, char *buf)
if (!l->fp)
return;
- flog_emit_stamp(_l);
+ flog_emit_stamp (_l);
fputs (buf, l->fp);
fputc ('\n', l->fp);
fflush (l->fp);
- flog_check_rotate(_l);
+ flog_check_rotate (_l);
}
@@ -252,17 +268,21 @@ file_log_new (char *fn, int rotate)
l = xmalloc (sizeof (File_Log));
- if (fn && strcmp (fn, "-")) {
- l->fp = fopen (fn, "a+");
- if (!l->fp) {
- free (l);
- return NULL;
+ if (fn && strcmp (fn, "-"))
+ {
+ l->fp = fopen (fn, "a+");
+ if (!l->fp)
+ {
+ free (l);
+ return NULL;
+ }
+ l->sighup = flog_sighup;
+ }
+ else
+ {
+ l->fp = stderr;
+ dc = 0;
}
- l->sighup = flog_sighup;
- } else {
- l->fp = stderr;
- dc = 0;
- }
l->log = flog_log;
@@ -272,7 +292,7 @@ file_log_new (char *fn, int rotate)
l->rotate = rotate;
l->filename = strdup (fn);
- l->needs_newline=0;
+ l->needs_newline = 0;
fput_cp (l->fp, 0xffef);
@@ -293,32 +313,35 @@ log_f (Log * log, char *fmt, ...)
if (!log)
return;
- if (!size) {
- size = 128;
- buf = malloc (size);
- }
+ if (!size)
+ {
+ size = 128;
+ buf = malloc (size);
+ }
if (!buf)
return;
- while (1) {
- va_start (ap, fmt);
- n = vsnprintf (buf, size, fmt, ap);
- va_end (ap);
+ while (1)
+ {
+ va_start (ap, fmt);
+ n = vsnprintf (buf, size, fmt, ap);
+ va_end (ap);
- if (n > -1 && n < size) {
- log->log (log, buf);
- return;
- }
+ if (n > -1 && n < size)
+ {
+ log->log (log, buf);
+ return;
+ }
- if (n > -1) /* glibc 2.1 */
- size = n + 1;
- else /* glibc 2.0 */
- size *= 2; /* twice the old size */
+ if (n > -1) /* glibc 2.1 */
+ size = n + 1;
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
- buf = xrealloc (buf, size);
+ buf = xrealloc (buf, size);
- if (!buf)
- return;
- }
+ if (!buf)
+ return;
+ }
}