Since some people work better from an example, the following is an example of both a library using libnslog, and a client application using that library.
The source is only inlined here, and not directly tested, but it should be moderately complete and thus functional. It was taken, in part, from the test suite so that you can be reasonably confident it works.
The library
First we need a public header:
#ifndef LIBEXAMPLE_H
#define LIBEXAMPLE_H
extern void example_do_stuff(void);
#endif
#define NSLOG_DECLARE_CATEGORY(catname)
Next we need a private header, because libexample has some more categories...
#ifndef LIBEXAMPLE__HIDDEN_H
#define LIBEXAMPLE__HIDDEN_H
#include "libexample.h"
void libexample__hidden_func();
#endif
Finally let's have some libexample code:
#include "hidden.h"
void example_do_stuff(void)
{
NSLOG(interesting, INFO,
"Did you know? Categories can be realised anywhere!");
libexample__hidden_func();
NSLOG(libexample, INFO,
"All done, good bye %s",
"Mr Bond");
}
#define NSLOG(catname, level, logmsg, args...)
#define NSLOG_DEFINE_CATEGORY(catname, description)
And because functionality may be spread among files:
#include "hidden.h"
void libexample__hidden_func(void)
{
NSLOG(libexample, INFO,
"Yay, top level stuff");
NSLOG(boring, DEBUG,
"Boring debug number: %d", 18);
}
#define NSLOG_DEFINE_SUBCATEGORY(parentcatname, catname, description)
The above, compiled together with libnslog's headers, will result in a library.
The client application
Since it's easy enough to do, we'll show a client application in a single file. It ought to be well enough commented to be of use...
#include "libexample.h"
#include <stdio.h>
#include <stdarg.h>
static void
const char *fmt, va_list args)
{
UNUSED(_ctx);
fprintf(stderr,
"EXAMPLE LOG MESSAGE:\n"
"Category name: %.*s\n",
"Category description: %s\n",
"Logging level: %s\n",
"Source location: %.*s (line %d function %.*s)\n",
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n\n");
}
int
main(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
example_do_stuff();
fprintf(stderr, "Unable to set render callback\n");
return 1;
}
fprintf(stderr, "Before uncork...\n");
fprintf(stderr, "Unable to uncork!\n");
return 2;
}
fprintf(stderr, "After uncork.\n");
example_do_stuff();
fprintf(stderr, "Giving up, unable to parse filter.\n");
return 3;
}
fprintf(stderr, "Unable to set active filter, stopping.\n");
return 4;
}
example_do_stuff();
fprintf(stderr, "Unable to clear active filter, stopping.\n");
return 4;
}
example_do_stuff();
return 0;
}
const char * nslog_level_name(nslog_level level)
nslog_error nslog_set_render_callback(nslog_callback cb, void *context)
nslog_error nslog_filter_set_active(nslog_filter_t *filter, nslog_filter_t **prev)
nslog_error nslog_uncork(void)
nslog_filter_t * nslog_filter_unref(nslog_filter_t *filter)
nslog_error nslog_filter_from_text(const char *input, nslog_filter_t **output)
struct nslog_filter_s nslog_filter_t
nslog_category_t * category