-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.cc
32 lines (28 loc) · 930 Bytes
/
log.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) 2010 David Caldwell, All Rights Reserved.
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include "log.h"
static const char *const priority_name[8] = { "Emergency", "Alert", "Critical", "Error", "Warning", "Notice", "Info", "Debug" };
static bool use_syslog;
static int log_level;
void init_log(bool _use_syslog, int minimum_log_level)
{
use_syslog = _use_syslog;
log_level = minimum_log_level;
openlog("daemon-manager", LOG_NDELAY | LOG_PID, LOG_DAEMON);
setlogmask(LOG_UPTO(log_level));
}
void log(int priority, const char *message, ...)
{
va_list ap;
va_start(ap, message);
if (use_syslog)
vsyslog(priority, (std::string("[")+priority_name[priority]+"] "+message).c_str(), ap);
else if (priority <= log_level) {
fprintf(stderr, "[%s] ", priority_name[priority]);
vfprintf(stderr, message, ap);
}
va_end(ap);
}