2 * Copyright © 2012 Martin Minarik
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include <wayland-util.h>
34 #include "compositor.h"
36 static FILE *weston_logfile = NULL;
38 static int cached_tm_mday = -1;
40 static int weston_log_timestamp(void)
43 struct tm *brokendown_time;
46 gettimeofday(&tv, NULL);
48 brokendown_time = localtime(&tv.tv_sec);
49 if (brokendown_time == NULL)
50 return fprintf(weston_logfile, "[(NULL)localtime] ");
52 if (brokendown_time->tm_mday != cached_tm_mday) {
53 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
54 fprintf(weston_logfile, "Date: %s\n", string);
56 cached_tm_mday = brokendown_time->tm_mday;
59 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
61 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
65 custom_handler(const char *fmt, va_list arg)
67 weston_log_timestamp();
68 fprintf(weston_logfile, "libwayland: ");
69 vfprintf(weston_logfile, fmt, arg);
73 weston_log_file_open(const char *filename)
75 wl_log_set_handler_server(custom_handler);
78 weston_logfile = fopen(filename, "a");
80 if (weston_logfile == NULL)
81 weston_logfile = stderr;
83 setvbuf(weston_logfile, NULL, _IOLBF, 256);
87 weston_log_file_close()
89 if ((weston_logfile != stderr) && (weston_logfile != NULL))
90 fclose(weston_logfile);
91 weston_logfile = stderr;
95 weston_vlog(const char *fmt, va_list ap)
99 l = weston_log_timestamp();
100 l += vfprintf(weston_logfile, fmt, ap);
106 weston_log(const char *fmt, ...)
112 l = weston_vlog(fmt, argp);
119 weston_vlog_continue(const char *fmt, va_list argp)
121 return vfprintf(weston_logfile, fmt, argp);
125 weston_log_continue(const char *fmt, ...)
131 l = weston_vlog_continue(fmt, argp);