46 #if defined(DLT_SYSTEMD_JOURNAL_ENABLE) 50 #include <sys/socket.h> 51 #include <netinet/in.h> 58 #include <systemd/sd-journal.h> 59 #include <systemd/sd-id128.h> 64 #define DLT_SYSTEM_JOURNAL_BUFFER_SIZE 256 65 #define DLT_SYSTEM_JOURNAL_BUFFER_SIZE_BIG 2048 67 #define DLT_SYSTEM_JOURNAL_ASCII_FIRST_VISIBLE_CHARACTER 31 68 #define DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH 9+32+1 72 char real[DLT_SYSTEM_JOURNAL_BUFFER_SIZE];
73 char monotonic[DLT_SYSTEM_JOURNAL_BUFFER_SIZE];
79 int journal_checkUserBufferForFreeSpace()
81 int total_size, used_size;
85 if((total_size - used_size) < (total_size/2))
92 int dlt_system_journal_get(sd_journal* j,
char *target,
const char *field,
size_t max_size)
100 if(max_size<1 || target == 0 || field == 0 || j == 0)
107 error_code = sd_journal_get_data(j, field,(
const void **) &data, &length);
114 field_size = strlen(field)+1;
117 if(length<field_size)
121 if(max_size<=(length-field_size))
124 strncpy(target,data+field_size,max_size-1);
125 target[max_size-1]=0;
130 strncpy(target,data+field_size,length-field_size);
131 target[length-field_size]=0;
142 void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *timestamp)
145 uint64_t time_secs = 0;
146 uint64_t time_usecs = 0;
147 struct tm * timeinfo =
NULL;
149 char buffer_realtime[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 };
150 char buffer_realtime_formatted[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 };
151 char buffer_monotime[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 };
154 ret = dlt_system_journal_get(journal, buffer_realtime,
"_SOURCE_REALTIME_TIMESTAMP",
sizeof(buffer_realtime));
155 if (ret == 0 && strlen(buffer_realtime) > 0)
158 time_usecs = strtoull(buffer_realtime,
NULL, 10);
164 if ((ret = sd_journal_get_realtime_usec(journal, &time_usecs)) < 0)
167 DLT_STRING(
"dlt-system-journal failed to get realtime: "),
175 time_secs = time_usecs / 1000000;
176 timeinfo = localtime((
const time_t*) (&time_secs));
177 strftime(buffer_realtime_formatted,
sizeof(buffer_realtime_formatted),
"%Y/%m/%d %H:%M:%S", timeinfo);
179 snprintf(timestamp->real,
sizeof(timestamp->real),
"%s.%06"PRIu64, buffer_realtime_formatted, time_usecs % 1000000);
182 ret = dlt_system_journal_get(journal, buffer_monotime,
"_SOURCE_MONOTONIC_TIMESTAMP",
sizeof(buffer_monotime));
183 if (ret == 0 && strlen(buffer_monotime) > 0)
186 time_usecs = strtoull(buffer_monotime,
NULL, 10);
192 if ((ret = sd_journal_get_monotonic_usec(journal, &time_usecs,
NULL)) < 0)
195 DLT_STRING(
"dlt-system-journal failed to get monotonic time: "),
203 snprintf(timestamp->monotonic,
sizeof(timestamp->monotonic),
"%"PRId64
".%06"PRIu64, time_usecs / 1000000, time_usecs % 1000000);
206 void journal_thread(
void *v_conf)
210 char match[DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH] =
"_BOOT_ID=";
213 char buffer_process[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
214 buffer_priority[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
215 buffer_pid[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
216 buffer_comm[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
217 buffer_message[DLT_SYSTEM_JOURNAL_BUFFER_SIZE_BIG] = { 0 },
218 buffer_transport[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 };
220 MessageTimestamp timestamp;
222 int loglevel,systemd_loglevel;
223 char* systemd_log_levels[] = {
"Emergency",
"Alert",
"Critical",
"Error",
"Warning",
"Notice",
"Informational",
"Debug" };
226 DLT_STRING(
"dlt-system-journal, in thread."));
231 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
232 printf(
"journal open return %d\n", r);
236 printf(
"journal open failed: %s\n", strerror(-r));
243 r = sd_id128_get_boot(&boot_id);
252 sd_id128_to_string(boot_id, match + 9);
253 r = sd_journal_add_match(j,match,strlen(match));
267 r = sd_journal_seek_tail(j);
276 r = sd_journal_previous_skip(j, 10);
291 r = sd_journal_next(j);
303 dlt_system_journal_get_timestamp(j, ×tamp);
306 dlt_system_journal_get(j,buffer_comm,
"_COMM",
sizeof(buffer_comm));
307 dlt_system_journal_get(j,buffer_pid,
"_PID",
sizeof(buffer_pid));
308 dlt_system_journal_get(j,buffer_priority,
"PRIORITY",
sizeof(buffer_priority));
309 dlt_system_journal_get(j,buffer_message,
"MESSAGE",
sizeof(buffer_message));
310 dlt_system_journal_get(j,buffer_transport,
"_TRANSPORT",
sizeof(buffer_transport));
313 if(strcmp(buffer_transport,
"kernel")==0)
314 snprintf(buffer_process,DLT_SYSTEM_JOURNAL_BUFFER_SIZE,
"kernel:");
316 snprintf(buffer_process,DLT_SYSTEM_JOURNAL_BUFFER_SIZE,
"%s[%s]:",buffer_comm,buffer_pid);
320 systemd_loglevel = atoi(buffer_priority);
324 switch(systemd_loglevel)
349 if(systemd_loglevel>=0 && systemd_loglevel<=7)
350 snprintf(buffer_priority,DLT_SYSTEM_JOURNAL_BUFFER_SIZE,
"%s:",systemd_log_levels[systemd_loglevel]);
352 snprintf(buffer_priority,DLT_SYSTEM_JOURNAL_BUFFER_SIZE,
"prio_unknown:");
355 DLT_LOG(journalContext, loglevel,
365 r = sd_journal_wait(j,1000000);
369 DLT_STRING(
"dlt-system-journal failed to call sd_journal_get_realtime_usec(): "),
DLT_STRING(strerror(-r)));
376 if(journal_checkUserBufferForFreeSpace()==-1)
382 t.tv_nsec = 1000000ul*500;
397 DLT_STRING(
"dlt-system-journal, start journal"));
398 static pthread_attr_t t_attr;
400 pthread_create(&pt, &t_attr, (
void *)journal_thread, conf);
static char data[kDataSize]
#define DLT_DECLARE_CONTEXT(CONTEXT)
DltReturnValue dlt_user_check_buffer(int *total_size, int *used_size)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
#define DLT_IMPORT_CONTEXT(CONTEXT)
pthread_t threads[MAX_THREADS]