Commit 5183121d authored by Marek Vavrusa's avatar Marek Vavrusa

Initial syslog-based logging facility.

parent bf8ff860
Makefile
src/common.h
src/main.c
src/dns/dns-simple.c
src/dns/dns-simple.h
src/hash/cuckoo-hash-table.c
src/hash/cuckoo-hash-table.h
src/hash/hash-functions.c
src/hash/hash-functions.h
src/hash/universal-system.c
src/hash/universal-system.h
src/other/print.c
src/other/print.h
src/server/socket-manager.c
src/server/socket-manager.h
src/server/dispatcher.c
......@@ -32,8 +27,9 @@ src/server/zone-parser.c
src/server/zone-parser.h
src/server/server.c
src/server/server.h
src/tests/tests.c
src/tests/tests.h
src/tests/cuckoo-test.c
src/tests/cuckoo-test.h
src/other/log.h
src/other/log.c
#ifndef COMMON
#define COMMON
#include "print.h"
/* Common types and macros.
*/
typedef unsigned int uint;
#define PROJECT_NAME "CuteDNS" // Project name
#define PROJECT_VER 0x000001 // 0xMMIIRR (MAJOR,MINOR,REVISION)
#define ERR_ALLOC_FAILED fprintf(stderr, "Allocation failed.\n")
/* Common includes.
*/
#include "print.h"
#include "log.h"
#endif // COMMON
......@@ -8,18 +8,23 @@
int main( int argc, char **argv )
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>.\n", argv[0]);
print_msg(LOG_ERR, "Usage: %s <filename>.\n", argv[0]);
return -1;
}
// Open log
log_open(LOG_UPTO(LOG_ERR), LOG_MASK(LOG_ERR)|LOG_MASK(LOG_WARNING));
// Start server
cute_server *server = cute_create();
int res;
if ((res = cute_start(server, argv[1])) != 0) {
fprintf (stderr, "Problem starting the server, exiting..\n");
log_error("Problem starting the server, exiting..\n");
}
// Stop server and close log
cute_destroy(&server);
log_close();
return res;
}
#include <stdarg.h>
#include <stdio.h>
#include "log.h"
#include "common.h"
/// Global log-level.
static int _LOG_OPEN = 0;
static int _LOG_MASK = ~0;
int log_open(int print_mask, int log_mask)
{
setlogmask(log_mask);
openlog(PROJECT_NAME, LOG_CONS|LOG_PID|LOG_NDELAY,LOG_LOCAL1); /// \todo May change to LOG_DAEMON.
_LOG_MASK = print_mask;
_LOG_OPEN = 1;
return 0;
}
int log_close()
{
_LOG_OPEN = 0;
_LOG_MASK = ~0;
closelog();
return 0;
}
int print_msg(int level, const char* msg, ...)
{
// Get output stream
va_list ap;
FILE* stream = stdout;
if(level & (LOG_ERR|LOG_WARNING|LOG_CRIT|LOG_ALERT))
stream = stderr;
// Check mask
int ret = 0;
if(level & _LOG_MASK) {
va_start(ap, msg);
ret = vfprintf(stream, msg, ap);
va_end (ap);
}
return ret;
}
/** \file log.h
* Logging facility.
*/
#ifndef __log_h__
#define __log_h__
/* Loglevel defined in syslog.h, may be redefined in other backend, but keep naming.
* LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
*/
#include <syslog.h>
/* Logging facility setup. */
int log_open(int print_mask, int log_mask);
int log_close();
/* Logging functions. */
int print_msg(int level, const char* msg, ...) __attribute__((format (printf, 2, 3)));
#define log_msg(level, msg...) \
syslog((level), msg); \
print_msg((level), msg)
/* Convenient logging. */
#define log_error(msg...) log_msg(LOG_ERR, msg)
#define log_warning(msg...) log_msg(LOG_WARNING, msg)
#define log_notice(msg...) log_msg(LOG_NOTICE, msg)
#define log_info(msg...) log_msg(LOG_INFO, msg)
#define log_debug(msg...) log_msg(LOG_DEBUG, msg)
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment