From 4dcc75370545070b4037058fcb447cdbe132b701 Mon Sep 17 00:00:00 2001 From: Alexander Wenzel Date: Wed, 17 Jul 2013 16:57:33 +0200 Subject: Added options for journal logging. Signed-off-by: Alexander Wenzel --- src/system/CMakeLists.txt | 2 +- src/system/dlt-system-journal.c | 121 ++++++++++++++++++++++++++++++++-------- src/system/dlt-system-options.c | 17 +++++- src/system/dlt-system.conf | 17 ++++++ src/system/dlt-system.h | 3 + 5 files changed, 135 insertions(+), 25 deletions(-) diff --git a/src/system/CMakeLists.txt b/src/system/CMakeLists.txt index e503688..7070bfc 100644 --- a/src/system/CMakeLists.txt +++ b/src/system/CMakeLists.txt @@ -23,7 +23,7 @@ set(dlt_system_SRCS dlt-system dlt-system-options dlt-system-process-handling dlt-system-syslog dlt-system-watchdog dlt-system-journal) add_executable(dlt-system ${dlt_system_SRCS} ${systemd_SRCS}) if(WITH_SYSTEMD_JOURNAL) - target_link_libraries(dlt-system dlt z systemd-journal) + target_link_libraries(dlt-system dlt z systemd-journal systemd-id128) else(WITH_SYSTEMD_JOURNAL) target_link_libraries(dlt-system dlt z) endif(WITH_SYSTEMD_JOURNAL) diff --git a/src/system/dlt-system-journal.c b/src/system/dlt-system-journal.c index e5419d3..8445682 100644 --- a/src/system/dlt-system-journal.c +++ b/src/system/dlt-system-journal.c @@ -47,10 +47,12 @@ #include #include #include +#include #include "dlt-system.h" #include +#include extern DltSystemThreads threads; @@ -74,43 +76,83 @@ void journal_thread(void *v_conf) { int r,r2,r3,r4,r5; sd_journal *j; - + char match[9+32+1] = "_BOOT_ID="; + sd_id128_t boot_id; + DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-journal, in thread.")); DltSystemConfiguration *conf = (DltSystemConfiguration *) v_conf; DLT_REGISTER_CONTEXT(journalContext, conf->Journal.ContextId, "Journal Adapter"); - r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY); + r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY/*SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY*/); if (r < 0) { DLT_LOG(dltsystem, DLT_LOG_ERROR, - DLT_STRING("dlt-system-journal, cannot open journal.")); + DLT_STRING("dlt-system-journal, cannot open journal:"),DLT_STRING(strerror(-r))); return; } + + if(conf->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_id128_to_string(boot_id, match + 9); + r = sd_journal_add_match(j,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; + + } + } + + if(conf->Journal.Follow) + { + /* show only last 10 entries and follow */ + r = sd_journal_seek_tail(j); + 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; + + } + r = sd_journal_previous_skip(j, 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; + + } - // Uncomment if only new jornal entries should be shown - //sd_journal_seek_tail(j); - - /* possible mapping of log levels */ - /* journal log levels - 0 Emergency DLT_LOG_CRITICAL - 1 Alert DLT_LOG_CRITICAL - 2 Critical DLT_LOG_CRITICAL - 3 Error DLT_LOG_ERROR - 4 Warning DLT_LOG_WARNING - 5 Notice DLT_LOG_WARNING - 6 Informational DLT_LOG_INFO - 7 Debug DLT_LOG_DEBUG - */ + } while(!threads.shutdown) { const char *d2="",*d3="",*d4="",*d5=""; size_t l; uint64_t time_usecs = 0; + int ret; + struct tm * timeinfo; + char buffer[256],buffer2[256]; + int loglevel,systemd_loglevel; // Jun 28 13:34:05 alexvbox systemd[1]: Started GENIVI DLT system. - if(sd_journal_next(j)>0) + ret = sd_journal_next(j); + if(ret>0) { sd_journal_get_realtime_usec(j, &time_usecs); r2 = sd_journal_get_data(j, "_COMM",(const void **) &d2, &l); @@ -122,18 +164,52 @@ void journal_thread(void *v_conf) if(r4>=0 && strlen(d4)>9) d4 +=9; if(r5>=0 && strlen(d5)>8) d5 +=8; - if(r5>=0) + time_usecs /=1000000; + timeinfo = localtime ((const time_t*)(&(time_usecs))); + strftime (buffer,sizeof(buffer),"%Y/%m/%d %H:%M:%S",timeinfo); + + sprintf(buffer2,"%s[%s]:",d2,d3); + + loglevel = DLT_LOG_INFO; + if(conf->Journal.MapLogLevels) { - DLT_LOG(journalContext, DLT_LOG_INFO, - DLT_UINT64(time_usecs),DLT_STRING(d2),DLT_STRING(d3),DLT_STRING(d4),DLT_STRING(d5)); + /* Map log levels from journal to DLT */ + systemd_loglevel = atoi(d4); + 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 */ + case 5: /* Notice */ + loglevel = DLT_LOG_WARN; + break; + case 6: /* Informational */ + loglevel = DLT_LOG_INFO; + break; + case 7: /* Debug */ + loglevel = DLT_LOG_DEBUG; + break; + default: + loglevel = DLT_LOG_INFO; + break; + } } + + DLT_LOG(journalContext, loglevel, + DLT_STRING(buffer),DLT_STRING(buffer2),DLT_STRING(d4),DLT_STRING(d5)); } else { sd_journal_wait(j,1000000); } - + if(journal_checkUserBufferForFreeSpace()==-1) { // buffer is nearly full @@ -144,7 +220,6 @@ void journal_thread(void *v_conf) nanosleep(&t, NULL); } - } sd_journal_close(j); diff --git a/src/system/dlt-system-options.c b/src/system/dlt-system-options.c index 1a35adc..a12a36c 100644 --- a/src/system/dlt-system-options.c +++ b/src/system/dlt-system-options.c @@ -140,7 +140,10 @@ void init_configuration(DltSystemConfiguration *config) // Journal config->Journal.Enable = 0; - config->Syslog.ContextId = "JOUR"; + config->Journal.ContextId = "JOUR"; + config->Journal.CurrentBoot = 1; + config->Journal.Follow = 0; + config->Journal.MapLogLevels = 0; // File transfer config->Filetransfer.Enable = 0; @@ -268,6 +271,18 @@ int read_configuration_file(DltSystemConfiguration *config, char *file_name) MALLOC_ASSERT(config->Journal.ContextId); strcpy(config->Journal.ContextId, value); } + else if(strcmp(token, "JournalCurrentBoot") == 0) + { + config->Journal.CurrentBoot = atoi(value); + } + else if(strcmp(token, "JournalFollow") == 0) + { + config->Journal.Follow = atoi(value); + } + else if(strcmp(token, "JournalMapLogLevels") == 0) + { + config->Journal.MapLogLevels = atoi(value); + } // File transfer else if(strcmp(token, "FiletransferEnable") == 0) diff --git a/src/system/dlt-system.conf b/src/system/dlt-system.conf index 9012db7..46e9598 100644 --- a/src/system/dlt-system.conf +++ b/src/system/dlt-system.conf @@ -34,6 +34,23 @@ JournalEnable = 0 # The Context Id of the journal adapter (Default: JOUR) JournalContextId = JOUR +# Show only log entries of current boot (Default: 1) +JournalCurrentBoot = 1 + +# Show only the last 10 entries and follow (Default: 0) +JournalFollow = 0 + +# Map the log levels (Default: 0) +# Mapping journal log levels to DLT log levels +# 0 Emergency DLT_LOG_FATAL +# 1 Alert DLT_LOG_FATAL +# 2 Critical DLT_LOG_FATAL +# 3 Error DLT_LOG_ERROR +# 4 Warning DLT_LOG_WARN +# 5 Notice DLT_LOG_WARN +# 6 Informational DLT_LOG_INFO +# 7 Debug DLT_LOG_DEBUG +JournalMapLogLevels = 0 ######################################################################## # Filetransfer Manager diff --git a/src/system/dlt-system.h b/src/system/dlt-system.h index 28a1b46..aed71a6 100644 --- a/src/system/dlt-system.h +++ b/src/system/dlt-system.h @@ -97,6 +97,9 @@ typedef struct { typedef struct { int Enable; char *ContextId; + int CurrentBoot; + int Follow; + int MapLogLevels; } JournalOptions; typedef struct { -- cgit v1.2.1