[PATCH v2 1/2] Added logging methods.
Ustun Ergenoglu
ustun.ergenoglu at gmail.com
Mon Feb 13 14:30:12 PST 2012
From: Üstün Ergenoğlu <ustun.ergenoglu at gmail.com>
Signed-off-by: Üstün Ergenoğlu <ego at ustun.fi>
---
src/Makefile.am | 2 +
src/compositor.c | 3 +
src/log.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/log.h | 75 +++++++++++++++++++++++++++++
4 files changed, 221 insertions(+), 0 deletions(-)
create mode 100644 src/log.c
create mode 100644 src/log.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 2c91abc..3bdd0cd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,8 @@ weston_SOURCES = \
util.c \
matrix.c \
matrix.h \
+ log.c \
+ log.h \
$(xserver_launcher_sources)
if ENABLE_SETUID_INSTALL
diff --git a/src/compositor.c b/src/compositor.c
index ab90ded..7ffadbd 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -49,6 +49,7 @@
#include <wayland-server.h>
#include "compositor.h"
+#include "log.h"
static const char *option_socket_name = NULL;
@@ -2235,6 +2236,7 @@ int main(int argc, char *argv[])
}
}
+ wl_log_init();
display = wl_display_create();
loop = wl_display_get_event_loop(display);
@@ -2316,6 +2318,7 @@ int main(int argc, char *argv[])
ec->destroy(ec);
wl_display_destroy(display);
+ wl_log_close();
return 0;
}
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..e289d20
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,141 @@
+/*
+ * 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-util.h>
+
+#include "log.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);
+
+ va_end(args);
+}
+
+WL_EXPORT void
+wl_log_va(wl_log_level level, const char *format, va_list args)
+{
+ if (log_level > level)
+ return;
+
+ char *log_text;
+ char *log_line;
+ vasprintf(&log_text, format, args);
+
+ char timestamp[80] = {0, };
+ _format_timestamp(timestamp, sizeof(timestamp)-1);
+
+ const char log_format[] = "%s: [%s] %s\n";
+ asprintf(&log_line, log_format,
+ _log_level_to_string(level), timestamp,
+ log_text);
+ free(log_text);
+
+ if (log_file != NULL) {
+ fprintf(log_file, log_line);
+ fflush(log_file);
+ }
+
+ fprintf(stderr, log_line);
+ free(log_line);
+}
+
+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/log.h b/src/log.h
new file mode 100644
index 0000000..2ecc729
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,75 @@
+/*
+ * 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_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);
+
+#define __FILELINE(str) ___FILELINE(__FILE__, __LINE__, str)
+#define ___FILELINE(x, y, z) x ":" __STR(y) " " z
+#define __STR(x) # x
+
+#define WL_LOG_D(str, ...) wl_log(WL_LOG_DEBUG, __FILELINE(str), ##__VA_ARGS__)
+#define WL_LOG_I(str, ...) wl_log(WL_LOG_INFO, __FILELINE(str), ##__VA_ARGS__)
+#define WL_LOG_W(str, ...) wl_log(WL_LOG_WARNING, __FILELINE(str), ##__VA_ARGS__)
+#define WL_LOG_E(str, ...) wl_log(WL_LOG_ERROR, __FILELINE(str), ##__VA_ARGS__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--
1.7.9
More information about the wayland-devel
mailing list