21 #include <winpr/config.h>
23 #include "JournaldAppender.h"
27 #include <systemd/sd-journal.h>
29 #include <winpr/crt.h>
30 #include <winpr/environment.h>
34 WLOG_APPENDER_COMMON();
37 } wLogJournaldAppender;
39 static BOOL WLog_JournaldAppender_Open(wLog* log, wLogAppender* appender)
42 wLogJournaldAppender* journaldAppender = NULL;
44 if (!log || !appender)
47 journaldAppender = (wLogJournaldAppender*)appender;
48 if (journaldAppender->stream)
51 fd = sd_journal_stream_fd(journaldAppender->identifier, LOG_INFO, 1);
55 journaldAppender->stream = fdopen(fd,
"w");
56 if (!journaldAppender->stream)
62 setbuffer(journaldAppender->stream, NULL, 0);
66 static BOOL WLog_JournaldAppender_Close(wLog* log, wLogAppender* appender)
68 if (!log || !appender)
74 static BOOL WLog_JournaldAppender_WriteMessage(wLog* log, wLogAppender* appender,
77 char* formatStr = NULL;
78 wLogJournaldAppender* journaldAppender = NULL;
79 char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
81 if (!log || !appender || !message)
84 journaldAppender = (wLogJournaldAppender*)appender;
86 switch (message->Level)
90 formatStr =
"<7>%s%s\n";
93 formatStr =
"<6>%s%s\n";
96 formatStr =
"<4>%s%s\n";
99 formatStr =
"<3>%s%s\n";
102 formatStr =
"<2>%s%s\n";
107 (void)fprintf(stderr,
"%s: unknown level %" PRIu32
"\n", __func__, message->Level);
111 message->PrefixString = prefix;
112 WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
114 if (message->Level != WLOG_OFF)
116 WINPR_PRAGMA_DIAG_PUSH
117 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL(
void)
118 fprintf(journaldAppender->stream, formatStr, message->PrefixString, message->TextString);
119 WINPR_PRAGMA_DIAG_POP
124 static BOOL WLog_JournaldAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
127 if (!log || !appender || !message)
133 static BOOL WLog_JournaldAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
136 if (!log || !appender || !message)
142 static BOOL WLog_JournaldAppender_Set(wLogAppender* appender,
const char* setting,
void* value)
144 wLogJournaldAppender* journaldAppender = (wLogJournaldAppender*)appender;
147 if (!value || (strnlen(value, 2) == 0))
150 if (strcmp(
"identifier", setting))
154 if (journaldAppender->stream)
157 if (journaldAppender->identifier)
158 free(journaldAppender->identifier);
160 return ((journaldAppender->identifier = _strdup((
const char*)value)) != NULL);
163 static void WLog_JournaldAppender_Free(wLogAppender* appender)
165 wLogJournaldAppender* journaldAppender = NULL;
168 journaldAppender = (wLogJournaldAppender*)appender;
169 if (journaldAppender->stream)
170 (void)fclose(journaldAppender->stream);
171 free(journaldAppender->identifier);
172 free(journaldAppender);
176 wLogAppender* WLog_JournaldAppender_New(wLog* log)
178 wLogJournaldAppender* appender = NULL;
180 LPCSTR name =
"WLOG_JOURNALD_ID";
182 appender = (wLogJournaldAppender*)calloc(1,
sizeof(wLogJournaldAppender));
186 appender->Type = WLOG_APPENDER_JOURNALD;
187 appender->Open = WLog_JournaldAppender_Open;
188 appender->Close = WLog_JournaldAppender_Close;
189 appender->WriteMessage = WLog_JournaldAppender_WriteMessage;
190 appender->WriteDataMessage = WLog_JournaldAppender_WriteDataMessage;
191 appender->WriteImageMessage = WLog_JournaldAppender_WriteImageMessage;
192 appender->Set = WLog_JournaldAppender_Set;
193 appender->Free = WLog_JournaldAppender_Free;
195 nSize = GetEnvironmentVariableA(name, NULL, 0);
198 appender->identifier = (LPSTR)malloc(nSize);
199 if (!appender->identifier)
202 if (GetEnvironmentVariableA(name, appender->identifier, nSize) != nSize - 1)
205 if (!WLog_JournaldAppender_Open(log, (wLogAppender*)appender))
209 return (wLogAppender*)appender;
212 free(appender->identifier);