blob: 99bbe180dc7520bd6dbd5380740feb42772caf08 [file] [log] [blame]
Martin Minarik19e6f262012-06-07 13:08:46 +02001/*
2 * Copyright © 2012 Martin Minarik
3 *
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.
13 *
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.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Martin Minarik19e6f262012-06-07 13:08:46 +020025#include <stdio.h>
26#include <stdarg.h>
27#include <stdlib.h>
28#include <string.h>
Martin Minarikeb587652012-06-08 20:59:53 +020029#include <sys/time.h>
Martin Minarik19e6f262012-06-07 13:08:46 +020030#include <time.h>
31
Martin Minarik19e6f262012-06-07 13:08:46 +020032#include <wayland-util.h>
33
Kristian Høgsberg64eca892012-08-01 00:00:57 -040034#include "compositor.h"
Martin Minarik19e6f262012-06-07 13:08:46 +020035
36static FILE *weston_logfile = NULL;
37
Martin Minarikbae43512012-06-11 01:00:23 +020038static int cached_tm_mday = -1;
39
Martin Minarik19e6f262012-06-07 13:08:46 +020040static int weston_log_timestamp(void)
41{
Martin Minarikeb587652012-06-08 20:59:53 +020042 struct timeval tv;
43 struct tm *brokendown_time;
44 char string[128];
Martin Minarik19e6f262012-06-07 13:08:46 +020045
Martin Minarikeb587652012-06-08 20:59:53 +020046 gettimeofday(&tv, NULL);
Martin Minarik19e6f262012-06-07 13:08:46 +020047
Martin Minarikeb587652012-06-08 20:59:53 +020048 brokendown_time = localtime(&tv.tv_sec);
U. Artie Eoff1db00722014-01-15 08:24:40 -080049 if (brokendown_time == NULL)
50 return fprintf(weston_logfile, "[(NULL)localtime] ");
51
Martin Minarikbae43512012-06-11 01:00:23 +020052 if (brokendown_time->tm_mday != cached_tm_mday) {
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040053 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
54 fprintf(weston_logfile, "Date: %s\n", string);
Martin Minarikbae43512012-06-11 01:00:23 +020055
56 cached_tm_mday = brokendown_time->tm_mday;
57 }
Martin Minarik19e6f262012-06-07 13:08:46 +020058
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040059 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
60
Martin Minarikeb587652012-06-08 20:59:53 +020061 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
Martin Minarik19e6f262012-06-07 13:08:46 +020062}
63
64static void
65custom_handler(const char *fmt, va_list arg)
66{
67 weston_log_timestamp();
68 fprintf(weston_logfile, "libwayland: ");
Martin Minarikeb587652012-06-08 20:59:53 +020069 vfprintf(weston_logfile, fmt, arg);
Martin Minarik19e6f262012-06-07 13:08:46 +020070}
71
72void
73weston_log_file_open(const char *filename)
74{
75 wl_log_set_handler_server(custom_handler);
76
Martin Minarikeb587652012-06-08 20:59:53 +020077 if (filename != NULL)
78 weston_logfile = fopen(filename, "a");
79
Martin Minarik19e6f262012-06-07 13:08:46 +020080 if (weston_logfile == NULL)
81 weston_logfile = stderr;
Martin Minarikeb587652012-06-08 20:59:53 +020082 else
83 setvbuf(weston_logfile, NULL, _IOLBF, 256);
Martin Minarik19e6f262012-06-07 13:08:46 +020084}
85
86void
87weston_log_file_close()
88{
Martin Minarikeb587652012-06-08 20:59:53 +020089 if ((weston_logfile != stderr) && (weston_logfile != NULL))
Martin Minarik19e6f262012-06-07 13:08:46 +020090 fclose(weston_logfile);
91 weston_logfile = stderr;
92}
93
94WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -040095weston_vlog(const char *fmt, va_list ap)
96{
97 int l;
98
99 l = weston_log_timestamp();
100 l += vfprintf(weston_logfile, fmt, ap);
101
102 return l;
103}
104
105WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200106weston_log(const char *fmt, ...)
107{
108 int l;
109 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400110
Martin Minarik19e6f262012-06-07 13:08:46 +0200111 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400112 l = weston_vlog(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200113 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400114
Martin Minarik19e6f262012-06-07 13:08:46 +0200115 return l;
116}
117
118WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400119weston_vlog_continue(const char *fmt, va_list argp)
120{
121 return vfprintf(weston_logfile, fmt, argp);
122}
123
124WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200125weston_log_continue(const char *fmt, ...)
126{
127 int l;
128 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400129
Martin Minarik19e6f262012-06-07 13:08:46 +0200130 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400131 l = weston_vlog_continue(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200132 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400133
Martin Minarik19e6f262012-06-07 13:08:46 +0200134 return l;
135}