*/
#include "ucw/lib.h"
-#include "ucw/clists.h"
-#include "ucw/simple-lists.h"
#include "ucw/log.h"
+#include "ucw/simple-lists.h"
#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/time.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <alloca.h>
-#include <fcntl.h>
+
+/* Initial number of streams to allocate (must be >=2) */
+#define LS_INIT_STREAMS 8
/* Flag indicating initialization of the module */
-static int ls_initialized = 0;
+static int log_initialized = 0;
-/* The head of the list of freed log_streams indexes in ls_streams.ptr (-1 if none free).
- * Freed positions in ls_streams.ptr are connected into a linked list in the following way:
- * ls_streams.ptr[ls_streams_free].idata is the index of next freed position (or -1) */
-static int ls_streams_free = -1;
+/* The head of the list of freed log_streams indexes in log_streams.ptr (-1 if none free).
+ * Freed positions in log_streams.ptr are connected into a linked list in the following way:
+ * log_streams.ptr[log_streams_free].idata is the index of next freed position (or -1) */
+static int log_streams_free = -1;
/* Initialize the logstream module.
* It is not neccessary to call this explicitely as it is called by
- * the first ls_new() (for backward compatibility and ease of use). */
-static void ls_init_module(void)
+ * the first log_new_stream() (for backward compatibility and ease of use). */
+static void
+log_init_module(void)
{
- if (ls_initialized) return;
+ if (log_initialized)
+ return;
- /* create the grow array */
+ /* Create the growing array */
lsbuf_init(&log_streams);
lsbuf_set_size(&log_streams, LS_INIT_STREAMS);
bzero(log_streams.ptr, sizeof(struct log_stream*) * (log_streams.len));
- ls_streams_free = -1;
+ log_streams_free = -1;
- ls_initialized = 1;
+ log_initialized = 1;
/* init the default stream (0) as forwarder to fd2 */
- struct log_stream *ls = ls_new();
+ struct log_stream *ls = log_new_stream();
ASSERT(ls == log_streams.ptr[0]);
ASSERT(ls->regnum == 0);
ls->name = "default";
- ls_add_substream(ls, (struct log_stream *) &ls_default_log);
+ log_add_substream(ls, (struct log_stream *) &log_stream_default);
- /* log this */
- ls_msg(L_DEBUG, "logstream module initialized.");
+ // FIXME
+ msg(L_DEBUG, "logstream module initialized.");
}
-/* close all open streams, un-initialize the module, free all memory,
- * use only ls_default_log */
-void ls_close_all(void)
+/* Close all open streams, un-initialize the module, free all memory,
+ * and fall back to using only log_stream_default. */
+void
+log_close_all(void)
{
- int i;
+ if (!log_initialized)
+ return;
- if (!ls_initialized) return;
-
- for (i=0; i<log_streams_after; i++)
- {
- if (log_streams.ptr[i]->regnum>=0)
- ls_close(log_streams.ptr[i]);
- xfree(log_streams.ptr[i]);
- }
+ for (int i=0; i < log_streams_after; i++)
+ {
+ if (log_streams.ptr[i]->regnum >= 0)
+ log_close_stream(log_streams.ptr[i]);
+ xfree(log_streams.ptr[i]);
+ }
- /* set to the default state */
+ /* Back to the default state */
lsbuf_done(&log_streams);
- log_streams_after=0;
- ls_streams_free=-1;
- ls_initialized = 0;
+ log_streams_after = 0;
+ log_streams_free = -1;
+ log_initialized = 0;
}
-/* add a new substream, malloc()-ate a new simp_node */
-void ls_add_substream(struct log_stream *where, struct log_stream *what)
+/* Add a new substream, xmalloc()-ate a new simp_node. */
+void
+log_add_substream(struct log_stream *where, struct log_stream *what)
{
ASSERT(where);
ASSERT(what);
simp_node *n = xmalloc(sizeof(simp_node));
n->p = what;
- clist_add_tail(&(where->substreams), (cnode*)n);
+ clist_add_tail(&where->substreams, &n->n);
}
-/* remove all occurences of a substream, free() the simp_node */
-/* return number of deleted entries */
-int ls_rm_substream(struct log_stream *where, struct log_stream *what)
+/* Remove all occurences of a substream, xfree() the simp_node. */
+/* Return the number of deleted entries. */
+int
+log_rm_substream(struct log_stream *where, struct log_stream *what)
{
void *tmp;
- int cnt=0;
+ int cnt = 0;
ASSERT(where);
ASSERT(what);
CLIST_FOR_EACH_DELSAFE(simp_node *, i, where->substreams, tmp)
if (i->p == what)
- {
- clist_remove((cnode*)i);
- xfree(i);
- cnt++;
- }
+ {
+ clist_remove(&i->n);
+ xfree(i);
+ cnt++;
+ }
return cnt;
}
/* Return a pointer to a new stream with no handler and an empty substream list. */
-struct log_stream *ls_new(void)
+struct log_stream *
+log_new_stream(void)
{
struct log_stream *l;
int index;
- /* initialize the array if not initialized already */
- if (unlikely(ls_initialized==0))
- ls_init_module();
-
- /* there is no closed stream -- allocate a new one */
- if (ls_streams_free==-1)
- {
- /* check the size of the pointer array */
- lsbuf_grow(&log_streams, log_streams_after+1);
- ls_streams_free = log_streams_after++;
- log_streams.ptr[ls_streams_free] = xmalloc(sizeof(struct log_stream));
- log_streams.ptr[ls_streams_free]->idata = -1;
- log_streams.ptr[ls_streams_free]->regnum = -1;
- }
-
- ASSERT(ls_streams_free>=0);
-
- /* initialize the stream */
- index = ls_streams_free;
+ /* Initialize the data structures if needed */
+ log_init_module();
+
+ /* Get a free stream, possibly recycling a closed one */
+ if (log_streams_free < 0)
+ {
+ lsbuf_grow(&log_streams, log_streams_after+1);
+ index = log_streams_after++;
+ l = log_streams.ptr[log_streams_free] = xmalloc(sizeof(struct log_stream));
+ }
+ else
+ {
+ index = log_streams_free;
l = log_streams.ptr[index];
- ls_streams_free = l->idata;
- memset(l, 0, sizeof(struct log_stream));
+ log_streams_free = l->idata;
+ }
+
+ /* Initialize the stream */
+ bzero(l, sizeof(*l));
l->levels = LS_ALL_LEVELS;
l->regnum = LS_SET_STRNUM(index);
- l->substreams.head.next = &(l->substreams.head);
- l->substreams.head.prev = &(l->substreams.head);
+ clist_init(&l->substreams);
return l;
}
-/* Close and remember given log_stream */
-/* does not affect substreams, but frees the .substreams list */
-void ls_close(struct log_stream *ls)
+/* Close a stream, unlink (but do not close) all its substreams */
+void
+log_close_stream(struct log_stream *ls)
{
void *tmp;
ASSERT(ls);
/* xfree() all the simp_nodes from substreams */
CLIST_FOR_EACH_DELSAFE(simp_node *, i, ls->substreams, tmp)
- {
- clist_remove((cnode*)i);
- xfree(i);
- }
+ {
+ clist_remove(&i->n);
+ xfree(i);
+ }
- /* close and remember the stream */
- if (ls->close!=NULL)
+ /* Close the stream and add it to the free-list */
+ if (ls->close)
ls->close(ls);
- ls->idata = ls_streams_free;
- ls_streams_free = LS_GET_STRNUM(ls->regnum);
+ ls->idata = log_streams_free;
+ log_streams_free = LS_GET_STRNUM(ls->regnum);
ls->regnum = -1;
}
};
/* the default logger */
-extern const struct log_stream ls_default_log;
+extern const struct log_stream log_stream_default;
/* A message is processed as follows:
* 1. Discard if message level not in levels
// The module is initialized when a first stream is created.
// Before that only the default stream exists.
-// Initial number of streams to allocate (must be >=2)
-#define LS_INIT_STREAMS 8
-
/* Return pointer a new (xmalloc()-ated) stream with no handler and an empty substream list. */
-struct log_stream *ls_new(void);
+struct log_stream *log_new_stream(void);
/* Close and xfree() given log_stream */
/* Does not affect substreams */
-void ls_close(struct log_stream *ls);
+void log_close_stream(struct log_stream *ls);
/* close all open streams, un-initialize the module, free all memory,
* use only ls_default_log */
-void ls_close_all(void);
+void log_close_all(void);
/* add a new substream, xmalloc()-ate a new simp_node */
-void ls_add_substream(struct log_stream *where, struct log_stream *what);
+void log_add_substream(struct log_stream *where, struct log_stream *what);
/* remove all occurences of a substream, free() the simp_node */
/* return number of deleted entries */
-int ls_rm_substream(struct log_stream *where, struct log_stream *what);
+int log_rm_substream(struct log_stream *where, struct log_stream *what);
/* get a stream by its number (regnum) */
/* returns NULL for free numbers */
/* defaults to ls_default_stream for 0 when stream number 0 not set */
struct log_stream *log_stream_by_flags(uns flags);
-/* The proposed alternative to original vmsg() */
-void ls_vmsg(unsigned int cat, const char *fmt, va_list args);
-
-/* The proposed alternative to original msg() */
-void ls_msg(unsigned int cat, const char *fmt, ...);
-
-/* The proposed alternative to original die() */
-void ls_die(const char *fmt, ...);
-
/* process a message (string) (INTERNAL) */
/* depth prevents undetected looping */
/* returns 1 in case of loop detection or other fatal error
int log_pass_msg(int depth, struct log_stream *ls, const char *stime, const char *sutime,
const char *msg, uns cat);
-/* Maximal depth of ls_passmsg recursion */
-#define LS_MAX_DEPTH 64
-
/* Define an array (growing buffer) for pointers to log_streams. */
#define GBUF_TYPE struct log_stream*
#define GBUF_PREFIX(x) lsbuf_##x