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 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 37 | #include <libweston/libweston.h> |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 38 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 39 | /** |
| 40 | * \defgroup wlog weston-logging |
| 41 | */ |
| 42 | |
Matt Hoosier | 74742e0 | 2018-05-04 09:26:34 -0500 | [diff] [blame] | 43 | static int |
| 44 | default_log_handler(const char *fmt, va_list ap); |
| 45 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 46 | /** Needs to be set, defaults to default_log_handler |
| 47 | * |
| 48 | * \ingroup wlog |
| 49 | */ |
Matt Hoosier | 74742e0 | 2018-05-04 09:26:34 -0500 | [diff] [blame] | 50 | static log_func_t log_handler = default_log_handler; |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 51 | |
| 52 | /** Needs to be set, defaults to default_log_handler |
| 53 | * |
| 54 | * \ingroup wlog |
| 55 | */ |
Matt Hoosier | 74742e0 | 2018-05-04 09:26:34 -0500 | [diff] [blame] | 56 | static log_func_t log_continue_handler = default_log_handler; |
| 57 | |
| 58 | /** Sentinel log message handler |
| 59 | * |
| 60 | * This function is used as the default handler for log messages. It |
| 61 | * exists only to issue a noisy reminder to the user that a real handler |
| 62 | * must be installed prior to issuing logging calls. The process is |
| 63 | * immediately aborted after the reminder is printed. |
| 64 | * |
| 65 | * \param fmt The format string. Ignored. |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 66 | * \param ap The variadic argument list. Ignored. |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 67 | * |
| 68 | * \ingroup wlog |
Matt Hoosier | 74742e0 | 2018-05-04 09:26:34 -0500 | [diff] [blame] | 69 | */ |
| 70 | static int |
| 71 | default_log_handler(const char *fmt, va_list ap) |
| 72 | { |
| 73 | fprintf(stderr, "weston_log_set_handler() must be called before using of weston_log().\n"); |
| 74 | abort(); |
| 75 | } |
Derek Foreman | 6bc33d6 | 2015-06-08 11:37:31 -0500 | [diff] [blame] | 76 | |
Giulio Camuffo | be2b11a | 2016-06-02 21:48:13 +0300 | [diff] [blame] | 77 | /** Install the log handler |
| 78 | * |
| 79 | * The given functions will be called to output text as passed to the |
| 80 | * \a weston_log and \a weston_log_continue functions. |
| 81 | * |
| 82 | * \param log The log function. This function will be called when |
| 83 | * \a weston_log is called, and should begin a new line, |
| 84 | * with user defined line headers, if any. |
| 85 | * \param cont The continue log function. This function will be called |
| 86 | * when \a weston_log_continue is called, and should append |
| 87 | * its output to the current line, without any header or |
| 88 | * other content in between. |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 89 | * |
| 90 | * \ingroup wlog |
Giulio Camuffo | be2b11a | 2016-06-02 21:48:13 +0300 | [diff] [blame] | 91 | */ |
| 92 | WL_EXPORT void |
| 93 | weston_log_set_handler(log_func_t log, log_func_t cont) |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 94 | { |
Giulio Camuffo | be2b11a | 2016-06-02 21:48:13 +0300 | [diff] [blame] | 95 | log_handler = log; |
| 96 | log_continue_handler = cont; |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 99 | /** weston_vlog calls log_handler |
| 100 | * \ingroup wlog |
| 101 | */ |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 102 | WL_EXPORT int |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 103 | weston_vlog(const char *fmt, va_list ap) |
| 104 | { |
Giulio Camuffo | be2b11a | 2016-06-02 21:48:13 +0300 | [diff] [blame] | 105 | return log_handler(fmt, ap); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 108 | /** printf() equivalent in weston compositor. |
| 109 | * |
| 110 | * \rststar |
| 111 | * .. note:: |
| 112 | * |
| 113 | * Needs :var:`log_handler` to be set-up! |
| 114 | * \endrststar |
| 115 | * |
| 116 | * \ingroup wlog |
| 117 | */ |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 118 | WL_EXPORT int |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 119 | weston_log(const char *fmt, ...) |
| 120 | { |
| 121 | int l; |
| 122 | va_list argp; |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 123 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 124 | va_start(argp, fmt); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 125 | l = weston_vlog(fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 126 | va_end(argp); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 127 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 128 | return l; |
| 129 | } |
| 130 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 131 | /** weston_vlog_continue calls log_continue_handler |
| 132 | * |
| 133 | * \ingroup wlog |
| 134 | */ |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 135 | WL_EXPORT int |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 136 | weston_vlog_continue(const char *fmt, va_list argp) |
| 137 | { |
Giulio Camuffo | be2b11a | 2016-06-02 21:48:13 +0300 | [diff] [blame] | 138 | return log_continue_handler(fmt, argp); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Marius Vlad | 35ff4a8 | 2019-06-28 14:08:24 +0300 | [diff] [blame] | 141 | /** weston_log_continue |
| 142 | * |
| 143 | * \ingroup wlog |
| 144 | */ |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 145 | WL_EXPORT int |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 146 | weston_log_continue(const char *fmt, ...) |
| 147 | { |
| 148 | int l; |
| 149 | va_list argp; |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 150 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 151 | va_start(argp, fmt); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 152 | l = weston_vlog_continue(fmt, argp); |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 153 | va_end(argp); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 154 | |
Martin Minarik | 19e6f26 | 2012-06-07 13:08:46 +0200 | [diff] [blame] | 155 | return l; |
| 156 | } |