OSSP CVS Repository

ossp - ossp-pkg/l2/l2_test.c 1.8
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/l2/l2_test.c 1.8
/*
**  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 "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);
}

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 *chBuf;
    l2_channel_t *chFile;
    l2_stream_t *st;

    /*
     * Typical steps to use a buffered file logging stream
     */

    if ((chBuf  = l2_channel_create(&l2_handler_buffer)) == NULL)
        die("failed to create buffer channel");

    if ((chFile = l2_channel_create(&l2_handler_file)) == NULL)
        die("failed to create file channel");

    if (l2_channel_configure(chBuf, "size", 100) != L2_OK)
        die("failed to configure buffer 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_open(chBuf) != L2_OK)
        die("failed to open buffer channel");

    if ((st = l2_stream_create()) == NULL)
        die("failed to create stream");

    if (l2_stream_channel(st, chBuf, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
        die("failed to attach channel into stream");

    if (l2_stream_levels(st, L2_LEVEL_UPTO(L2_LEVEL_WARNING), NULL) != 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, "test %s %{foo}k", "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;
}


CVSTrac 2.0.1