ossp-pkg/l2/l2_test.c
1.12
/*
** L2 - OSSP Logging Library
** Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
** Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
**
** This file is part of OSSP L2, a flexible logging library which
** can be found at http://www.ossp.org/pkg/l2/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** l2_test.c: C API test suite
*/
#include <stdio.h>
#include <sys/types.h> /* following group of includes are for */
#include <sys/socket.h> /* socket testing only */
#include <netinet/in.h>
#include <netdb.h>
#include "l2.h"
static void die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "l2_test:ERROR: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
static l2_result_t
formatter(l2_context_t *ctx, const char id, const char *param,
char *bufptr, size_t bufsize, size_t *buflen, va_list *ap)
{
int i;
i = va_arg(*ap, int);
sprintf(bufptr, "[%d/%s]", i, param);
*buflen = strlen(bufptr);
return L2_OK;
}
int main(int argc, char *argv[])
{
l2_channel_t *chPrefix;
l2_channel_t *chBuf;
l2_channel_t *chFile;
l2_channel_t *chSock;
l2_stream_t *st;
/*
* Typical steps to use a buffered file logging stream
*/
if ((st = l2_stream_create()) == NULL)
die("failed to create stream");
/* Atenzione! Before doing any socket testing, make sure you have a valid */
/* end point listening, or else you will only get an error message when */
/* the thing tries to connect. */
/* if ((chSock = l2_channel_create(&l2_handler_socket)) == NULL)
die("failed to create socket channel");
if (l2_channel_configure(chSock, "ipversion,host,port", AF_INET,\
"localhost", 2002) != L2_OK)
die("failed to configure socket ipv4 channel");
if (l2_channel_configure(chSock, "ipversion,host,port", AF_INET6,\
"0123:4567:4455:6677:8899:AABB:9876:5432", 2002) != L2_OK)
die("failed to configure socket ipv6 channel");
if (l2_channel_open(chSock) != L2_OK)
die("failed to open socket channel");
if (l2_stream_channel(st, chSock, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
die("failed to attach first channel into stream");
*/
if ((chPrefix = l2_channel_create(&l2_handler_prefix)) == NULL) /* Prefix */
die("failed to create prefix channel");
if (l2_channel_configure(chPrefix, "timefmt,timezone", "[%d-%m-%Y/%H:%M:%S] ", "local") != L2_OK)
die("failed to configure prefix channel");
if ((chBuf = l2_channel_create(&l2_handler_buffer)) == NULL) /* Buffer */
die("failed to create buffer channel");
if (l2_channel_configure(chBuf, "size", 100) != L2_OK)
die("failed to configure buffer channel");
if ((chFile = l2_channel_create(&l2_handler_file)) == NULL) /* File */
die("failed to create file channel");
if (l2_channel_configure(chFile, "path,append,perm", "l2_test.log", TRUE, 0644) != L2_OK)
die("failed to configure file channel");
if (l2_channel_stack(chFile, chBuf) != L2_OK)
die("failed to stack buffer channel on top of file channel");
if (l2_channel_stack(chBuf, chPrefix) != L2_OK)
die("failed to stack prefix channel on top of buffer channel");
if (l2_channel_open(chPrefix) != L2_OK)
die("failed to open channel stack");
if (l2_stream_channel(st, chPrefix, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
die("failed to attach channel stack into stream");
if (l2_stream_levels(st, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
die("failed to set global logging level");
if (l2_stream_formatter(st, 'k', formatter, NULL) != L2_OK)
die("failed to configure formatter for %%x");
if (l2_stream_log(st, L2_LEVEL_PANIC, "Checking localhost %s %{myparm}k\n", "foo", 12345) != L2_OK)
die("failed to log message to stream");
if (l2_stream_destroy(st) != L2_OK)
die("failed to destroy stream");
return 0;
}