Commit 79bfa0aa authored by Daniel Salzman's avatar Daniel Salzman

log: constant and structure names cleanup

parent 7f98c2a6
......@@ -43,11 +43,11 @@ int use_journal = 0;
/*! Log context. */
typedef struct {
size_t facility_count; /*!< Log sink count. */
int *facility; /*!< Log sinks. */
size_t file_count; /*!< Open files count. */
FILE **file; /*!< Open files. */
logflag_t flags; /*!< Formatting flags. */
size_t target_count; /*!< Log target count. */
int *target; /*!< Log targets. */
size_t file_count; /*!< Open files count. */
FILE **file; /*!< Open files. */
log_flag_t flags; /*!< Formatting flags. */
} log_t;
/*! Log singleton. */
......@@ -68,13 +68,13 @@ static void sink_free(log_t *log)
for (int i = 0; i < log->file_count; ++i) {
fclose(log->file[i]);
}
free(log->facility);
free(log->target);
free(log->file);
free(log);
}
/*!
* \brief Create logging facilities respecting their canonical order.
* \brief Create logging targets respecting their canonical order.
*
* Facilities ordering: Syslog, Stderr, Stdout, File0...
*/
......@@ -86,20 +86,20 @@ static log_t *sink_setup(size_t file_count)
}
memset(log, 0, sizeof(*log));
// Reserve space for facilities.
log->facility_count = LOGT_FILE + file_count;
log->facility = malloc(LOG_ANY * sizeof(int) * log->facility_count);
if (!log->facility) {
// Reserve space for targets.
log->target_count = LOG_TARGET_FILE + file_count;
log->target = malloc(LOG_SOURCE_ANY * sizeof(int) * log->target_count);
if (!log->target) {
free(log);
return NULL;
}
memset(log->facility, 0, LOG_ANY * sizeof(int) * log->facility_count);
memset(log->target, 0, LOG_SOURCE_ANY * sizeof(int) * log->target_count);
// Reserve space for log files.
if (file_count > 0) {
log->file = malloc(sizeof(FILE *) * file_count);
if (!log->file) {
free(log->facility);
free(log->target);
free(log);
return NULL;
}
......@@ -117,34 +117,34 @@ static void sink_publish(log_t *log)
sink_free(old_log);
}
static int *src_levels(log_t *log, logfacility_t facility, logsrc_t src)
static int *src_levels(log_t *log, log_target_t target, log_source_t src)
{
assert(src < LOG_ANY);
return &log->facility[LOG_ANY * facility + src];
assert(src < LOG_SOURCE_ANY);
return &log->target[LOG_SOURCE_ANY * target + src];
}
static void sink_levels_set(log_t *log, logfacility_t facility, logsrc_t src, int levels)
static void sink_levels_set(log_t *log, log_target_t target, log_source_t src, int levels)
{
// Assign levels to the specified source.
if (src != LOG_ANY) {
*src_levels(log, facility, src) = levels;
if (src != LOG_SOURCE_ANY) {
*src_levels(log, target, src) = levels;
} else {
// ANY ~ set levels to all sources.
for (int i = 0; i < LOG_ANY; ++i) {
*src_levels(log, facility, i) = levels;
for (int i = 0; i < LOG_SOURCE_ANY; ++i) {
*src_levels(log, target, i) = levels;
}
}
}
static void sink_levels_add(log_t *log, logfacility_t facility, logsrc_t src, int levels)
static void sink_levels_add(log_t *log, log_target_t target, log_source_t src, int levels)
{
// Add levels to the specified source.
if (src != LOG_ANY) {
*src_levels(log, facility, src) |= levels;
if (src != LOG_SOURCE_ANY) {
*src_levels(log, target, src) |= levels;
} else {
// ANY ~ add levels to all sources.
for (int i = 0; i < LOG_ANY; ++i) {
*src_levels(log, facility, i) |= levels;
for (int i = 0; i < LOG_SOURCE_ANY; ++i) {
*src_levels(log, target, i) |= levels;
}
}
}
......@@ -167,9 +167,9 @@ void log_init(void)
use_journal = sd_booted();
#endif
sink_levels_set(log, LOGT_SYSLOG, LOG_ANY, emask);
sink_levels_set(log, LOGT_STDERR, LOG_ANY, emask);
sink_levels_set(log, LOGT_STDOUT, LOG_ANY, imask);
sink_levels_set(log, LOG_TARGET_SYSLOG, LOG_SOURCE_ANY, emask);
sink_levels_set(log, LOG_TARGET_STDERR, LOG_SOURCE_ANY, emask);
sink_levels_set(log, LOG_TARGET_STDOUT, LOG_SOURCE_ANY, imask);
sink_publish(log);
setlogmask(LOG_UPTO(LOG_DEBUG));
......@@ -186,34 +186,34 @@ void log_close(void)
closelog();
}
void log_flag_set(logflag_t flag)
void log_flag_set(log_flag_t flag)
{
if (log_isopen()) {
s_log->flags |= flag;
}
}
void log_levels_set(logfacility_t facility, logsrc_t src, int levels)
void log_levels_set(log_target_t target, log_source_t src, int levels)
{
if (log_isopen()) {
sink_levels_set(s_log, facility, src, levels);
sink_levels_set(s_log, target, src, levels);
}
}
void log_levels_add(logfacility_t facility, logsrc_t src, int levels)
void log_levels_add(log_target_t target, log_source_t src, int levels)
{
if (log_isopen()) {
sink_levels_add(s_log, facility, src, levels);
sink_levels_add(s_log, target, src, levels);
}
}
static void emit_log_msg(int level, const char *zone, size_t zone_len, const char *msg)
{
log_t *log = s_log;
logsrc_t src = zone ? LOG_ZONE : LOG_SERVER;
log_source_t src = zone ? LOG_SOURCE_ZONE : LOG_SOURCE_SERVER;
// Syslog facility.
if (*src_levels(log, LOGT_SYSLOG, src) & LOG_MASK(level)) {
// Syslog target.
if (*src_levels(log, LOG_TARGET_SYSLOG, src) & LOG_MASK(level)) {
#ifdef ENABLE_SYSTEMD
if (use_journal) {
char *zone_fmt = zone ? "ZONE=%.*s" : NULL;
......@@ -230,7 +230,7 @@ static void emit_log_msg(int level, const char *zone, size_t zone_len, const cha
// Prefix date and time.
char tstr[LOG_BUFLEN] = { 0 };
if (!(s_log->flags & LOG_FNO_TIMESTAMP)) {
if (!(s_log->flags & LOG_FLAG_NOTIMESTAMP)) {
struct tm lt;
struct timeval tv;
gettimeofday(&tv, NULL);
......@@ -240,14 +240,14 @@ static void emit_log_msg(int level, const char *zone, size_t zone_len, const cha
}
}
// Other log facilities.
for (int i = LOGT_STDERR; i < LOGT_FILE + log->file_count; ++i) {
// Other log targets.
for (int i = LOG_TARGET_STDERR; i < LOG_TARGET_FILE + log->file_count; ++i) {
if (*src_levels(log, i, src) & LOG_MASK(level)) {
FILE *stream;
switch (i) {
case LOGT_STDERR: stream = stderr; break;
case LOGT_STDOUT: stream = stdout; break;
default: stream = log->file[i - LOGT_FILE]; break;
case LOG_TARGET_STDERR: stream = stderr; break;
case LOG_TARGET_STDOUT: stream = stdout; break;
default: stream = log->file[i - LOG_TARGET_FILE]; break;
}
// Print the message.
......@@ -303,7 +303,7 @@ static void log_msg_text(int level, const char *zone, const char *fmt, va_list a
rcu_read_lock();
// Prefix error level.
if (level != LOG_INFO || !(s_log->flags & LOG_FNO_INFO)) {
if (level != LOG_INFO || !(s_log->flags & LOG_FLAG_NOINFO)) {
const char *prefix = level_prefix(level);
int ret = log_msg_add(&write, &capacity, "%s: ", prefix);
if (ret != KNOT_EOK) {
......@@ -327,7 +327,7 @@ static void log_msg_text(int level, const char *zone, const char *fmt, va_list a
// Compile log message.
int ret = vsnprintf(write, capacity, fmt, args);
if (ret >= 0) {
// Send to logging facilities.
// Send to logging targets.
emit_log_msg(level, zone, zone_len, buff);
}
......@@ -376,24 +376,24 @@ int log_update_privileges(int uid, int gid)
return KNOT_EOK;
}
static logfacility_t get_logtype(const char *logname)
static log_target_t get_logtype(const char *logname)
{
assert(logname);
if (strcasecmp(logname, "syslog") == 0) {
return LOGT_SYSLOG;
return LOG_TARGET_SYSLOG;
} else if (strcasecmp(logname, "stderr") == 0) {
return LOGT_STDERR;
return LOG_TARGET_STDERR;
} else if (strcasecmp(logname, "stdout") == 0) {
return LOGT_STDOUT;
return LOG_TARGET_STDOUT;
} else {
return LOGT_FILE;
return LOG_TARGET_FILE;
}
}
static int log_open_file(log_t *log, const char *filename)
{
assert(LOGT_FILE + log->file_count < log->facility_count);
assert(LOG_TARGET_FILE + log->file_count < log->target_count);
// Open the file.
log->file[log->file_count] = fopen(filename, "a");
......@@ -404,7 +404,7 @@ static int log_open_file(log_t *log, const char *filename)
// Disable buffering.
setvbuf(log->file[log->file_count], NULL, _IONBF, 0);
return LOGT_FILE + log->file_count++;
return LOG_TARGET_FILE + log->file_count++;
}
void log_reconfigure(conf_t *conf)
......@@ -416,12 +416,12 @@ void log_reconfigure(conf_t *conf)
return;
}
// Find maximum log facility id.
// Find maximum log target id.
unsigned files = 0;
for (conf_iter_t iter = conf_iter(conf, C_LOG); iter.code == KNOT_EOK;
conf_iter_next(conf, &iter)) {
conf_val_t id = conf_iter_id(conf, &iter);
if (get_logtype(conf_str(&id)) == LOGT_FILE) {
if (get_logtype(conf_str(&id)) == LOG_TARGET_FILE) {
++files;
}
}
......@@ -439,13 +439,13 @@ void log_reconfigure(conf_t *conf)
conf_val_t id = conf_iter_id(conf, &iter);
const char *logname = conf_str(&id);
// Get facility.
int facility = get_logtype(logname);
if (facility == LOGT_FILE) {
facility = log_open_file(log, logname);
if (facility < 0) {
// Get target.
int target = get_logtype(logname);
if (target == LOG_TARGET_FILE) {
target = log_open_file(log, logname);
if (target < 0) {
log_error("failed to open log, file '%s' (%s)",
logname, knot_strerror(facility));
logname, knot_strerror(target));
continue;
}
}
......@@ -456,17 +456,17 @@ void log_reconfigure(conf_t *conf)
// Set SERVER logging.
levels_val = conf_id_get(conf, C_LOG, C_SERVER, &id);
levels = conf_opt(&levels_val);
sink_levels_add(log, facility, LOG_SERVER, levels);
sink_levels_add(log, target, LOG_SOURCE_SERVER, levels);
// Set ZONE logging.
levels_val = conf_id_get(conf, C_LOG, C_ZONE, &id);
levels = conf_opt(&levels_val);
sink_levels_add(log, facility, LOG_ZONE, levels);
sink_levels_add(log, target, LOG_SOURCE_ZONE, levels);
// Set ANY logging.
levels_val = conf_id_get(conf, C_LOG, C_ANY, &id);
levels = conf_opt(&levels_val);
sink_levels_add(log, facility, LOG_ANY, levels);
sink_levels_add(log, target, LOG_SOURCE_ANY, levels);
}
sink_publish(log);
......
......@@ -40,26 +40,26 @@
/*! \brief Format for timestamps in log files. */
#define KNOT_LOG_TIME_FORMAT "%Y-%m-%dT%H:%M:%S"
/*! \brief Logging facility types. */
/*! \brief Logging targets. */
typedef enum {
LOGT_SYSLOG = 0, /*!< Logging to syslog(3) facility. */
LOGT_STDERR = 1, /*!< Print log messages to the stderr. */
LOGT_STDOUT = 2, /*!< Print log messages to the stdout. */
LOGT_FILE = 3 /*!< Generic logging to (unbuffered) file on the disk. */
} logfacility_t;
LOG_TARGET_SYSLOG = 0, /*!< System log. */
LOG_TARGET_STDERR = 1, /*!< Standard error stream. */
LOG_TARGET_STDOUT = 2, /*!< Standard output stream. */
LOG_TARGET_FILE = 3 /*!< Generic logging to a file (unbuffered). */
} log_target_t;
/*! \brief Logging sources. */
typedef enum {
LOG_SERVER = 0, /*!< Server module. */
LOG_ZONE = 1, /*!< Zone manipulation module. */
LOG_ANY = 2 /*!< Any module. */
} logsrc_t;
LOG_SOURCE_SERVER = 0, /*!< Server module. */
LOG_SOURCE_ZONE = 1, /*!< Zone manipulation module. */
LOG_SOURCE_ANY = 2 /*!< Any module. */
} log_source_t;
/*! \brief Logging format flags. */
typedef enum {
LOG_FNO_TIMESTAMP = 1 << 0, /*!< Don't print timestamp prefix. */
LOG_FNO_INFO = 1 << 1 /*!< Don't print info level prefix. */
} logflag_t;
LOG_FLAG_NOTIMESTAMP = 1 << 0, /*!< Don't print timestamp prefix. */
LOG_FLAG_NOINFO = 1 << 1 /*!< Don't print info level prefix. */
} log_flag_t;
/*!
* \brief Setup logging subsystem.
......@@ -74,28 +74,28 @@ void log_close(void);
/*!
* \brief Set logging format flag.
*/
void log_flag_set(logflag_t flag);
void log_flag_set(log_flag_t flag);
/*!
* \brief Set log levels for given facility.
* \brief Set log levels for given target.
*
* \param facility Logging facility index (LOGT_SYSLOG...).
* \param src Logging source (LOG_SERVER...LOG_ANY).
* \param levels Bitmask of specified log levels.
* \param target Logging target index (LOG_TARGET_SYSLOG...).
* \param src Logging source (LOG_SOURCE_SERVER...LOG_SOURCE_ANY).
* \param levels Bitmask of specified log levels.
*/
void log_levels_set(logfacility_t facility, logsrc_t src, int levels);
void log_levels_set(log_target_t target, log_source_t src, int levels);
/*!
* \brief Add log levels to a given facility.
* \brief Add log levels to a given target.
*
* New levels are added on top of existing, the resulting levels set is
* "old_levels OR new_levels".
*
* \param facility Logging facility index (LOGT_SYSLOG...).
* \param src Logging source (LOG_SERVER...LOG_ANY).
* \param levels Bitmask of specified log levels.
* \param target Logging target index (LOG_TARGET_SYSLOG...).
* \param src Logging source (LOG_SOURCE_SERVER...LOG_SOURCE_ANY).
* \param levels Bitmask of specified log levels.
*/
void log_levels_add(logfacility_t facility, logsrc_t src, int levels);
void log_levels_add(log_target_t target, log_source_t src, int levels);
/*!
* \brief Log message into server category.
......
......@@ -111,12 +111,13 @@ int main(int argc, char **argv)
/* Set up simplified logging just to stdout/stderr. */
log_init();
log_levels_set(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_INFO) | LOG_MASK(LOG_NOTICE));
log_levels_set(LOGT_STDERR, LOG_ANY, LOG_UPTO(LOG_WARNING));
log_levels_set(LOGT_SYSLOG, LOG_ANY, 0);
log_flag_set(LOG_FNO_TIMESTAMP | LOG_FNO_INFO);
log_levels_set(LOG_TARGET_STDOUT, LOG_SOURCE_ANY,
LOG_MASK(LOG_INFO) | LOG_MASK(LOG_NOTICE));
log_levels_set(LOG_TARGET_STDERR, LOG_SOURCE_ANY, LOG_UPTO(LOG_WARNING));
log_levels_set(LOG_TARGET_SYSLOG, LOG_SOURCE_ANY, 0);
log_flag_set(LOG_FLAG_NOTIMESTAMP | LOG_FLAG_NOINFO);
if (params.verbose) {
log_levels_add(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_DEBUG));
log_levels_add(LOG_TARGET_STDOUT, LOG_SOURCE_ANY, LOG_MASK(LOG_DEBUG));
}
int ret;
......
......@@ -451,7 +451,7 @@ int main(int argc, char **argv)
/* Initialize logging subsystem. */
log_init();
if (verbose) {
log_levels_add(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_DEBUG));
log_levels_add(LOG_TARGET_STDOUT, LOG_SOURCE_ANY, LOG_MASK(LOG_DEBUG));
}
/* Set up the configuration */
......
......@@ -99,12 +99,12 @@ int main(int argc, char *argv[])
/* TODO: Remove logging from zone loading. */
log_init();
log_levels_set(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_ERR));
log_levels_set(LOGT_STDERR, LOG_ANY, 0);
log_levels_set(LOGT_SYSLOG, LOG_ANY, 0);
log_flag_set(LOG_FNO_TIMESTAMP | LOG_FNO_INFO);
log_levels_set(LOG_TARGET_STDOUT, LOG_SOURCE_ANY, LOG_MASK(LOG_ERR));
log_levels_set(LOG_TARGET_STDERR, LOG_SOURCE_ANY, 0);
log_levels_set(LOG_TARGET_SYSLOG, LOG_SOURCE_ANY, 0);
log_flag_set(LOG_FLAG_NOTIMESTAMP | LOG_FLAG_NOINFO);
if (verbose) {
log_levels_add(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_DEBUG));
log_levels_add(LOG_TARGET_STDOUT, LOG_SOURCE_ANY, LOG_MASK(LOG_DEBUG));
}
knot_dname_t *dname = knot_dname_from_str_alloc(zonename);
......
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