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