This will log the name passed into qb_log_init()
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
examples/simplelog.c | 2 +-
include/qb/qblog.h | 1 +
lib/log.c | 27 +++++++++++++++++++++------
lib/log_blackbox.c | 5 +++--
lib/log_file.c | 6 +++---
lib/log_int.h | 1 +
6 files changed, 30 insertions(+), 12 deletions(-)
diff --git a/examples/simplelog.c b/examples/simplelog.c
index ef57503..0752807 100644
--- a/examples/simplelog.c
+++ b/examples/simplelog.c
@@ -190,7 +190,7 @@ int32_t main(int32_t argc, char *argv[])
log_fd = qb_log_file_open(logfile);
qb_log_filter_ctl(log_fd, QB_LOG_FILTER_ADD,
QB_LOG_FILTER_FILE, __FILE__, priority);
- qb_log_format_set(log_fd, "%t %n() [%p] %b");
+ qb_log_format_set(log_fd, "[%N] %t %n() [%p] %b");
qb_log_ctl(log_fd, QB_LOG_CONF_THREADED, do_threaded);
qb_log_ctl(log_fd, QB_LOG_CONF_ENABLED, QB_TRUE);
}
diff --git a/include/qb/qblog.h b/include/qb/qblog.h
index 1f5f00c..7961209 100644
--- a/include/qb/qblog.h
+++ b/include/qb/qblog.h
@@ -477,6 +477,7 @@ void qb_log_tags_stringify_fn_set(qb_log_tags_stringify_fn fn);
* %t TIMESTAMP
* %b BUFFER
* %g TAGS
+ * %N name (passed into qb_log_init)
*
* any number between % and character specify field length to pad or chop
*/
diff --git a/lib/log.c b/lib/log.c
index 1002953..9dfb99e 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -670,7 +670,7 @@ qb_log_init(const char *name, int32_t facility, uint8_t priority)
conf[i].pos = i;
conf[i].debug = QB_FALSE;
conf[i].state = QB_LOG_STATE_UNUSED;
- conf[i].name[0] = '\0';
+ strncpy(conf[i].name, name, PATH_MAX);
conf[i].facility = facility;
qb_list_init(&conf[i].filter_head);
qb_log_format_set(i, NULL);
@@ -684,8 +684,6 @@ qb_log_init(const char *name, int32_t facility, uint8_t priority)
conf[QB_LOG_STDERR].state = QB_LOG_STATE_DISABLED;
conf[QB_LOG_BLACKBOX].state = QB_LOG_STATE_DISABLED;
- strncpy(conf[QB_LOG_SYSLOG].name, name, PATH_MAX);
- snprintf(conf[QB_LOG_BLACKBOX].name, PATH_MAX, "%s-blackbox", name);
logger_inited = QB_TRUE;
(void)qb_log_syslog_open(&conf[QB_LOG_SYSLOG]);
@@ -757,7 +755,7 @@ qb_log_target_free(struct qb_log_target *t)
(void)qb_log_filter_ctl(t->pos, QB_LOG_FILTER_CLEAR_ALL,
QB_LOG_FILTER_FILE, NULL, 0);
t->debug = QB_FALSE;
- t->name[0] = '\0';
+ t->filename[0] = '\0';
qb_log_format_set(t->pos, NULL);
_log_target_state_set(t, QB_LOG_STATE_UNUSED);
}
@@ -808,7 +806,7 @@ qb_log_custom_open(qb_log_logger_fn log_fn,
}
t->instance = user_data;
- snprintf(t->name, PATH_MAX, "custom-%d", t->pos);
+ snprintf(t->filename, PATH_MAX, "custom-%d", t->pos);
t->logger = log_fn;
t->vlogger = NULL;
@@ -934,7 +932,24 @@ qb_log_ctl(int32_t t, enum qb_log_conf c, int32_t arg)
void
qb_log_format_set(int32_t t, const char *format)
{
+ char* ptr = NULL;
+ size_t len = 0;
+
+ if (format) {
+ ptr = strstr(format, "%N");
+ len = strlen(format);
+ }
free(conf[t].format);
- conf[t].format = strdup(format ? format : "[%p] %b");
+
+ if (ptr) {
+ len += strlen(conf[t].name);
+ conf[t].format = calloc(len + 1, sizeof(char));
+ strncpy(conf[t].format, format, ptr - format);
+ strcat(conf[t].format, conf[t].name);
+ ptr += 2;
+ strcat(conf[t].format, ptr);
+ } else {
+ conf[t].format = strdup(format ? format : "[%p] %b");
+ }
assert(conf[t].format != NULL);
}
diff --git a/lib/log_blackbox.c b/lib/log_blackbox.c
index c9d091b..1c07c2e 100644
--- a/lib/log_blackbox.c
+++ b/lib/log_blackbox.c
@@ -32,7 +32,7 @@ _blackbox_reload(int32_t target)
return;
}
qb_rb_close(t->instance);
- t->instance = qb_rb_open(t->name, t->size,
+ t->instance = qb_rb_open(t->filename, t->size,
QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0);
}
@@ -100,8 +100,9 @@ qb_log_blackbox_open(struct qb_log_target *t)
if (t->size < 1024) {
return -EINVAL;
}
+ snprintf(t->filename, PATH_MAX, "%s-blackbox", t->name);
- t->instance = qb_rb_open(t->name, t->size,
+ t->instance = qb_rb_open(t->filename, t->size,
QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0);
if (t->instance == NULL) {
return -errno;
diff --git a/lib/log_file.c b/lib/log_file.c
index b8c2038..c374927 100644
--- a/lib/log_file.c
+++ b/lib/log_file.c
@@ -57,7 +57,7 @@ _file_reload(int32_t target)
if (t->instance) {
fclose(t->instance);
}
- t->instance = fopen(t->name, "a+");
+ t->instance = fopen(t->filename, "a+");
}
int32_t
@@ -66,7 +66,7 @@ qb_log_stderr_open(struct qb_log_target *t)
t->logger = _file_logger;
t->reload = NULL;
t->close = NULL;
- strncpy(t->name, "stderr", PATH_MAX);
+ strncpy(t->filename, "stderr", PATH_MAX);
t->instance = stderr;
return 0;
}
@@ -90,7 +90,7 @@ qb_log_file_open(const char *filename)
return rc;
}
t->instance = fp;
- strncpy(t->name, filename, PATH_MAX);
+ strncpy(t->filename, filename, PATH_MAX);
t->logger = _file_logger;
t->reload = _file_reload;
diff --git a/lib/log_int.h b/lib/log_int.h
index 1e311db..a1755a9 100644
--- a/lib/log_int.h
+++ b/lib/log_int.h
@@ -31,6 +31,7 @@ struct qb_log_target {
uint32_t pos;
enum qb_log_target_state state;
char name[PATH_MAX];
+ char filename[PATH_MAX];
struct qb_list_head filter_head;
int32_t facility;
int32_t priority_bump;
--
1.7.7.5