summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/utils/wlog/Appender.c
blob: a1cbbd66a353e4d1067f7fde963f4efa6c6ba5fa (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
167
168
169
170
171
172
173
174
175
176
/**
 * WinPR: Windows Portable Runtime
 * WinPR Logger
 *
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <winpr/config.h>

#include "Appender.h"

void WLog_Appender_Free(wLog* log, wLogAppender* appender)
{
	if (!appender)
		return;

	if (appender->Layout)
	{
		WLog_Layout_Free(log, appender->Layout);
		appender->Layout = NULL;
	}

	DeleteCriticalSection(&appender->lock);
	appender->Free(appender);
}

wLogAppender* WLog_GetLogAppender(wLog* log)
{
	if (!log)
		return NULL;

	if (!log->Appender)
		return WLog_GetLogAppender(log->Parent);

	return log->Appender;
}

BOOL WLog_OpenAppender(wLog* log)
{
	int status = 0;
	wLogAppender* appender = NULL;

	appender = WLog_GetLogAppender(log);

	if (!appender)
		return FALSE;

	if (!appender->Open)
		return TRUE;

	if (!appender->active)
	{
		status = appender->Open(log, appender);
		appender->active = TRUE;
	}

	return status;
}

BOOL WLog_CloseAppender(wLog* log)
{
	int status = 0;
	wLogAppender* appender = NULL;

	appender = WLog_GetLogAppender(log);

	if (!appender)
		return FALSE;

	if (!appender->Close)
		return TRUE;

	if (appender->active)
	{
		status = appender->Close(log, appender);
		appender->active = FALSE;
	}

	return status;
}

static wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType)
{
	wLogAppender* appender = NULL;

	if (!log)
		return NULL;

	switch (logAppenderType)
	{
		case WLOG_APPENDER_CONSOLE:
			appender = WLog_ConsoleAppender_New(log);
			break;
		case WLOG_APPENDER_FILE:
			appender = WLog_FileAppender_New(log);
			break;
		case WLOG_APPENDER_BINARY:
			appender = WLog_BinaryAppender_New(log);
			break;
		case WLOG_APPENDER_CALLBACK:
			appender = WLog_CallbackAppender_New(log);
			break;
#ifdef WINPR_HAVE_SYSLOG_H
		case WLOG_APPENDER_SYSLOG:
			appender = WLog_SyslogAppender_New(log);
			break;
#endif
#ifdef WINPR_HAVE_JOURNALD_H
		case WLOG_APPENDER_JOURNALD:
			appender = WLog_JournaldAppender_New(log);
			break;
#endif
		case WLOG_APPENDER_UDP:
			appender = (wLogAppender*)WLog_UdpAppender_New(log);
			break;
		default:
			fprintf(stderr, "%s: unknown handler type %" PRIu32 "\n", __func__, logAppenderType);
			appender = NULL;
			break;
	}

	if (!appender)
		appender = (wLogAppender*)WLog_ConsoleAppender_New(log);

	if (!appender)
		return NULL;

	if (!(appender->Layout = WLog_Layout_New(log)))
	{
		WLog_Appender_Free(log, appender);
		return NULL;
	}

	InitializeCriticalSectionAndSpinCount(&appender->lock, 4000);

	return appender;
}

BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType)
{
	if (!log)
		return FALSE;

	if (log->Appender)
	{
		WLog_Appender_Free(log, log->Appender);
		log->Appender = NULL;
	}

	log->Appender = WLog_Appender_New(log, logAppenderType);
	return log->Appender != NULL;
}

BOOL WLog_ConfigureAppender(wLogAppender* appender, const char* setting, void* value)
{
	/* Just check the settings string is not empty */
	if (!appender || !setting || (strnlen(setting, 2) == 0))
		return FALSE;

	if (appender->Set)
		return appender->Set(appender, setting, value);
	else
		return FALSE;
}