blob: 1c05e25a085adb77a3352eccc9774d2edfc002fc [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
32#include <wayland-server.h>
33#include <wayland-util.h>
34
Kristian Høgsberg64eca892012-08-01 00:00:57 -040035#include "compositor.h"
Martin Minarik19e6f262012-06-07 13:08:46 +020036
37static FILE *weston_logfile = NULL;
38
Martin Minarikbae43512012-06-11 01:00:23 +020039static int cached_tm_mday = -1;
40
Martin Minarik19e6f262012-06-07 13:08:46 +020041static int weston_log_timestamp(void)
42{
Martin Minarikeb587652012-06-08 20:59:53 +020043 struct timeval tv;
44 struct tm *brokendown_time;
45 char string[128];
Martin Minarik19e6f262012-06-07 13:08:46 +020046
Martin Minarikeb587652012-06-08 20:59:53 +020047 gettimeofday(&tv, NULL);
Martin Minarik19e6f262012-06-07 13:08:46 +020048
Martin Minarikeb587652012-06-08 20:59:53 +020049 brokendown_time = localtime(&tv.tv_sec);
Martin Minarik19e6f262012-06-07 13:08:46 +020050
Martin Minarikbae43512012-06-11 01:00:23 +020051 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
52
53 if (brokendown_time->tm_mday != cached_tm_mday) {
54 char date_string[128];
55
56 strftime(date_string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
57 fprintf(weston_logfile, "Date: %s\n", date_string);
58
59 cached_tm_mday = brokendown_time->tm_mday;
60 }
Martin Minarik19e6f262012-06-07 13:08:46 +020061
Martin Minarikeb587652012-06-08 20:59:53 +020062 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
Martin Minarik19e6f262012-06-07 13:08:46 +020063}
64
65static void
66custom_handler(const char *fmt, va_list arg)
67{
68 weston_log_timestamp();
69 fprintf(weston_logfile, "libwayland: ");
Martin Minarikeb587652012-06-08 20:59:53 +020070 vfprintf(weston_logfile, fmt, arg);
Martin Minarik19e6f262012-06-07 13:08:46 +020071}
72
73void
74weston_log_file_open(const char *filename)
75{
76 wl_log_set_handler_server(custom_handler);
77
Martin Minarikeb587652012-06-08 20:59:53 +020078 if (filename != NULL)
79 weston_logfile = fopen(filename, "a");
80
Martin Minarik19e6f262012-06-07 13:08:46 +020081 if (weston_logfile == NULL)
82 weston_logfile = stderr;
Martin Minarikeb587652012-06-08 20:59:53 +020083 else
84 setvbuf(weston_logfile, NULL, _IOLBF, 256);
Martin Minarik19e6f262012-06-07 13:08:46 +020085}
86
87void
88weston_log_file_close()
89{
Martin Minarikeb587652012-06-08 20:59:53 +020090 if ((weston_logfile != stderr) && (weston_logfile != NULL))
Martin Minarik19e6f262012-06-07 13:08:46 +020091 fclose(weston_logfile);
92 weston_logfile = stderr;
93}
94
95WL_EXPORT int
96weston_log(const char *fmt, ...)
97{
98 int l;
99 va_list argp;
100 va_start(argp, fmt);
101 l = weston_log_timestamp();
Martin Minarikeb587652012-06-08 20:59:53 +0200102 l += vfprintf(weston_logfile, fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200103 va_end(argp);
104 return l;
105}
106
107WL_EXPORT int
108weston_log_continue(const char *fmt, ...)
109{
110 int l;
111 va_list argp;
112 va_start(argp, fmt);
Martin Minarikeb587652012-06-08 20:59:53 +0200113 l = vfprintf(weston_logfile, fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200114 va_end(argp);
115 return l;
116}