blob: 0042619bc1e0d693c92598a752f51f311381e633 [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>
27#include <time.h>
28
29#include <wayland-server.h>
30#include <wayland-util.h>
31
32#include "log.h"
33
34static FILE *weston_logfile = NULL;
35
36static char cached_timestamp[21];
37static int cached_tv_sec = 0;
38
39static int weston_log_timestamp(void)
40{
41 struct timespec tp;
42 unsigned int time;
43
44 clock_gettime(CLOCK_REALTIME, &tp);
45 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
46
47 if (cached_tv_sec != tp.tv_sec) {
48 char string[26];
49 struct tm *lt = localtime(&tp.tv_sec);
50
51 cached_tv_sec = tp.tv_sec;
52
53 strftime (string,24,"%Y-%m-%d %H:%M:%S", lt);
54 strncpy (cached_timestamp, string, 20);
55 }
56
57 return fprintf(weston_logfile, "[%s.%03u] ", cached_timestamp, tp.tv_nsec/1000000);
58}
59
60static int weston_log_print(const char *fmt, va_list arg)
61{
62 int l;
63 l = vfprintf(weston_logfile, fmt, arg);
64 fflush(weston_logfile);
65 return l;
66}
67
68static void
69custom_handler(const char *fmt, va_list arg)
70{
71 weston_log_timestamp();
72 fprintf(weston_logfile, "libwayland: ");
73 weston_log_print(fmt, arg);
74}
75
76void
77weston_log_file_open(const char *filename)
78{
79 wl_log_set_handler_server(custom_handler);
80
81 weston_logfile = fopen(filename, "a");
82 if (weston_logfile == NULL)
83 weston_logfile = stderr;
84}
85
86void
87weston_log_file_close()
88{
89 if (weston_logfile != stderr)
90 fclose(weston_logfile);
91 weston_logfile = stderr;
92}
93
94WL_EXPORT int
95weston_log(const char *fmt, ...)
96{
97 int l;
98 va_list argp;
99 va_start(argp, fmt);
100 l = weston_log_timestamp();
101 l += weston_log_print(fmt, argp);
102 va_end(argp);
103 return l;
104}
105
106WL_EXPORT int
107weston_log_continue(const char *fmt, ...)
108{
109 int l;
110 va_list argp;
111 va_start(argp, fmt);
112 l = weston_log_print(fmt, argp);
113 va_end(argp);
114 return l;
115}