blob: 0302c8caa31862d1f68217b3afb8a6316dd889e8 [file] [log] [blame]
Martin Minarik19e6f262012-06-07 13:08:46 +02001/*
2 * Copyright © 2012 Martin Minarik
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Martin Minarik19e6f262012-06-07 13:08:46 +020011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Martin Minarik19e6f262012-06-07 13:08:46 +020024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Martin Minarik19e6f262012-06-07 13:08:46 +020028#include <stdio.h>
29#include <stdarg.h>
30#include <stdlib.h>
31#include <string.h>
Martin Minarikeb587652012-06-08 20:59:53 +020032#include <sys/time.h>
Martin Minarik19e6f262012-06-07 13:08:46 +020033#include <time.h>
34
Martin Minarik19e6f262012-06-07 13:08:46 +020035#include <wayland-util.h>
36
Kristian Høgsberg64eca892012-08-01 00:00:57 -040037#include "compositor.h"
Martin Minarik19e6f262012-06-07 13:08:46 +020038
Derek Foreman6bc33d62015-06-08 11:37:31 -050039#include "os-compatibility.h"
40
Martin Minarik19e6f262012-06-07 13:08:46 +020041static FILE *weston_logfile = NULL;
42
Martin Minarikbae43512012-06-11 01:00:23 +020043static int cached_tm_mday = -1;
44
Martin Minarik19e6f262012-06-07 13:08:46 +020045static int weston_log_timestamp(void)
46{
Martin Minarikeb587652012-06-08 20:59:53 +020047 struct timeval tv;
48 struct tm *brokendown_time;
49 char string[128];
Martin Minarik19e6f262012-06-07 13:08:46 +020050
Martin Minarikeb587652012-06-08 20:59:53 +020051 gettimeofday(&tv, NULL);
Martin Minarik19e6f262012-06-07 13:08:46 +020052
Martin Minarikeb587652012-06-08 20:59:53 +020053 brokendown_time = localtime(&tv.tv_sec);
U. Artie Eoff1db00722014-01-15 08:24:40 -080054 if (brokendown_time == NULL)
55 return fprintf(weston_logfile, "[(NULL)localtime] ");
56
Martin Minarikbae43512012-06-11 01:00:23 +020057 if (brokendown_time->tm_mday != cached_tm_mday) {
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040058 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
59 fprintf(weston_logfile, "Date: %s\n", string);
Martin Minarikbae43512012-06-11 01:00:23 +020060
61 cached_tm_mday = brokendown_time->tm_mday;
62 }
Martin Minarik19e6f262012-06-07 13:08:46 +020063
Kristian Høgsberg9ae2f112013-06-19 09:07:28 -040064 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
65
Martin Minarikeb587652012-06-08 20:59:53 +020066 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
Martin Minarik19e6f262012-06-07 13:08:46 +020067}
68
69static void
70custom_handler(const char *fmt, va_list arg)
71{
72 weston_log_timestamp();
73 fprintf(weston_logfile, "libwayland: ");
Martin Minarikeb587652012-06-08 20:59:53 +020074 vfprintf(weston_logfile, fmt, arg);
Martin Minarik19e6f262012-06-07 13:08:46 +020075}
76
77void
78weston_log_file_open(const char *filename)
79{
80 wl_log_set_handler_server(custom_handler);
81
Derek Foreman6bc33d62015-06-08 11:37:31 -050082 if (filename != NULL) {
Martin Minarikeb587652012-06-08 20:59:53 +020083 weston_logfile = fopen(filename, "a");
Derek Foreman6bc33d62015-06-08 11:37:31 -050084 if (weston_logfile)
85 os_fd_set_cloexec(fileno(weston_logfile));
86 }
Martin Minarikeb587652012-06-08 20:59:53 +020087
Martin Minarik19e6f262012-06-07 13:08:46 +020088 if (weston_logfile == NULL)
89 weston_logfile = stderr;
Martin Minarikeb587652012-06-08 20:59:53 +020090 else
91 setvbuf(weston_logfile, NULL, _IOLBF, 256);
Martin Minarik19e6f262012-06-07 13:08:46 +020092}
93
94void
95weston_log_file_close()
96{
Martin Minarikeb587652012-06-08 20:59:53 +020097 if ((weston_logfile != stderr) && (weston_logfile != NULL))
Martin Minarik19e6f262012-06-07 13:08:46 +020098 fclose(weston_logfile);
99 weston_logfile = stderr;
100}
101
102WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400103weston_vlog(const char *fmt, va_list ap)
104{
105 int l;
106
107 l = weston_log_timestamp();
108 l += vfprintf(weston_logfile, fmt, ap);
109
110 return l;
111}
112
113WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200114weston_log(const char *fmt, ...)
115{
116 int l;
117 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400118
Martin Minarik19e6f262012-06-07 13:08:46 +0200119 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400120 l = weston_vlog(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200121 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400122
Martin Minarik19e6f262012-06-07 13:08:46 +0200123 return l;
124}
125
126WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400127weston_vlog_continue(const char *fmt, va_list argp)
128{
129 return vfprintf(weston_logfile, fmt, argp);
130}
131
132WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200133weston_log_continue(const char *fmt, ...)
134{
135 int l;
136 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400137
Martin Minarik19e6f262012-06-07 13:08:46 +0200138 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400139 l = weston_vlog_continue(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200140 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400141
Martin Minarik19e6f262012-06-07 13:08:46 +0200142 return l;
143}