From 4ba78138d5bc60aed33d2ec8fc56ceee96b6f540 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 7 Jan 2012 14:33:43 +0100 Subject: [PATCH] Logging: Added log_set_default_stream() --- ucw/doc/log.txt | 4 ++-- ucw/log-stream.c | 11 ++++++++++- ucw/log.h | 11 +++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ucw/doc/log.txt b/ucw/doc/log.txt index 28265f72..61e443e9 100644 --- a/ucw/doc/log.txt +++ b/ucw/doc/log.txt @@ -64,9 +64,9 @@ When a log stream receives a message, it is processed as follows: When no stream is explicitly selected, msg() uses the default stream, which has registration number 0 and which is also returned by log_default_stream(). This stream has no explicit destination, but it can have substreams. (When -a program starts, the default stream is connected to stderr; a call to log_file() +a program starts, the default stream is connected to stderr. A call to log_file() establishes a file logging stream and links it as the only substream of the -default stream.) +default stream. If you want to do that with any other log stream, call log_set_default_stream().) Streams are reference-counted. When a stream is created, it gets reference count 1. When it is linked as a substream of another stream, its reference count is incremented. diff --git a/ucw/log-stream.c b/ucw/log-stream.c index 5af9f3e6..2cc9f7a0 100644 --- a/ucw/log-stream.c +++ b/ucw/log-stream.c @@ -2,7 +2,7 @@ * UCW Library -- Logging: Management of Log Streams * * (c) 2008 Tomas Gavenciak - * (c) 2009 Martin Mares + * (c) 2009--2012 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -170,6 +170,15 @@ log_set_format(struct log_stream *ls, uns mask, uns data) log_set_format(i->p, mask, data); } +void +log_set_default_stream(struct log_stream *ls) +{ + struct log_stream *def = log_stream_by_flags(0); + log_rm_substream(def, NULL); + log_add_substream(def, ls); + log_close_stream(ls); +} + /*** Registry of type names ***/ int log_register_type(const char *name) diff --git a/ucw/log.h b/ucw/log.h index f2d99a07..5e8d4ef5 100644 --- a/ucw/log.h +++ b/ucw/log.h @@ -179,6 +179,17 @@ static inline struct log_stream *log_default_stream(void) return log_stream_by_flags(0); } +/** + * Make the specified stream the default destination. + * + * In fact, it takes the fixed default stream and attaches @ls as its only + * substream. If there were any other substreams, they are removed. + * + * Log streams created by log_file() or log_configured() are made default + * by calling this function. + **/ +void log_set_default_stream(struct log_stream *ls); + /** * Close all open streams, un-initialize the module, free all memory and * reset the logging mechanism to use stderr only. -- 2.39.2