1 /*
2 * Copyright © 2012 Martin Minarik
3 *
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:
11 *
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.
24 */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <time.h>
34
35 #include <wayland-util.h>
36
37 #include <libweston/libweston.h>
38 #include "weston-log-internal.h"
39
40 /**
41 * \defgroup wlog weston-logging
42 */
43
44 static int
45 default_log_handler(const char *fmt, va_list ap);
46
47 /** Needs to be set, defaults to default_log_handler
48 *
49 * \ingroup wlog
50 */
51 static log_func_t log_handler = default_log_handler;
52
53 /** Needs to be set, defaults to default_log_handler
54 *
55 * \ingroup wlog
56 */
57 static log_func_t log_continue_handler = default_log_handler;
58
59 /** Sentinel log message handler
60 *
61 * This function is used as the default handler for log messages. It
62 * exists only to issue a noisy reminder to the user that a real handler
63 * must be installed prior to issuing logging calls. The process is
64 * immediately aborted after the reminder is printed.
65 *
66 * \param fmt The format string. Ignored.
67 * \param ap The variadic argument list. Ignored.
68 *
69 * \ingroup wlog
70 */
71 static int
default_log_handler(const char * fmt,va_list ap)72 default_log_handler(const char *fmt, va_list ap)
73 {
74 fprintf(stderr, "weston_log_set_handler() must be called before using of weston_log().\n");
75 abort();
76 }
77
78 /** Install the log handler
79 *
80 * The given functions will be called to output text as passed to the
81 * \a weston_log and \a weston_log_continue functions.
82 *
83 * \param log The log function. This function will be called when
84 * \a weston_log is called, and should begin a new line,
85 * with user defined line headers, if any.
86 * \param cont The continue log function. This function will be called
87 * when \a weston_log_continue is called, and should append
88 * its output to the current line, without any header or
89 * other content in between.
90 *
91 * \ingroup wlog
92 */
93 WL_EXPORT void
weston_log_set_handler(log_func_t log,log_func_t cont)94 weston_log_set_handler(log_func_t log, log_func_t cont)
95 {
96 log_handler = log;
97 log_continue_handler = cont;
98 }
99
100 /** weston_vlog calls log_handler
101 * \ingroup wlog
102 */
103 WL_EXPORT int
weston_vlog(const char * fmt,va_list ap)104 weston_vlog(const char *fmt, va_list ap)
105 {
106 return log_handler(fmt, ap);
107 }
108
109 /** printf() equivalent in weston compositor.
110 *
111 * \rststar
112 * .. note::
113 *
114 * Needs :var:`log_handler` to be set-up!
115 * \endrststar
116 *
117 * \ingroup wlog
118 */
119 WL_EXPORT int
weston_log(const char * fmt,...)120 weston_log(const char *fmt, ...)
121 {
122 int l;
123 va_list argp;
124
125 va_start(argp, fmt);
126 l = weston_vlog(fmt, argp);
127 va_end(argp);
128
129 return l;
130 }
131
132 /** weston_vlog_continue calls log_continue_handler
133 *
134 * \ingroup wlog
135 */
136 WL_EXPORT int
weston_vlog_continue(const char * fmt,va_list argp)137 weston_vlog_continue(const char *fmt, va_list argp)
138 {
139 return log_continue_handler(fmt, argp);
140 }
141
142 /** weston_log_continue
143 *
144 * \ingroup wlog
145 */
146 WL_EXPORT int
weston_log_continue(const char * fmt,...)147 weston_log_continue(const char *fmt, ...)
148 {
149 int l;
150 va_list argp;
151
152 va_start(argp, fmt);
153 l = weston_vlog_continue(fmt, argp);
154 va_end(argp);
155
156 return l;
157 }
158