Commit d3a0a8d5 authored by Jan Včelák's avatar Jan Včelák 🚀

log: always add level prefix, add zone name if present

- log_msg() logs into 'server' category
- log_msg_zone() and log_msg_zone_str() log into 'zone' category
parent f46ba602
......@@ -228,7 +228,7 @@ int log_levels_add(int facility, logsrc_t src, uint8_t levels)
return sink_levels_add(s_log, facility, src, levels);
}
static int _log_msg(logsrc_t src, int level, const char *msg)
static int emit_log_msg(logsrc_t src, int level, const char *msg)
{
rcu_read_lock();
struct log_sink *log = s_log;
......@@ -307,56 +307,121 @@ static int _log_msg(logsrc_t src, int level, const char *msg)
return ret;
}
int log_msg(logsrc_t src, int level, const char *msg, ...)
static const char *level_prefix(int level)
{
switch (level) {
case LOG_DEBUG: return "debug";
case LOG_INFO: return "info";
case LOG_NOTICE: return "notice";
case LOG_WARNING: return "warning";
case LOG_ERR: return "error";
case LOG_CRIT: return "critical";
default:
return NULL;
};
}
static int log_msg_add(char **write, size_t *capacity, const char *fmt, ...)
{
assert(*write);
assert(capacity);
assert(fmt);
va_list args;
va_start(args, fmt);
int written = vsnprintf(*write, *capacity, fmt, args);
va_end(args);
if (written < 0) {
return KNOT_ESPACE;
}
*write += written;
*capacity -= written;
return KNOT_EOK;
}
static int log_msg_text(int level, const char *zone, const char *fmt, va_list args)
{
int ret;
/* Buffer for log message. */
char sbuf[LOG_BUFLEN];
int buflen = sizeof(sbuf) - 1;
char *write = sbuf;
size_t capacity = sizeof(sbuf) - 1;
/* Prefix error level. */
const char *prefix = "";
switch (level) {
case LOG_DEBUG: prefix = "[debug] "; break;
case LOG_INFO: prefix = ""; break;
case LOG_NOTICE: prefix = "[notice] "; break;
case LOG_WARNING: prefix = "[warning] "; break;
case LOG_ERR: prefix = "[error] "; break;
case LOG_CRIT: prefix = "[fatal] "; break;
default: break;
const char *prefix = level_prefix(level);
ret = log_msg_add(&write, &capacity, "%s: ", prefix);
if (ret != KNOT_EOK) {
return ret;
}
/* Prepend prefix. */
size_t pr_size = strlcpy(sbuf, prefix, sizeof(sbuf));
if (pr_size >= sizeof(sbuf)) {
return KNOT_ESPACE;
/* Prefix zone name. */
if (zone) {
ret = log_msg_add(&write, &capacity, "[%s] ", zone);
if (ret != KNOT_EOK) {
return ret;
}
}
/* Compile log message. */
int ret = 0;
va_list ap;
va_start(ap, msg);
ret = vsnprintf(sbuf + pr_size, buflen - pr_size, msg, ap);
va_end(ap);
ret = vsnprintf(write, capacity, fmt, args);
/* Send to logging facilities. */
if (ret > 0) {
ret = _log_msg(src, level, sbuf);
if (ret >= 0) {
logsrc_t src = zone ? LOG_ZONE : LOG_SERVER;
ret = emit_log_msg(src, level, sbuf);
}
return ret;
}
int log_vmsg(logsrc_t src, int level, const char *msg, va_list ap)
int log_msg(int priority, const char *fmt, ...)
{
int ret = 0;
char buf[LOG_BUFLEN];
ret = vsnprintf(buf, sizeof(buf) - 1, msg, ap);
if (!fmt) {
return KNOT_EINVAL;
}
va_list args;
va_start(args, fmt);
int result = log_msg_text(priority, NULL, fmt, args);
va_end(args);
return result;
}
if (ret > 0) {
ret = _log_msg(src, level, buf);
int log_msg_zone(int priority, const knot_dname_t *zone, const char *fmt, ...)
{
if (!zone || !fmt) {
return KNOT_EINVAL;
}
return ret;
char *zone_str = knot_dname_to_str(zone);
va_list args;
va_start(args, fmt);
int result = log_msg_text(priority, zone_str, fmt, args);
va_end(args);
free(zone_str);
return result;
}
int log_msg_zone_str(int priority, const char *zone, const char *fmt, ...)
{
if (!zone|| !fmt) {
return KNOT_EINVAL;
}
va_list args;
va_start(args, fmt);
int result = log_msg_text(priority, zone, fmt, args);
va_end(args);
return result;
}
void hex_log(int source, const char *data, int length)
......
......@@ -38,6 +38,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include "libknot/dname.h"
struct conf_t;
/*! \brief Log facility types. */
......@@ -120,45 +122,60 @@ int log_levels_set(int facility, logsrc_t src, uint8_t levels);
int log_levels_add(int facility, logsrc_t src, uint8_t levels);
/*!
* \brief Log message.
* \brief Log message into server category.
*
* Function follows printf() format.
*
* \param src Origin of the message.
* \param level Message error level.
* \param msg Content of the logged message.
* \param priority Message error level.
* \param fmt Content of the logged message.
*
* \return Number of logged bytes, negative error.
*/
int log_msg(int priority, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
/*!
* \brief Log message into zone category.
*
* \retval Number of logged bytes on success.
* \retval 0 When the message is ignored.
* \retval KNOT_EINVAL invalid parameters.
* \retval KNOT_ERROR unspecified error.
* \see log_msg
* \param zone Zone name in wire format.
*/
int log_msg(logsrc_t src, int level, const char *msg, ...)
int log_msg_zone(int priority, const knot_dname_t *zone, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
/*!
* \brief Log message for stdarg.
* \brief Log message into zone category.
*
* \see log_msg
* \param zone Zone name as an ASCII string.
*/
int log_vmsg(logsrc_t src, int level, const char *msg, va_list ap);
int log_msg_zone_str(int priority, const char *zone, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
void hex_log(int source, const char *data, int length);
/* Convenient logging. */
#define log_server_fatal(msg...) log_msg(LOG_SERVER, LOG_CRIT, msg)
#define log_server_error(msg...) log_msg(LOG_SERVER, LOG_ERR, msg)
#define log_server_warning(msg...) log_msg(LOG_SERVER, LOG_WARNING, msg)
#define log_server_notice(msg...) log_msg(LOG_SERVER, LOG_NOTICE, msg)
#define log_server_info(msg...) log_msg(LOG_SERVER, LOG_INFO, msg)
#define log_server_debug(msg...) log_msg(LOG_SERVER, LOG_DEBUG, msg)
#define log_zone_fatal(msg...) log_msg(LOG_ZONE, LOG_CRIT, msg)
#define log_zone_error(msg...) log_msg(LOG_ZONE, LOG_ERR, msg)
#define log_zone_warning(msg...) log_msg(LOG_ZONE, LOG_WARNING, msg)
#define log_zone_notice(msg...) log_msg(LOG_ZONE, LOG_NOTICE, msg)
#define log_zone_info(msg...) log_msg(LOG_ZONE, LOG_INFO, msg)
#define log_zone_debug(msg...) log_msg(LOG_ZONE, LOG_DEBUG, msg)
#define log_fatal(msg...) log_msg(LOG_CRIT, msg)
#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)
#define log_zone_fatal(zone, msg...) log_msg_zone(LOG_CRIT, zone, msg)
#define log_zone_error(zone, msg...) log_msg_zone(LOG_ERR, zone, msg)
#define log_zone_warning(zone, msg...) log_msg_zone(LOG_WARNING, zone, msg)
#define log_zone_notice(zone, msg...) log_msg_zone(LOG_NOTICE, zone, msg)
#define log_zone_info(zone, msg...) log_msg_zone(LOG_INFO, zone, msg)
#define log_zone_debug(zone, msg...) log_msg_zone(LOG_DEBUG, zone, msg)
#define log_zone_str_fatal(zone, msg...) log_msg_zone_str(LOG_CRIT, zone, msg)
#define log_zone_str_error(zone, msg...) log_msg_zone_str(LOG_ERR, zone, msg)
#define log_zone_str_warning(zone, msg...) log_msg_zone_str(LOG_WARNING, zone, msg)
#define log_zone_str_notice(zone, msg...) log_msg_zone_str(LOG_NOTICE, zone, msg)
#define log_zone_str_info(zone, msg...) log_msg_zone_str(LOG_INFO, zone, msg)
#define log_zone_str_debug(zone, msg...) log_msg_zone_str(LOG_DEBUG, zone, msg)
/*!
* \brief Update open files ownership.
......
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