Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logging.c « libwdi « source « driver - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5de1a97c8c619a0b12658edd54aac9742edf4e5a (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * libwdi logging functions
 * Copyright (c) Johannes Erdfelt, Daniel Drake et al.
 * Copyright (c) 2010-2013 Pete Batard <pete@akeo.ie>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
#ifdef _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

#include <windows.h>
#include <config.h>
#include <stdarg.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stdint.h>

#include "libwdi.h"
#include "logging.h"

static HANDLE logger_rd_handle = INVALID_HANDLE_VALUE;
static HANDLE logger_wr_handle = INVALID_HANDLE_VALUE;
// Handle and Message for the destination Window when registered
static HWND logger_dest = NULL;
static UINT logger_msg = 0;
// Detect spurious log readouts
static unsigned log_messages_pending = 0;
// Keep track of how many bytes are in the pipe
static DWORD log_messages_pipe_size = 0;
// Global debug level
static int global_log_level = WDI_LOG_LEVEL_INFO;

extern char *windows_error_str(uint32_t retval);

static void write_to_pipe(const char* buffer, DWORD size, enum wdi_log_level level)
{
	DWORD written;

	// Earlier checks should ensure that this is never the case, but if it is
	// drop the message
	if (size > LOGGER_PIPE_SIZE)
		return;

	// Do not allow the pipe to outgrow its allocated size, as this freezes the app
	// See http://msdn.microsoft.com/en-us/library/aa365150.aspx
	// If we are about to overflow, we deplete the pipe queue by issuing a bunch of
	// SendMessage, which forces the messages to be processed (as opposed to PostMessage)
	if (((log_messages_pipe_size + size) > LOGGER_PIPE_SIZE) && (log_messages_pending > 0)) {
		while (log_messages_pending > 0)
			SendMessage(logger_dest, logger_msg, (WPARAM)level, 0);
		// Just in case
		log_messages_pipe_size = 0;
	}
	if (WriteFile(logger_wr_handle, buffer, size, &written, NULL))
		log_messages_pipe_size += written;
	log_messages_pending++;
	PostMessage(logger_dest, logger_msg, (WPARAM)level, 0);
}

static void pipe_wdi_log_v(enum wdi_log_level level,
	const char *function, const char *format, va_list args)
{
	char buffer[LOGBUF_SIZE];
	int size1, size2;
	BOOL truncated = FALSE;
	const char* prefix;
	const char* truncation_notice = "TRUNCATION detected for above line - Please "
		"send this log excerpt to the libwdi developers so we can fix it.";

	if (logger_wr_handle == INVALID_HANDLE_VALUE)
		return;

#ifndef ENABLE_DEBUG_LOGGING
	if (level < global_log_level)
		return;
#endif

	switch (level) {
	case WDI_LOG_LEVEL_DEBUG:
		prefix = "debug";
		break;
	case WDI_LOG_LEVEL_INFO:
		prefix = "info";
		break;
	case WDI_LOG_LEVEL_WARNING:
		prefix = "warning";
		break;
	case WDI_LOG_LEVEL_ERROR:
		prefix = "error";
		break;
	default:
		prefix = "unknown";
		break;
	}

	size1 = safe_snprintf(buffer, LOGBUF_SIZE, "libwdi:%s [%s] ", prefix, function);
	size2 = 0;
	if (size1 < 0) {
		buffer[LOGBUF_SIZE-1] = 0;
		size1 = LOGBUF_SIZE-1;
		truncated = TRUE;
	} else {
		size2 = safe_vsnprintf(buffer+size1, LOGBUF_SIZE-size1, format, args);
		if (size2 < 0) {
			buffer[LOGBUF_SIZE-1] = 0;
			size2 = LOGBUF_SIZE-1-size1;
			truncated = TRUE;
		}
	}

	write_to_pipe(buffer, size1+size2+1, level);
	if (truncated)
		write_to_pipe(truncation_notice, strlen(truncation_notice)+1, level);
}

static void console_wdi_log_v(enum wdi_log_level level,
	const char *function, const char *format, va_list args)
{
	FILE *stream;
	const char *prefix;

	stream = stdout;

#ifndef ENABLE_DEBUG_LOGGING
	if (level < global_log_level)
		return;
#endif

	switch (level) {
	case WDI_LOG_LEVEL_DEBUG:
		stream = stderr;
		prefix = "debug";
		break;
	case WDI_LOG_LEVEL_INFO:
		prefix = "info";
		break;
	case WDI_LOG_LEVEL_WARNING:
		stream = stderr;
		prefix = "warning";
		break;
	case WDI_LOG_LEVEL_ERROR:
		stream = stderr;
		prefix = "error";
		break;
	default:
		stream = stderr;
		prefix = "unknown";
		break;
	}

	fprintf(stream, "libwdi:%s [%s] ", prefix, function);

	vfprintf(stream, format, args);

	fprintf(stream, "\n");

}

void wdi_log(enum wdi_log_level level,
	const char *function, const char *format, ...)
{
	va_list args;

	va_start (args, format);
	if (logger_dest != NULL) {
		pipe_wdi_log_v(level, function, format, args);
	} else {
		console_wdi_log_v(level, function, format, args);
	}
	va_end (args);
}

// Create a synchronous pipe for messaging
static int create_logger(DWORD buffsize)
{
	if (buffsize == 0) {
		buffsize = LOGGER_PIPE_SIZE;
	}

	if (logger_wr_handle != INVALID_HANDLE_VALUE) {
		// We (supposedly) don't have logging, so try to reach a stderr
		fprintf(stderr, "trying to recreate logger pipe\n");
		return WDI_ERROR_EXISTS;
	}

	// Read end of the pipe
	logger_rd_handle = CreateNamedPipeA(LOGGER_PIPE_NAME, PIPE_ACCESS_INBOUND,
		PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE, 1, buffsize, buffsize, 0, NULL);
	if (logger_rd_handle == INVALID_HANDLE_VALUE) {
		fprintf(stderr, "could not create logger pipe for reading: %s\n", windows_error_str(0));
		return WDI_ERROR_RESOURCE;
	}

	// Write end of the pipe
	logger_wr_handle = CreateFileA(LOGGER_PIPE_NAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL, NULL);
	if (logger_wr_handle == INVALID_HANDLE_VALUE) {
		fprintf(stderr, "could not create logger pipe for writing: %s\n", windows_error_str(0));
		CloseHandle(logger_rd_handle);
		logger_rd_handle = INVALID_HANDLE_VALUE;
		return WDI_ERROR_RESOURCE;
	}

	log_messages_pending = 0;

	return WDI_SUCCESS;
}

// Destroy the logging pipe
static void destroy_logger(void)
{
	if (logger_wr_handle != INVALID_HANDLE_VALUE) {
		CloseHandle(logger_wr_handle);
		logger_wr_handle = INVALID_HANDLE_VALUE;
	}
	if (logger_rd_handle != INVALID_HANDLE_VALUE) {
		CloseHandle(logger_rd_handle);
		logger_rd_handle = INVALID_HANDLE_VALUE;
	}
}

/*
 * Register a Window as destination for logging message
 * This Window will be notified with a message event and should call
 * wdi_read_logger() to retrieve the message data
 */
int LIBWDI_API wdi_register_logger(HWND hWnd, UINT message, DWORD buffsize)
{
	int r;

	MUTEX_START;

	if (logger_dest != NULL) {
		MUTEX_RETURN(WDI_ERROR_EXISTS);
	}

	r = create_logger(buffsize);
	if (r == WDI_SUCCESS) {
		logger_dest = hWnd;
		logger_msg = message;
	}

	MUTEX_RETURN(r);
}

/*
 * Unregister a Window as destination for logging message
 */
int LIBWDI_API wdi_unregister_logger(HWND hWnd)
{
	MUTEX_START;

	if (logger_dest == NULL) {
		MUTEX_RETURN(WDI_SUCCESS);
	}

	if (logger_dest != hWnd) {
		MUTEX_RETURN(WDI_ERROR_INVALID_PARAM);
	}

	destroy_logger();
	logger_dest = NULL;
	logger_msg = 0;

	MUTEX_RETURN(WDI_SUCCESS);
}

/*
 * Read a log message
 */
int LIBWDI_API wdi_read_logger(char* buffer, DWORD buffer_size, DWORD* message_size)
{
	int size;
	DWORD r;

	MUTEX_START;

	if ( (logger_rd_handle == INVALID_HANDLE_VALUE) && (create_logger(0) != WDI_SUCCESS) ) {
		*message_size = 0;
		MUTEX_RETURN(WDI_ERROR_NOT_FOUND);
	}

	if (log_messages_pending == 0) {
		if (log_messages_pipe_size == 0) {
			buffer[0] = 0;
			*message_size = 0;
			MUTEX_RETURN(WDI_SUCCESS);
		}
		size = safe_snprintf(buffer, buffer_size, "ERROR: log buffer is empty");
		if (size < 0) {
			buffer[buffer_size-1] = 0;
			MUTEX_RETURN(buffer_size);
		}
		*message_size = (DWORD)size;
		MUTEX_RETURN(WDI_SUCCESS);
	}
	log_messages_pending--;

	if (ReadFile(logger_rd_handle, (void*)buffer, buffer_size, message_size, NULL)) {
		log_messages_pipe_size -= *message_size;
		MUTEX_RETURN(WDI_SUCCESS);
	}

	log_messages_pipe_size -= *message_size;
	*message_size = 0;
	r = GetLastError();
	if ((r == ERROR_INSUFFICIENT_BUFFER) || (r == ERROR_MORE_DATA)) {
		MUTEX_RETURN(WDI_ERROR_OVERFLOW);
	}
	MUTEX_RETURN(WDI_ERROR_IO);
}

/*
 * Set the global log level. Only works if ENABLE_DEBUG_LOGGING is not set
 */
int LIBWDI_API wdi_set_log_level(int level)
{
#if defined(ENABLE_DEBUG_LOGGING)
	return WDI_ERROR_NOT_SUPPORTED;
#endif
	global_log_level = level;
	return WDI_SUCCESS;
}