summaryrefslogtreecommitdiff
path: root/src/journal/sd-journal.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-11-15 23:03:31 +0100
committerLennart Poettering <lennart@poettering.net>2012-11-15 23:09:07 +0100
commitd4205751d4643c272059a3728045929dd0e5e800 (patch)
treec9931806e99d42789fc5ce3fc03e17285b9bb208 /src/journal/sd-journal.c
parent59f432ea6d6d441d0af7c76c37e80730c8df473a (diff)
journal: implement message catalog
The message catalog can be used to attach short help texts to log lines, keyed by their MESSAGE_ID= fields. This is useful to help the administrator understand the context and cause of a message, find possible solutions and find further related documentation. Since this is keyed off MESSAGE_ID= this will only work for native journal messages. The message catalog supports i18n, and is useful to augment english language system messages with explanations in the local language. This commit only includes short explanatory messages for a few example message IDs, we'll add more complete documentation for the relevant systemd messages later on.
Diffstat (limited to 'src/journal/sd-journal.c')
-rw-r--r--src/journal/sd-journal.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index a346691e21..c86f1eaab1 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -38,11 +38,15 @@
#include "compress.h"
#include "journal-internal.h"
#include "missing.h"
+#include "catalog.h"
+#include "replace-var.h"
#define JOURNAL_FILES_MAX 1024
#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
+#define REPLACE_VAR_MAX 256
+
static void detach_location(sd_journal *j) {
Iterator i;
JournalFile *f;
@@ -2389,3 +2393,59 @@ _public_ int sd_journal_reliable_fd(sd_journal *j) {
return !j->on_network;
}
+
+static char *lookup_field(const char *field, void *userdata) {
+ sd_journal *j = userdata;
+ const void *data;
+ size_t size, d;
+ int r;
+
+ assert(field);
+ assert(j);
+
+ r = sd_journal_get_data(j, field, &data, &size);
+ if (r < 0 ||
+ size > REPLACE_VAR_MAX)
+ return strdup(field);
+
+ d = strlen(field) + 1;
+
+ return strndup((const char*) data + d, size - d);
+}
+
+_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
+ const void *data;
+ size_t size;
+ sd_id128_t id;
+ _cleanup_free_ char *text = NULL, *cid = NULL;
+ char *t;
+ int r;
+
+ if (!j)
+ return -EINVAL;
+ if (!ret)
+ return -EINVAL;
+
+ r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
+ if (r < 0)
+ return r;
+
+ cid = strndup((const char*) data + 11, size - 11);
+ if (!cid)
+ return -ENOMEM;
+
+ r = sd_id128_from_string(cid, &id);
+ if (r < 0)
+ return r;
+
+ r = catalog_get(id, &text);
+ if (r < 0)
+ return r;
+
+ t = replace_var(text, lookup_field, j);
+ if (!t)
+ return -ENOMEM;
+
+ *ret = t;
+ return 0;
+}