[PATCH 1/2] New logging functionality

ustun.ergenoglu at gmail.com ustun.ergenoglu at gmail.com
Thu Feb 2 14:16:00 PST 2012


From: Ustun Ergenoglu <ego at ustun.fi>

Added logging fuctions that print out to a logfile and stderr. Logging levels
can be specified.

Signed-off-by: Ustun Ergenoglu <ego at ustun.fi>
---
 src/Makefile.am   |    6 ++-
 src/wayland-log.c |  131 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/wayland-log.h |   67 +++++++++++++++++++++++++++
 3 files changed, 202 insertions(+), 2 deletions(-)
 create mode 100644 src/wayland-log.c
 create mode 100644 src/wayland-log.h

diff --git a/src/Makefile.am b/src/Makefile.am
index f356b54..944d24d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,7 +7,8 @@ include_HEADERS =				\
 	wayland-server.h			\
 	wayland-client-protocol.h		\
 	wayland-client.h			\
-	wayland-egl.h
+	wayland-egl.h				\
+	wayland-log.h
 
 libwayland_util_la_SOURCES =			\
 	connection.c				\
@@ -21,7 +22,8 @@ libwayland_server_la_SOURCES =			\
 	wayland-server.c			\
 	wayland-shm.c				\
 	data-device.c				\
-	event-loop.c
+	event-loop.c				\
+	wayland-log.c
 
 libwayland_client_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt
 libwayland_client_la_SOURCES =			\
diff --git a/src/wayland-log.c b/src/wayland-log.c
new file mode 100644
index 0000000..eb7528d
--- /dev/null
+++ b/src/wayland-log.c
@@ -0,0 +1,131 @@
+/*
+ * Copyright © 2012 Üstün Ergenoğlu
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "wayland-log.h"
+#include "wayland-util.h"
+
+#define LOG_FILE_FORMAT "wayland.%d.log"
+
+static FILE *log_file = NULL;
+static wl_log_level log_level = WL_LOG_DEBUG;
+
+static const char *
+_log_level_to_string(wl_log_level level);
+
+static void
+_format_timestamp(char outstr[], size_t size);
+
+WL_EXPORT void
+wl_log_init(void)
+{
+	if (log_file != NULL)
+		return;
+
+	const char *runtime_dir;
+	char *log_file_name = NULL;
+
+	runtime_dir = getenv("XDG_RUNTIME_DIR");
+	if (runtime_dir == NULL) {
+		runtime_dir = ".";
+	}
+
+	asprintf(&log_file_name, "%s/" LOG_FILE_FORMAT, runtime_dir, getpid());
+
+	log_file = fopen(log_file_name, "w");
+	free(log_file_name);
+}
+
+WL_EXPORT void
+wl_log_close(void)
+{
+	if (log_file != NULL)
+		fclose(log_file);
+	log_file = NULL;
+}
+
+WL_EXPORT void
+wl_log_set_log_level(wl_log_level level)
+{
+	log_level = level;
+}
+
+WL_EXPORT void
+wl_log(wl_log_level level, const char *format, ...)
+{
+	va_list args;
+	va_start(args, format);
+
+	wl_log_va(level, format, args);
+}
+
+WL_EXPORT void
+wl_log_va(wl_log_level level, const char *format, va_list args)
+{
+	if (log_level > level)
+		return;
+
+	char *log_line;
+	vasprintf(&log_line, format, args);
+
+	char timestamp[80] = {0, };
+	_format_timestamp(timestamp, sizeof(timestamp)-1);
+
+	if (log_file != NULL)
+		fprintf(log_file, "%s: [%s] %s\n", _log_level_to_string(level), timestamp, log_line);
+	fprintf(stderr, "%s: [%s] %s\n", _log_level_to_string(level), timestamp, log_line);
+	free(log_line);
+
+	fflush(log_file);
+}
+
+const char *
+_log_level_to_string(wl_log_level level)
+{
+	switch (level) {
+		case WL_LOG_DEBUG:
+			return "DEBUG";
+		case WL_LOG_INFO:
+			return "INFO";
+		case WL_LOG_WARNING:
+			return "WARNING";
+		case WL_LOG_ERROR:
+			return "ERROR";
+		default:
+			return "UNKNOWN";
+	}
+}
+
+void
+_format_timestamp(char outstr[], size_t size)
+{
+	time_t t = time((time_t *) 0);
+	struct tm *timeinfo = localtime(&t);
+
+	strftime(outstr, size, "%F %T", timeinfo);
+}
diff --git a/src/wayland-log.h b/src/wayland-log.h
new file mode 100644
index 0000000..2c656ff
--- /dev/null
+++ b/src/wayland-log.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright © 2012 Üstün Ergenoğlu
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef WAYLAND_LOG_H
+#define WAYLAND_LOG_H
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+#include <stdarg.h>
+
+typedef enum {
+	WL_LOG_DEBUG,
+	WL_LOG_INFO,
+	WL_LOG_WARNING,
+	WL_LOG_ERROR,
+	WL_LOG_UNKNOWN,
+} wl_log_level;
+
+/*
+ * Open the log file to be written. The wl_log calls before
+ * this only go to stderr.
+ */
+void
+wl_log_init(void);
+
+void
+wl_log_close(void);
+
+/*
+ * Set a filter level for debugging output. If the given log
+ * message is of lower priority than the set level it is omitted.
+ */
+void
+wl_log_set_log_level(wl_log_level level);
+
+void
+wl_log(wl_log_level level, const char *format, ...);
+
+void
+wl_log_va(wl_log_level level, const char *format, va_list args);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif
-- 
1.7.5.4



More information about the wayland-devel mailing list