summaryrefslogtreecommitdiff
path: root/src/system/dlt-system-journal.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/system/dlt-system-journal.c')
-rw-r--r--src/system/dlt-system-journal.c289
1 files changed, 140 insertions, 149 deletions
diff --git a/src/system/dlt-system-journal.c b/src/system/dlt-system-journal.c
index aa9ba52..c56e58a 100644
--- a/src/system/dlt-system-journal.c
+++ b/src/system/dlt-system-journal.c
@@ -43,7 +43,7 @@
*******************************************************************************/
#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
-#include <pthread.h>
+#include <poll.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -56,7 +56,6 @@
#include <systemd/sd-id128.h>
#include <inttypes.h> /* for PRI formatting macro */
-extern DltSystemThreads threads;
#define DLT_SYSTEM_JOURNAL_BUFFER_SIZE 256
#define DLT_SYSTEM_JOURNAL_BUFFER_SIZE_BIG 2048
@@ -199,13 +198,10 @@ void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *tim
time_usecs % 1000000);
}
-void journal_thread(void *v_conf)
-{
- int r;
- sd_journal *j;
- char match[DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH] = "_BOOT_ID=";
- sd_id128_t boot_id;
+void get_journal_msg(sd_journal *j, DltSystemConfiguration *config)
+{
uint32_t ts;
+ int r;
char buffer_process[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
buffer_priority[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
@@ -220,183 +216,178 @@ void journal_thread(void *v_conf)
char *systemd_log_levels[] =
{ "Emergency", "Alert", "Critical", "Error", "Warning", "Notice", "Informational", "Debug" };
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-journal, in thread."));
+ for(;;)
+ {
+ r = sd_journal_next(j);
+ if (r < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR,
+ DLT_STRING("dlt-system-journal failed to get next entry:"), DLT_STRING(strerror(-r)));
+ sd_journal_close(j);
+ return;
+ }
+ else if (r == 0) {
+ return;
+ }
- DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
- DLT_REGISTER_CONTEXT(journalContext, conf->Journal.ContextId, "Journal Adapter");
+ /* get all data from current journal entry */
+ dlt_system_journal_get_timestamp(j, &timestamp);
+
+ /* get data from journal entry, empty string if invalid fields */
+ dlt_system_journal_get(j, buffer_comm, "_COMM", sizeof(buffer_comm));
+ dlt_system_journal_get(j, buffer_pid, "_PID", sizeof(buffer_pid));
+ dlt_system_journal_get(j, buffer_priority, "PRIORITY", sizeof(buffer_priority));
+ dlt_system_journal_get(j, buffer_message, "MESSAGE", sizeof(buffer_message));
+ dlt_system_journal_get(j, buffer_transport, "_TRANSPORT", sizeof(buffer_transport));
+
+ /* prepare process string */
+ if (strcmp(buffer_transport, "kernel") == 0)
+ snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "kernel:");
+ else
+ snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s[%s]:", buffer_comm, buffer_pid);
+
+ /* map log level on demand */
+ loglevel = DLT_LOG_INFO;
+ systemd_loglevel = atoi(buffer_priority);
+
+ if (config->Journal.MapLogLevels) {
+ /* Map log levels from journal to DLT */
+ switch (systemd_loglevel) {
+ case 0: /* Emergency */
+ case 1: /* Alert */
+ case 2: /* Critical */
+ loglevel = DLT_LOG_FATAL;
+ break;
+ case 3: /* Error */
+ loglevel = DLT_LOG_ERROR;
+ break;
+ case 4: /* Warning */
+ loglevel = DLT_LOG_WARN;
+ break;
+ case 5: /* Notice */
+ case 6: /* Informational */
+ loglevel = DLT_LOG_INFO;
+ break;
+ case 7: /* Debug */
+ loglevel = DLT_LOG_DEBUG;
+ break;
+ default:
+ loglevel = DLT_LOG_INFO;
+ break;
+ }
+ }
- r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY /*SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY*/);
- printf("journal open return %d\n", r);
+ if ((systemd_loglevel >= 0) && (systemd_loglevel <= 7))
+ snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s:", systemd_log_levels[systemd_loglevel]);
+ else
+ snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "prio_unknown:");
+
+ /* write log entry */
+ if (config->Journal.UseOriginalTimestamp == 0) {
+ DLT_LOG(journalContext, loglevel,
+ DLT_STRING(timestamp.real),
+ DLT_STRING(timestamp.monotonic),
+ DLT_STRING(buffer_process),
+ DLT_STRING(buffer_priority),
+ DLT_STRING(buffer_message)
+ );
+ }
+ else {
+ /* since we are talking about points in time, I'd prefer truncating over arithmetic rounding */
+ ts = (uint32_t)(atof(timestamp.monotonic) * 10000);
+ DLT_LOG_TS(journalContext, loglevel, ts,
+ DLT_STRING(timestamp.real),
+ DLT_STRING(buffer_process),
+ DLT_STRING(buffer_priority),
+ DLT_STRING(buffer_message)
+ );
+ }
+
+ if (journal_checkUserBufferForFreeSpace() == -1) {
+ /* buffer is nearly full */
+ /* wait 500ms for writing next entry */
+ struct timespec t;
+ t.tv_sec = 0;
+ t.tv_nsec = 1000000ul * 500;
+ nanosleep(&t, NULL);
+ }
+ }
+}
+
+void register_journal_fd(sd_journal **j, struct pollfd *pollfd, int i, DltSystemConfiguration *config)
+{
+ DLT_REGISTER_CONTEXT(journalContext, config->Journal.ContextId, "Journal Adapter");
+ sd_journal *j_tmp;
+ char match[DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH] = "_BOOT_ID=";
+ sd_id128_t boot_id;
+ int r;
+
+ r = sd_journal_open(&j_tmp, SD_JOURNAL_LOCAL_ONLY /*SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY*/);
+ printf("journal open return %d\n", r);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal, cannot open journal:"), DLT_STRING(strerror(-r)));
printf("journal open failed: %s\n", strerror(-r));
- return;
+ j_tmp = NULL;
}
- if (conf->Journal.CurrentBoot) {
+ if (config->Journal.CurrentBoot) {
/* show only current boot entries */
r = sd_id128_get_boot(&boot_id);
-
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to get boot id:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
sd_id128_to_string(boot_id, match + 9);
- r = sd_journal_add_match(j, match, strlen(match));
-
+ r = sd_journal_add_match(j_tmp, match, strlen(match));
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to get match:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
}
- if (conf->Journal.Follow) {
+ if (config->Journal.Follow) {
/* show only last 10 entries and follow */
- r = sd_journal_seek_tail(j);
-
+ r = sd_journal_seek_tail(j_tmp);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to seek to tail:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
- r = sd_journal_previous_skip(j, 10);
-
+ r = sd_journal_previous_skip(j_tmp, 10);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to seek back 10 entries:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
}
- while (!threads.shutdown) {
-
- r = sd_journal_next(j);
-
- if (r < 0) {
- DLT_LOG(dltsystem, DLT_LOG_ERROR,
- DLT_STRING("dlt-system-journal failed to get next entry:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
- }
- else if (r > 0)
- {
- /* get all data from current journal entry */
- dlt_system_journal_get_timestamp(j, &timestamp);
-
- /* get data from journal entry, empty string if invalid fields */
- dlt_system_journal_get(j, buffer_comm, "_COMM", sizeof(buffer_comm));
- dlt_system_journal_get(j, buffer_pid, "_PID", sizeof(buffer_pid));
- dlt_system_journal_get(j, buffer_priority, "PRIORITY", sizeof(buffer_priority));
- dlt_system_journal_get(j, buffer_message, "MESSAGE", sizeof(buffer_message));
- dlt_system_journal_get(j, buffer_transport, "_TRANSPORT", sizeof(buffer_transport));
-
- /* prepare process string */
- if (strcmp(buffer_transport, "kernel") == 0)
- snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "kernel:");
- else
- snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s[%s]:", buffer_comm, buffer_pid);
-
- /* map log level on demand */
- loglevel = DLT_LOG_INFO;
- systemd_loglevel = atoi(buffer_priority);
-
- if (conf->Journal.MapLogLevels) {
- /* Map log levels from journal to DLT */
- switch (systemd_loglevel) {
- case 0: /* Emergency */
- case 1: /* Alert */
- case 2: /* Critical */
- loglevel = DLT_LOG_FATAL;
- break;
- case 3: /* Error */
- loglevel = DLT_LOG_ERROR;
- break;
- case 4: /* Warning */
- loglevel = DLT_LOG_WARN;
- break;
- case 5: /* Notice */
- case 6: /* Informational */
- loglevel = DLT_LOG_INFO;
- break;
- case 7: /* Debug */
- loglevel = DLT_LOG_DEBUG;
- break;
- default:
- loglevel = DLT_LOG_INFO;
- break;
- }
- }
-
- if ((systemd_loglevel >= 0) && (systemd_loglevel <= 7))
- snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s:", systemd_log_levels[systemd_loglevel]);
- else
- snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "prio_unknown:");
-
- /* write log entry */
- if (conf->Journal.UseOriginalTimestamp == 0) {
- DLT_LOG(journalContext, loglevel,
- DLT_STRING(timestamp.real),
- DLT_STRING(timestamp.monotonic),
- DLT_STRING(buffer_process),
- DLT_STRING(buffer_priority),
- DLT_STRING(buffer_message)
- );
-
- }
- else {
- /* since we are talking about points in time, I'd prefer truncating over arithmetic rounding */
- ts = (uint32_t)(atof(timestamp.monotonic) * 10000);
- DLT_LOG_TS(journalContext, loglevel, ts,
- DLT_STRING(timestamp.real),
- DLT_STRING(buffer_process),
- DLT_STRING(buffer_priority),
- DLT_STRING(buffer_message)
- );
- }
- }
- else {
- r = sd_journal_wait(j, 1000000);
-
- if (r < 0) {
- DLT_LOG(dltsystem, DLT_LOG_ERROR,
- DLT_STRING("dlt-system-journal failed to call sd_journal_get_realtime_usec(): "),
- DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
- }
- }
-
- if (journal_checkUserBufferForFreeSpace() == -1) {
- /* buffer is nearly full */
- /* wait 500ms for writing next entry */
- struct timespec t;
- t.tv_sec = 0;
- t.tv_nsec = 1000000ul * 500;
- nanosleep(&t, NULL);
- }
+ pollfd[i].fd = sd_journal_get_fd(j_tmp);
+ if(pollfd[i].fd < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while getting journal fd: "),
+ DLT_STRING(strerror(pollfd[i].fd)));
+ j_tmp = NULL;
}
+ pollfd[i].events = sd_journal_get_events(j_tmp);
+ if(pollfd[i].events < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while getting journal events: "),
+ DLT_STRING(strerror(pollfd[i].events)));
+ j_tmp = NULL;
+ }
+ *j = j_tmp;
+}
- sd_journal_close(j);
-
- DLT_UNREGISTER_CONTEXT(journalContext);
-
+void journal_fd_handler(sd_journal *j, DltSystemConfiguration *config)
+{
+ get_journal_msg(j, config);
}
-#endif /* DLT_SYSTEMD_JOURNAL_ENABLE */
+#endif /* DLT_SYSTEMD_JOURNAL_ENABLE */ \ No newline at end of file