summaryrefslogtreecommitdiff
path: root/src/system/dlt-system-journal.c
blob: e5419d36871f039f922f0dd9ff6f4c1d81ef7393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * @licence app begin@
 * Copyright (C) 2013  BMW AG
 *
 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
 * \author Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> BMW 2013
 *
 * \file dlt-system-journal.c
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt-system-journal.c                                          **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
**                                                                            **
**  PURPOSE   :                                                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: yes                                          **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/
#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)

#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <errno.h>

#include "dlt-system.h"

#include <systemd/sd-journal.h>

extern DltSystemThreads threads;

DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(journalContext)

int journal_checkUserBufferForFreeSpace()
{
	int total_size, used_size;

	dlt_user_check_buffer(&total_size, &used_size);

	if((total_size - used_size) < (total_size/2))
	{
		return -1;
	}
	return 1;
}

void journal_thread(void *v_conf)
{
	int r,r2,r3,r4,r5;
	sd_journal *j;

	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);
	if (r < 0) {
			DLT_LOG(dltsystem, DLT_LOG_ERROR,
					DLT_STRING("dlt-system-journal, cannot open journal."));
			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;

		// Jun 28 13:34:05 alexvbox systemd[1]: Started GENIVI DLT system.
		if(sd_journal_next(j)>0)
		{
			sd_journal_get_realtime_usec(j, &time_usecs);
			r2 = sd_journal_get_data(j, "_COMM",(const void **) &d2, &l);
			r3 = sd_journal_get_data(j, "_PID",(const void **) &d3, &l);
			r4 = sd_journal_get_data(j, "PRIORITY",(const void **) &d4, &l);
			r5 = sd_journal_get_data(j, "MESSAGE",(const void **) &d5, &l);
			if(r2>=0 && strlen(d2)>6) 	d2 +=6;
			if(r3>=0 && strlen(d3)>5) 	d3 +=5;
			if(r4>=0 && strlen(d4)>9) 	d4 +=9;
			if(r5>=0 && strlen(d5)>8) 	d5 +=8;
			
			if(r5>=0)
			{
				DLT_LOG(journalContext, DLT_LOG_INFO,
						DLT_UINT64(time_usecs),DLT_STRING(d2),DLT_STRING(d3),DLT_STRING(d4),DLT_STRING(d5));
			}

		}
		else
		{
			sd_journal_wait(j,1000000);			
		}

		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);
		}


	}

    sd_journal_close(j);

	DLT_UNREGISTER_CONTEXT(journalContext);

}

void start_systemd_journal(DltSystemConfiguration *conf)
{
	DLT_LOG(dltsystem, DLT_LOG_DEBUG,
			DLT_STRING("dlt-system-journal, start journal"));
	static pthread_attr_t t_attr;
	static pthread_t pt;
	pthread_create(&pt, &t_attr, (void *)journal_thread, conf);
	threads.threads[threads.count++] = pt;
}

#endif /* DLT_SYSTEMD_JOURNAL_ENABLE */