OSSP CVS Repository

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

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

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 *ch;
    l2_channel_t *chFilter;
    l2_channel_t *chPrefix;
    l2_channel_t *chBuffer;
    l2_channel_t *chFile;
    l2_channel_t *chSyslog;
    l2_channel_t *chSmtp;
    l2_env_t *env;

    /* create environment */
    if ((l2_env_create(&env)) != L2_OK)
        die("failed to create environment");
    if (l2_env_formatter(env, 'k', formatter, NULL) != L2_OK)
        die("failed to configure formatter for %%x");
    if (l2_env_formatter(env, 'S', l2_util_fmt_dump, NULL) != L2_OK)
        die("failed to configure formatter for %%S");

    /* create noop channel */
    if ((l2_channel_create(&ch, env, &l2_handler_noop)) != L2_OK)
        die("failed to create noop channel");

    /* create prefix channel */
    if ((l2_channel_create(&chPrefix, env, &l2_handler_prefix)) != L2_OK)
        die("failed to create prefix channel");
    if (l2_channel_configure(chPrefix, "prefix,timezone", 
                             "[%d-%m-%Y/%H:%M:%S] %L test[%P]: ", "local") != L2_OK)
        die("failed to configure prefix channel");

    /* create prefix channel */
    if ((l2_channel_create(&chFilter, env, &l2_handler_filter)) != L2_OK)
        die("failed to create filter channel");
    if (l2_channel_configure(chFilter, "regex,negate", "hecking", 0) != L2_OK)
        die("failed to configure filter channel");

    /* create buffer channel */
    if ((l2_channel_create(&chBuffer, env, &l2_handler_buffer)) != L2_OK)
        die("failed to create buffer channel");
    if (l2_channel_configure(chBuffer, "size", 800) != L2_OK)
        die("failed to configure buffer channel");

    /* create file channel */
    if ((l2_channel_create(&chFile, env, &l2_handler_file)) != L2_OK)
        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_levels(chFile, L2_LEVEL_UPTO(L2_LEVEL_INFO), L2_LEVEL_NONE) != L2_OK)
        die("failed to level of smtp channel");

    /* create syslog channel */
    if ((l2_channel_create(&chSyslog, env, &l2_handler_syslog)) != L2_OK)
        die("failed to create syslog channel");
    if (l2_channel_configure(chSyslog, "ident,facility,target,remotehost,logpid",
                             "L2-Test", "user", "remote", "en1", 1) != L2_OK)
        die("failed to configure syslog channel");
    if (l2_channel_levels(chSyslog, L2_LEVEL_UPTO(L2_LEVEL_ERROR), L2_LEVEL_ALL) != L2_OK)
        die("failed to level of syslog channel");

    /* create smtp channel */
    if ((l2_channel_create(&chSmtp, env, &l2_handler_smtp)) != L2_OK)
        die("failed to create smtp channel");
    if (l2_channel_configure(chSmtp, "rcpt,host,port", "rse@engelschall.com", "en1", "25") != L2_OK)
        die("failed to configure smtp channel");
    if (l2_channel_levels(chSmtp, L2_LEVEL_UPTO(L2_LEVEL_PANIC), L2_LEVEL_ALL) != L2_OK)
        die("failed to level of smtp channel");

    /* build channel tree */
    if (l2_channel_link(ch, L2_LINK_CHILD, chFilter, chPrefix, chBuffer, chFile, NULL) != L2_OK)
        die("failed to link file-related channels together as a child sequence");
    if (l2_channel_link(ch, L2_LINK_CHILD, chSyslog, NULL) != L2_OK)
        die("failed to link syslog-related channels together as a child sequence");

    /* open channel tree */
    if (l2_channel_open(ch) != L2_OK)
        die("failed to open channel tree");
    
    /* perform a few log operations */
    if (l2_channel_log(ch, L2_LEVEL_PANIC, "1: Checking localhost %s %{myparm}k <%{text}S><%{hex}S><%{base64}S>\n", "foo", 12345, "foo\1bar", 7, "foo\1bar", 7, "foo\1bar", 7) != L2_OK)
        die("failed to log message #1 to channel");
    if (l2_channel_log(ch, L2_LEVEL_PANIC, "2: Shecking\n") != L2_OK)
        die("failed to log message #2 to channel");
    if (l2_channel_log(ch, L2_LEVEL_PANIC, "3: Checking localhost %s %{myparm}k <%{text}S><%{hex}S><%{base64}S>\n", "foo", 12345, "foo\1bar", 7, "foo\1bar", 7, "foo\1bar", 7) != L2_OK)
        die("failed to log message #3 to channel");
    if (l2_channel_log(ch, L2_LEVEL_PANIC, "4: Shecking\n") != L2_OK)
        die("failed to log message #4 to channel");

    /* destroy channel tree */
    if (l2_channel_destroy(ch) != L2_OK)
        die("failed to destroy channel tree");

    return 0;
}


CVSTrac 2.0.1