Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 1 | /* |
| 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 Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 29 | #include <sys/time.h> |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 30 | #include <time.h> |
| 31 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 32 | #include <wayland-util.h> |
| 33 | |
Kristian Høgsberg | 64eca89 | 2012-08-01 00:00:57 -0400 | [diff] [blame] | 34 | #include "compositor.h" |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 35 | |
| 36 | static FILE *weston_logfile = NULL; |
| 37 | |
Martin Minarik | bae4351 | 2012-06-11 01:00:23 +0200 | [diff] [blame] | 38 | static int cached_tm_mday = -1; |
| 39 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 40 | static int weston_log_timestamp(void) |
| 41 | { |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 42 | struct timeval tv; |
| 43 | struct tm *brokendown_time; |
| 44 | char string[128]; |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 45 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 46 | gettimeofday(&tv, NULL); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 47 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 48 | brokendown_time = localtime(&tv.tv_sec); |
U. Artie Eoff | 1db0072 | 2014-01-15 08:24:40 -0800 | [diff] [blame] | 49 | if (brokendown_time == NULL) |
| 50 | return fprintf(weston_logfile, "[(NULL)localtime] "); |
| 51 | |
Martin Minarik | bae4351 | 2012-06-11 01:00:23 +0200 | [diff] [blame] | 52 | if (brokendown_time->tm_mday != cached_tm_mday) { |
Kristian Høgsberg | 9ae2f11 | 2013-06-19 09:07:28 -0400 | [diff] [blame] | 53 | strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time); |
| 54 | fprintf(weston_logfile, "Date: %s\n", string); |
Martin Minarik | bae4351 | 2012-06-11 01:00:23 +0200 | [diff] [blame] | 55 | |
| 56 | cached_tm_mday = brokendown_time->tm_mday; |
| 57 | } |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 58 | |
Kristian Høgsberg | 9ae2f11 | 2013-06-19 09:07:28 -0400 | [diff] [blame] | 59 | strftime(string, sizeof string, "%H:%M:%S", brokendown_time); |
| 60 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 61 | return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void |
| 65 | custom_handler(const char *fmt, va_list arg) |
| 66 | { |
| 67 | weston_log_timestamp(); |
| 68 | fprintf(weston_logfile, "libwayland: "); |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 69 | vfprintf(weston_logfile, fmt, arg); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
| 73 | weston_log_file_open(const char *filename) |
| 74 | { |
| 75 | wl_log_set_handler_server(custom_handler); |
| 76 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 77 | if (filename != NULL) |
| 78 | weston_logfile = fopen(filename, "a"); |
| 79 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 80 | if (weston_logfile == NULL) |
| 81 | weston_logfile = stderr; |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 82 | else |
| 83 | setvbuf(weston_logfile, NULL, _IOLBF, 256); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
| 87 | weston_log_file_close() |
| 88 | { |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame] | 89 | if ((weston_logfile != stderr) && (weston_logfile != NULL)) |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 90 | fclose(weston_logfile); |
| 91 | weston_logfile = stderr; |
| 92 | } |
| 93 | |
| 94 | WL_EXPORT int |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 95 | weston_vlog(const char *fmt, va_list ap) |
| 96 | { |
| 97 | int l; |
| 98 | |
| 99 | l = weston_log_timestamp(); |
| 100 | l += vfprintf(weston_logfile, fmt, ap); |
| 101 | |
| 102 | return l; |
| 103 | } |
| 104 | |
| 105 | WL_EXPORT int |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 106 | weston_log(const char *fmt, ...) |
| 107 | { |
| 108 | int l; |
| 109 | va_list argp; |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 110 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 111 | va_start(argp, fmt); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 112 | l = weston_vlog(fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 113 | va_end(argp); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 114 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 115 | return l; |
| 116 | } |
| 117 | |
| 118 | WL_EXPORT int |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 119 | weston_vlog_continue(const char *fmt, va_list argp) |
| 120 | { |
| 121 | return vfprintf(weston_logfile, fmt, argp); |
| 122 | } |
| 123 | |
| 124 | WL_EXPORT int |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 125 | weston_log_continue(const char *fmt, ...) |
| 126 | { |
| 127 | int l; |
| 128 | va_list argp; |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 129 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 130 | va_start(argp, fmt); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 131 | l = weston_vlog_continue(fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 132 | va_end(argp); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 133 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 134 | return l; |
| 135 | } |