syslog-async

Category: c library
Tags: C library

A non-blocking syslog() replacement.

History

Created by Simon Kelly during the development of dnsmasq.

FAQ

What is it?

Syslog that does not block.

How cool is it?

Rather.

Competitors?

liblogfaf

When to use it?

When you want want a nonblockin syslog replacement.

Is it dead?

No, it seems to be rather complete.

Where is it?

http://www.thekelleys.org.uk/syslog-async/READ-ME

How to use it

The documentation and the examples are rather good.

Download

file: 1_download.sh

#!/bin/bash

mkdir -p tmp
cd ./tmp
wget -q http://www.thekelleys.org.uk/syslog-async/syslog-async-0.2.tar.gz
tar -xaf syslog-async-0.2.tar.gz
cd syslog-async-0.2
sed -i.bak -e "s/Fpic/fPIC/" Makefile
make
cp libsyslogasync.so.1.0 libsyslogasync.so

Init

The great thing is that that you can use name groups directly. Here is one examples, albeit a little modified.

file: main.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <string.h>
#include <unistd.h>

#include "syslog_async.h"

int main(int argc, char *argv[]) {
    int opt, fd, size, flags= LOG_PERROR | LOG_CONS | LOG_PID;
    fd_set rs, ws;
    char *buf, *end, *nl;

    buf = end = strdup("logging stuff");
    size = strlen(buf);
    openlog_async("groksome.com", flags, LOG_USER);

    FD_ZERO(&ws);
    if ((fd = log_fd_async()) != -1) {
        FD_SET(fd, &ws);
    }
    // replace this by your eventloop
    if (select(10, NULL, &ws, NULL, NULL) < 0) {
        FD_ZERO(&ws);
    }

    if (fd != -1 && FD_ISSET(fd, &ws)) {
        log_write_async();
    }
    for (nl = buf; nl < end; nl++) {
        if (*nl == '\n') {
            *nl = 0;
            syslog_async(LOG_CRIT, "%s", buf);
            memmove(buf, nl + 1, end - (nl + 1));
            end -= nl - buf + 1;
            nl = buf;
        }
    }

    closelog_async();
    free(buf);
}

file: 2_compile_and_run.sh

#!/bin/bash

clang \
    main.c \
    -Wall -Wextra -Weverything \
        -Wno-unused-parameter \
        -Wno-reserved-id-macro \
        -Wno-unused-variable \
        -Wno-shorten-64-to-32 \
        -Wno-sign-conversion \
    -o ./tmp/main \
    -I ./tmp/syslog-async-0.2/ \
    -L ./tmp/syslog-async-0.2/ -lsyslogasync

Further reading:

We only tipped the top of the iceberg here. There is much more:

https://tools.ietf.org/html/rfc5424