Skip to content

Commit

Permalink
syslog: Add a message-id parameter for messages (#433)
Browse files Browse the repository at this point in the history
The message-id parameter will enable systemd catalogs.
To enable message-id's the libqb should be configured with the
 --enable-systemd-journal option.

Co-authored-by: root <Aleksei Burlakov>
  • Loading branch information
aleksei-burlakov authored Mar 1, 2021
1 parent c7e1afe commit aae7a0a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
64 changes: 62 additions & 2 deletions include/qb/qblog.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ typedef const char *(*qb_log_tags_stringify_fn)(uint32_t tags);

/**
* An instance of this structure is created for each log message
* with the message-id
*/
struct qb_log_callsite {
const char *function;
Expand All @@ -258,6 +259,7 @@ struct qb_log_callsite {
uint32_t lineno;
uint32_t targets;
uint32_t tags;
const char *message_id;
} __attribute__((aligned(8)));

typedef void (*qb_log_filter_fn)(struct qb_log_callsite * cs);
Expand Down Expand Up @@ -319,6 +321,39 @@ struct qb_log_callsite* qb_log_callsite_get(const char *function,
uint32_t lineno,
uint32_t tags);

/**
* Get or create a callsite at the given position.
* The same that qb_log_callsite_get but with the
* message_id parameter.
*
* The result can then be passed into qb_log_real_()
*
* @param message_id in the systemd catalog or NULL
* @param function originating function name
* @param filename originating filename
* @param format format string
* @param priority this takes syslog priorities.
* @param lineno file line number
* @param tags the tag
*/
struct qb_log_callsite* qb_log_callsite_get2(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority,
uint32_t lineno,
uint32_t tags);

void qb_log_from_external_source_va2(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority,
uint32_t lineno,
uint32_t tags,
va_list ap)
__attribute__ ((format (printf, 4, 0)));

void qb_log_from_external_source_va(const char *function,
const char *filename,
const char *format,
Expand All @@ -332,6 +367,7 @@ void qb_log_from_external_source_va(const char *function,
* This is the function to generate a log message if you want to
* manually add tags.
*
* @param message_id in the systemd catalog or NULL
* @param priority this takes syslog priorities.
* @param tags this is a uint32_t that you can use with
* qb_log_tags_stringify_fn_set() to "tag" a log message
Expand All @@ -340,13 +376,37 @@ void qb_log_from_external_source_va(const char *function,
* @param fmt usual printf style format specifiers
* @param args usual printf style args
*/
#define qb_logt(priority, tags, fmt, args...) do { \
#define qb_logt2(message_id, priority, tags, fmt, args...) do { \
struct qb_log_callsite* descriptor_pt = \
qb_log_callsite_get(__func__, __FILE__, fmt, \
qb_log_callsite_get2(message_id, __func__, __FILE__, fmt, \
priority, __LINE__, tags); \
qb_log_real_(descriptor_pt, ##args); \
} while(0)

/**
* This is the function to generate a log message if you want to
* manually add tags.
*
* @param priority this takes syslog priorities.
* @param tags this is a uint32_t that you can use with
* qb_log_tags_stringify_fn_set() to "tag" a log message
* with a feature or sub-system then you can use "%g"
* in the format specifer to print it out.
* @param fmt usual printf style format specifiers
* @param args usual printf style args
*/
#define qb_logt(priority, tags, fmt, args...) qb_logt2(NULL, priority, tags, fmt, ##args)


/**
* This is the main function to generate a log message.
*
* @param message_id in the systemd catalog or NULL
* @param priority this takes syslog priorities.
* @param fmt usual printf style format specifiers
* @param args usual printf style args
*/
#define qb_log2(message_id, priority, fmt, args...) qb_logt2(message_id, priority, 0, fmt, ##args)

/**
* This is the main function to generate a log message.
Expand Down
36 changes: 31 additions & 5 deletions lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ qb_log_thread_log_write(struct qb_log_callsite *cs,
}

struct qb_log_callsite*
qb_log_callsite_get(const char *function,
qb_log_callsite_get2(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority,
Expand All @@ -335,8 +336,9 @@ qb_log_callsite_get(const char *function,
return NULL;
}

cs = qb_log_dcs_get(&new_dcs, function, filename,
format, priority, lineno, tags);
cs = qb_log_dcs_get(&new_dcs, message_id, function, filename,
format, priority, lineno, tags);

if (cs == NULL) {
return NULL;
}
Expand Down Expand Up @@ -380,8 +382,21 @@ qb_log_callsite_get(const char *function,
return cs;
}

struct qb_log_callsite*
qb_log_callsite_get(const char *function,
const char *filename,
const char *format,
uint8_t priority,
uint32_t lineno,
uint32_t tags)
{
return qb_log_callsite_get2(NULL, function, filename, format,
priority, lineno, tags);
}

void
qb_log_from_external_source_va(const char *function,
qb_log_from_external_source_va2(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority,
Expand All @@ -393,11 +408,22 @@ qb_log_from_external_source_va(const char *function,
return;
}

cs = qb_log_callsite_get(function, filename,
cs = qb_log_callsite_get2(message_id, function, filename,
format, priority, lineno, tags);
qb_log_real_va_(cs, ap);
}

void
qb_log_from_external_source_va(const char *function,
const char *filename,
const char *format,
uint8_t priority,
uint32_t lineno, uint32_t tags, va_list ap)
{
qb_log_from_external_source_va2(NULL, function, filename,
format, priority, lineno, tags, ap);
}

void
qb_log_from_external_source(const char *function,
const char *filename,
Expand Down
12 changes: 9 additions & 3 deletions lib/log_dcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ _log_register_callsites(qb_array_t * a, uint32_t bin)
}

static struct qb_log_callsite *
_log_dcs_new_cs(const char *function,
_log_dcs_new_cs(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority, uint32_t lineno, uint32_t tags)
Expand All @@ -70,6 +71,7 @@ _log_dcs_new_cs(const char *function,
assert(rc == 0);
assert(cs != NULL);

cs->message_id = message_id ? strdup(message_id) : NULL;
cs->function = strdup(function);
cs->filename = strdup(filename);
cs->format = strdup(format);
Expand All @@ -82,6 +84,7 @@ _log_dcs_new_cs(const char *function,

struct qb_log_callsite *
qb_log_dcs_get(int32_t * newly_created,
const char *message_id,
const char *function,
const char *filename,
const char *format,
Expand Down Expand Up @@ -120,6 +123,7 @@ qb_log_dcs_get(int32_t * newly_created,
(void)qb_thread_lock(arr_next_lock);
if (csl_head->cs &&
priority == csl_head->cs->priority &&
(message_id ? (strcmp(message_id, csl_head->cs->message_id) == 0) : 1) &&
strcmp(safe_filename, csl_head->cs->filename) == 0 &&
strcmp(safe_format, csl_head->cs->format) == 0) {
(void)qb_thread_unlock(arr_next_lock);
Expand All @@ -130,7 +134,8 @@ qb_log_dcs_get(int32_t * newly_created,
* so we will either have to create it or go through a list
*/
if (csl_head->cs == NULL) {
csl_head->cs = _log_dcs_new_cs(safe_function, safe_filename, safe_format,
csl_head->cs = _log_dcs_new_cs(message_id, safe_function,
safe_filename, safe_format,
priority, lineno, tags);
cs = csl_head->cs;
csl_head->next = NULL;
Expand All @@ -152,7 +157,8 @@ qb_log_dcs_get(int32_t * newly_created,
if (csl == NULL) {
goto cleanup;
}
csl->cs = _log_dcs_new_cs(safe_function, safe_filename, safe_format,
csl->cs = _log_dcs_new_cs(message_id, safe_function,
safe_filename, safe_format,
priority, lineno, tags);
csl->next = NULL;
csl_last->next = csl;
Expand Down
1 change: 1 addition & 0 deletions lib/log_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void qb_log_thread_resume(struct qb_log_target *t);
void qb_log_dcs_init(void);
void qb_log_dcs_fini(void);
struct qb_log_callsite *qb_log_dcs_get(int32_t *newly_created,
const char *message_id,
const char *function,
const char *filename,
const char *format,
Expand Down
13 changes: 12 additions & 1 deletion lib/log_syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,24 @@ _syslog_logger(int32_t target,
}
#ifdef USE_JOURNAL
if (t->use_journal) {
sd_journal_send("PRIORITY=%d", final_priority,
if (cs->message_id) {
sd_journal_send("MESSAGE_ID=%s", cs->message_id,
"PRIORITY=%d", final_priority,
"CODE_LINE=%d", cs->lineno,
"CODE_FILE=%s", cs->filename,
"CODE_FUNC=%s", cs->function,
"SYSLOG_IDENTIFIER=%s", t->name,
"MESSAGE=%s", output_buffer,
NULL);
} else {
sd_journal_send("PRIORITY=%d", final_priority,
"CODE_LINE=%d", cs->lineno,
"CODE_FILE=%s", cs->filename,
"CODE_FUNC=%s", cs->function,
"SYSLOG_IDENTIFIER=%s", t->name,
"MESSAGE=%s", output_buffer,
NULL);
}
} else {
#endif
syslog(final_priority, "%s", output_buffer);
Expand Down
6 changes: 5 additions & 1 deletion tests/check_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,13 @@ START_TEST(test_journal)
pid_t log_pid;
sd_journal *jnl;
int count = 0;
const char *msgid="f77379a8490b408bbe5f6940505a777b";

qb_log_init("check_log", LOG_USER, LOG_DEBUG);
qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
rc = qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_USE_JOURNAL, 1);
ck_assert_int_eq(rc, 0);
qb_log(LOG_ERR, "Test message 1 from libqb");
qb_log2(msgid, LOG_ERR, "Test message 1 from libqb");

qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
rc = qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_USE_JOURNAL, 1);
Expand All @@ -1046,6 +1047,9 @@ START_TEST(test_journal)
if (log_pid == getpid()) {
rc = sd_journal_get_data(jnl, "MESSAGE", (const void **)&msg, &len);
ck_assert_int_eq(rc, 0);
rc = sd_journal_get_data(jnl, "MESSAGE_ID", (const void **)&msg, &len);
ck_assert_int_eq(rc, 0);
ck_assert_str_eq(msg+11, msgid);
break;
}
if (++count > 20) {
Expand Down

0 comments on commit aae7a0a

Please sign in to comment.