Skip to content

Commit

Permalink
Add a message-id parameter for messages
Browse files Browse the repository at this point in the history
DON'T MERGE

The message-ids are used by systemd catalogs. (see ClusterLabs/sbd#117)

To enable this feature the libqb should be configured with the
 --enable-systemd-journal option.

IMPORTANT:

The libqb exposes the

struct qb_log_callsite
qb_log_callsite_get(...
qb_log_from_external_source_va(..

To make it completelly backward compatible, they all three should come with
and without the message_id parameted. This would however requre repeating a
good half of the libqb. We did only a halfmeasure: we provided the

qb_log_from_external_source_va2

that uses the message id, to make it working with corosync. It's not however
backward compatible with pacemaker.
  • Loading branch information
root authored and root committed Jan 19, 2021
1 parent 5097155 commit c6997df
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
18 changes: 16 additions & 2 deletions include/qb/qblog.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,10 @@ 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 *message_id;
const char *function;
const char *filename;
const char *format;
Expand Down Expand Up @@ -305,20 +307,32 @@ void qb_log_from_external_source(const char *function,
*
* The result can then be passed into qb_log_real_()
*
* @param message_id is the id of the message 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_get(const char *function,
struct qb_log_callsite* qb_log_callsite_get(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, 3, 0)));

void qb_log_from_external_source_va(const char *function,
const char *filename,
const char *format,
Expand All @@ -342,7 +356,7 @@ void qb_log_from_external_source_va(const char *function,
*/
#define qb_logt(priority, tags, fmt, args...) do { \
struct qb_log_callsite* descriptor_pt = \
qb_log_callsite_get(__func__, __FILE__, fmt, \
qb_log_callsite_get(NULL, __func__, __FILE__, fmt, \
priority, __LINE__, tags); \
qb_log_real_(descriptor_pt, ##args); \
} while(0)
Expand Down
23 changes: 18 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_get(const char *message_id,
const char *function,
const char *filename,
const char *format,
uint8_t priority,
Expand All @@ -335,7 +336,7 @@ qb_log_callsite_get(const char *function,
return NULL;
}

cs = qb_log_dcs_get(&new_dcs, function, filename,
cs = qb_log_dcs_get(&new_dcs, message_id, function, filename,
format, priority, lineno, tags);
if (cs == NULL) {
return NULL;
Expand Down Expand Up @@ -381,7 +382,8 @@ qb_log_callsite_get(const char *function,
}

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 +395,22 @@ qb_log_from_external_source_va(const char *function,
return;
}

cs = qb_log_callsite_get(function, filename,
cs = qb_log_callsite_get(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 All @@ -412,7 +425,7 @@ qb_log_from_external_source(const char *function,
return;
}

cs = qb_log_callsite_get(function, filename,
cs = qb_log_callsite_get(NULL, function, filename,
format, priority, lineno, tags);
va_start(ap, tags);
qb_log_real_va_(cs, ap);
Expand Down
24 changes: 18 additions & 6 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 = strdup(message_id);
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 All @@ -92,10 +95,14 @@ qb_log_dcs_get(int32_t * newly_created,
struct callsite_list *csl_head;
struct callsite_list *csl_last = NULL;
struct callsite_list *csl;
const char *safe_message_id = message_id;
const char *safe_filename = filename;
const char *safe_function = function;
const char *safe_format = format;

if (message_id == NULL) {
safe_message_id = "";
}
if (filename == NULL) {
safe_filename = "";
}
Expand All @@ -120,6 +127,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 &&
strcmp(safe_message_id, csl_head->cs->message_id) == 0 &&
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 +138,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(safe_message_id, safe_function,
safe_filename, safe_format,
priority, lineno, tags);
cs = csl_head->cs;
csl_head->next = NULL;
Expand All @@ -140,9 +149,11 @@ qb_log_dcs_get(int32_t * newly_created,
assert(csl->cs->lineno == lineno);
if (priority == csl->cs->priority &&
strcmp(safe_format, csl->cs->format) == 0 &&
strcmp(safe_filename, csl->cs->filename) == 0) {
cs = csl->cs;
break;
strcmp(safe_filename, csl->cs->filename) == 0 &&
strcmp(safe_message_id, csl->cs->message_id) == 0)
{
cs = csl->cs;
break;
}
csl_last = csl;
}
Expand All @@ -152,7 +163,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(safe_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
3 changes: 2 additions & 1 deletion lib/log_syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ _syslog_logger(int32_t target,
}
#ifdef USE_JOURNAL
if (t->use_journal) {
sd_journal_send("PRIORITY=%d", final_priority,
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,
Expand Down

0 comments on commit c6997df

Please sign in to comment.