blob: 911b0c631dff99b286deffc944c287e08dbc4bfe [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);
Martin Minarikbae43512012-06-11 01:00:23 +020049 if (brokendown_time->tm_mday != cached_tm_mday) {
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040050 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
51 fprintf(weston_logfile, "Date: %s\n", string);
Martin Minarikbae43512012-06-11 01:00:23 +020052
53 cached_tm_mday = brokendown_time->tm_mday;
54 }
Martin Minarik19e6f262012-06-07 13:08:46 +020055
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040056 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
57
Martin Minarikeb587652012-06-08 20:59:53 +020058 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
Martin Minarik19e6f262012-06-07 13:08:46 +020059}
60
61static void
62custom_handler(const char *fmt, va_list arg)
63{
64 weston_log_timestamp();
65 fprintf(weston_logfile, "libwayland: ");
Martin Minarikeb587652012-06-08 20:59:53 +020066 vfprintf(weston_logfile, fmt, arg);
Martin Minarik19e6f262012-06-07 13:08:46 +020067}
68
69void
70weston_log_file_open(const char *filename)
71{
72 wl_log_set_handler_server(custom_handler);
73
Martin Minarikeb587652012-06-08 20:59:53 +020074 if (filename != NULL)
75 weston_logfile = fopen(filename, "a");
76
Martin Minarik19e6f262012-06-07 13:08:46 +020077 if (weston_logfile == NULL)
78 weston_logfile = stderr;
Martin Minarikeb587652012-06-08 20:59:53 +020079 else
80 setvbuf(weston_logfile, NULL, _IOLBF, 256);
Martin Minarik19e6f262012-06-07 13:08:46 +020081}
82
83void
84weston_log_file_close()
85{
Martin Minarikeb587652012-06-08 20:59:53 +020086 if ((weston_logfile != stderr) && (weston_logfile != NULL))
Martin Minarik19e6f262012-06-07 13:08:46 +020087 fclose(weston_logfile);
88 weston_logfile = stderr;
89}
90
91WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -040092weston_vlog(const char *fmt, va_list ap)
93{
94 int l;
95
96 l = weston_log_timestamp();
97 l += vfprintf(weston_logfile, fmt, ap);
98
99 return l;
100}
101
102WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200103weston_log(const char *fmt, ...)
104{
105 int l;
106 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400107
Martin Minarik19e6f262012-06-07 13:08:46 +0200108 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400109 l = weston_vlog(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200110 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400111
Martin Minarik19e6f262012-06-07 13:08:46 +0200112 return l;
113}
114
115WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400116weston_vlog_continue(const char *fmt, va_list argp)
117{
118 return vfprintf(weston_logfile, fmt, argp);
119}
120
121WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200122weston_log_continue(const char *fmt, ...)
123{
124 int l;
125 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400126
Martin Minarik19e6f262012-06-07 13:08:46 +0200127 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400128 l = weston_vlog_continue(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200129 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400130
Martin Minarik19e6f262012-06-07 13:08:46 +0200131 return l;
132}