blob: 0452e65e302d917c15d76cae75ad5fe2e792a086 [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
23#include <stdio.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <string.h>
Martin Minarikeb587652012-06-08 20:59:53 +020027#include <sys/time.h>
Martin Minarik19e6f262012-06-07 13:08:46 +020028#include <time.h>
29
30#include <wayland-server.h>
31#include <wayland-util.h>
32
33#include "log.h"
34
35static FILE *weston_logfile = NULL;
36
Martin Minarikbae43512012-06-11 01:00:23 +020037static int cached_tm_mday = -1;
38
Martin Minarik19e6f262012-06-07 13:08:46 +020039static int weston_log_timestamp(void)
40{
Martin Minarikeb587652012-06-08 20:59:53 +020041 struct timeval tv;
42 struct tm *brokendown_time;
43 char string[128];
Martin Minarik19e6f262012-06-07 13:08:46 +020044
Martin Minarikeb587652012-06-08 20:59:53 +020045 gettimeofday(&tv, NULL);
Martin Minarik19e6f262012-06-07 13:08:46 +020046
Martin Minarikeb587652012-06-08 20:59:53 +020047 brokendown_time = localtime(&tv.tv_sec);
Martin Minarik19e6f262012-06-07 13:08:46 +020048
Martin Minarikbae43512012-06-11 01:00:23 +020049 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
50
51 if (brokendown_time->tm_mday != cached_tm_mday) {
52 char date_string[128];
53
54 strftime(date_string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
55 fprintf(weston_logfile, "Date: %s\n", date_string);
56
57 cached_tm_mday = brokendown_time->tm_mday;
58 }
Martin Minarik19e6f262012-06-07 13:08:46 +020059
Martin Minarikeb587652012-06-08 20:59:53 +020060 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
Martin Minarik19e6f262012-06-07 13:08:46 +020061}
62
63static void
64custom_handler(const char *fmt, va_list arg)
65{
66 weston_log_timestamp();
67 fprintf(weston_logfile, "libwayland: ");
Martin Minarikeb587652012-06-08 20:59:53 +020068 vfprintf(weston_logfile, fmt, arg);
Martin Minarik19e6f262012-06-07 13:08:46 +020069}
70
71void
72weston_log_file_open(const char *filename)
73{
74 wl_log_set_handler_server(custom_handler);
75
Martin Minarikeb587652012-06-08 20:59:53 +020076 if (filename != NULL)
77 weston_logfile = fopen(filename, "a");
78
Martin Minarik19e6f262012-06-07 13:08:46 +020079 if (weston_logfile == NULL)
80 weston_logfile = stderr;
Martin Minarikeb587652012-06-08 20:59:53 +020081 else
82 setvbuf(weston_logfile, NULL, _IOLBF, 256);
Martin Minarik19e6f262012-06-07 13:08:46 +020083}
84
85void
86weston_log_file_close()
87{
Martin Minarikeb587652012-06-08 20:59:53 +020088 if ((weston_logfile != stderr) && (weston_logfile != NULL))
Martin Minarik19e6f262012-06-07 13:08:46 +020089 fclose(weston_logfile);
90 weston_logfile = stderr;
91}
92
93WL_EXPORT int
94weston_log(const char *fmt, ...)
95{
96 int l;
97 va_list argp;
98 va_start(argp, fmt);
99 l = weston_log_timestamp();
Martin Minarikeb587652012-06-08 20:59:53 +0200100 l += vfprintf(weston_logfile, fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200101 va_end(argp);
102 return l;
103}
104
105WL_EXPORT int
106weston_log_continue(const char *fmt, ...)
107{
108 int l;
109 va_list argp;
110 va_start(argp, fmt);
Martin Minarikeb587652012-06-08 20:59:53 +0200111 l = vfprintf(weston_logfile, fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200112 va_end(argp);
113 return l;
114}