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 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 27 | #include <sys/time.h> |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 28 | #include <time.h> |
| 29 | |
| 30 | #include <wayland-server.h> |
| 31 | #include <wayland-util.h> |
| 32 | |
| 33 | #include "log.h" |
| 34 | |
| 35 | static FILE *weston_logfile = NULL; |
| 36 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 37 | static int weston_log_timestamp(void) |
| 38 | { |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 39 | struct timeval tv; |
| 40 | struct tm *brokendown_time; |
| 41 | char string[128]; |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 42 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 43 | gettimeofday(&tv, NULL); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 44 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 45 | brokendown_time = localtime(&tv.tv_sec); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 46 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 47 | strftime(string, sizeof string, "%Y-%m-%d %H:%M:%S", brokendown_time); |
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 | return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void |
| 53 | custom_handler(const char *fmt, va_list arg) |
| 54 | { |
| 55 | weston_log_timestamp(); |
| 56 | fprintf(weston_logfile, "libwayland: "); |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 57 | vfprintf(weston_logfile, fmt, arg); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | weston_log_file_open(const char *filename) |
| 62 | { |
| 63 | wl_log_set_handler_server(custom_handler); |
| 64 | |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 65 | if (filename != NULL) |
| 66 | weston_logfile = fopen(filename, "a"); |
| 67 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 68 | if (weston_logfile == NULL) |
| 69 | weston_logfile = stderr; |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 70 | else |
| 71 | setvbuf(weston_logfile, NULL, _IOLBF, 256); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
| 75 | weston_log_file_close() |
| 76 | { |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 77 | if ((weston_logfile != stderr) && (weston_logfile != NULL)) |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 78 | fclose(weston_logfile); |
| 79 | weston_logfile = stderr; |
| 80 | } |
| 81 | |
| 82 | WL_EXPORT int |
| 83 | weston_log(const char *fmt, ...) |
| 84 | { |
| 85 | int l; |
| 86 | va_list argp; |
| 87 | va_start(argp, fmt); |
| 88 | l = weston_log_timestamp(); |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 89 | l += vfprintf(weston_logfile, fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 90 | va_end(argp); |
| 91 | return l; |
| 92 | } |
| 93 | |
| 94 | WL_EXPORT int |
| 95 | weston_log_continue(const char *fmt, ...) |
| 96 | { |
| 97 | int l; |
| 98 | va_list argp; |
| 99 | va_start(argp, fmt); |
Martin Minarik | eb58765 | 2012-06-08 20:59:53 +0200 | [diff] [blame^] | 100 | l = vfprintf(weston_logfile, fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 101 | va_end(argp); |
| 102 | return l; |
| 103 | } |