blob: 8e0838ed9f69ac7b2c6971dc45a178b5b6337952 [file] [log] [blame]
Martin Minarik19e6f262012-06-07 13:08:46 +02001/*
2 * Copyright © 2012 Martin Minarik
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * 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 Minarik19e6f262012-06-07 13:08:46 +020011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * 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 Minarik19e6f262012-06-07 13:08:46 +020024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Martin Minarik19e6f262012-06-07 13:08:46 +020028#include <stdio.h>
29#include <stdarg.h>
30#include <stdlib.h>
31#include <string.h>
Martin Minarikeb587652012-06-08 20:59:53 +020032#include <sys/time.h>
Martin Minarik19e6f262012-06-07 13:08:46 +020033#include <time.h>
34
Martin Minarik19e6f262012-06-07 13:08:46 +020035#include <wayland-util.h>
36
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020037#include <libweston/libweston.h>
Guillaume Champagnef1e8fc92020-01-27 20:14:29 -050038#include "weston-log-internal.h"
limin.tianc616dad2024-07-15 11:35:38 +000039#include "aml-weston/aml-util.h"
Martin Minarik19e6f262012-06-07 13:08:46 +020040
Marius Vlad35ff4a82019-06-28 14:08:24 +030041/**
42 * \defgroup wlog weston-logging
43 */
44
Matt Hoosier74742e02018-05-04 09:26:34 -050045static int
46default_log_handler(const char *fmt, va_list ap);
47
Marius Vlad35ff4a82019-06-28 14:08:24 +030048/** Needs to be set, defaults to default_log_handler
49 *
50 * \ingroup wlog
51 */
Matt Hoosier74742e02018-05-04 09:26:34 -050052static log_func_t log_handler = default_log_handler;
Marius Vlad35ff4a82019-06-28 14:08:24 +030053
54/** Needs to be set, defaults to default_log_handler
55 *
56 * \ingroup wlog
57 */
Matt Hoosier74742e02018-05-04 09:26:34 -050058static log_func_t log_continue_handler = default_log_handler;
59
60/** Sentinel log message handler
61 *
62 * This function is used as the default handler for log messages. It
63 * exists only to issue a noisy reminder to the user that a real handler
64 * must be installed prior to issuing logging calls. The process is
65 * immediately aborted after the reminder is printed.
66 *
67 * \param fmt The format string. Ignored.
Marius Vlada2dace22019-06-12 16:05:44 +030068 * \param ap The variadic argument list. Ignored.
Marius Vlad35ff4a82019-06-28 14:08:24 +030069 *
70 * \ingroup wlog
Matt Hoosier74742e02018-05-04 09:26:34 -050071 */
72static int
73default_log_handler(const char *fmt, va_list ap)
74{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +020075 fprintf(stderr, "weston_log_set_handler() must be called before using of weston_log().\n");
76 abort();
Matt Hoosier74742e02018-05-04 09:26:34 -050077}
Derek Foreman6bc33d62015-06-08 11:37:31 -050078
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030079/** Install the log handler
80 *
81 * The given functions will be called to output text as passed to the
82 * \a weston_log and \a weston_log_continue functions.
83 *
84 * \param log The log function. This function will be called when
85 * \a weston_log is called, and should begin a new line,
86 * with user defined line headers, if any.
87 * \param cont The continue log function. This function will be called
88 * when \a weston_log_continue is called, and should append
89 * its output to the current line, without any header or
90 * other content in between.
Marius Vlad35ff4a82019-06-28 14:08:24 +030091 *
92 * \ingroup wlog
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030093 */
limin.tianc616dad2024-07-15 11:35:38 +000094
95static bool disable_log = false;
96
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030097WL_EXPORT void
98weston_log_set_handler(log_func_t log, log_func_t cont)
Martin Minarik19e6f262012-06-07 13:08:46 +020099{
limin.tianc616dad2024-07-15 11:35:38 +0000100 disable_log = disable_all_log();
101
Giulio Camuffobe2b11a2016-06-02 21:48:13 +0300102 log_handler = log;
103 log_continue_handler = cont;
Martin Minarik19e6f262012-06-07 13:08:46 +0200104}
105
Marius Vlad35ff4a82019-06-28 14:08:24 +0300106/** weston_vlog calls log_handler
107 * \ingroup wlog
108 */
Martin Minarik19e6f262012-06-07 13:08:46 +0200109WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400110weston_vlog(const char *fmt, va_list ap)
111{
Giulio Camuffobe2b11a2016-06-02 21:48:13 +0300112 return log_handler(fmt, ap);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400113}
114
Marius Vlad35ff4a82019-06-28 14:08:24 +0300115/** printf() equivalent in weston compositor.
116 *
117 * \rststar
118 * .. note::
119 *
120 * Needs :var:`log_handler` to be set-up!
121 * \endrststar
122 *
123 * \ingroup wlog
124 */
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400125WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200126weston_log(const char *fmt, ...)
127{
limin.tianc616dad2024-07-15 11:35:38 +0000128 if (disable_log) {
129 return 0;
130 }
Martin Minarik19e6f262012-06-07 13:08:46 +0200131 int l;
132 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400133
Martin Minarik19e6f262012-06-07 13:08:46 +0200134 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400135 l = weston_vlog(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200136 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400137
Martin Minarik19e6f262012-06-07 13:08:46 +0200138 return l;
139}
140
Marius Vlad35ff4a82019-06-28 14:08:24 +0300141/** weston_vlog_continue calls log_continue_handler
142 *
143 * \ingroup wlog
144 */
Martin Minarik19e6f262012-06-07 13:08:46 +0200145WL_EXPORT int
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400146weston_vlog_continue(const char *fmt, va_list argp)
147{
Giulio Camuffobe2b11a2016-06-02 21:48:13 +0300148 return log_continue_handler(fmt, argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400149}
150
Marius Vlad35ff4a82019-06-28 14:08:24 +0300151/** weston_log_continue
152 *
153 * \ingroup wlog
154 */
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400155WL_EXPORT int
Martin Minarik19e6f262012-06-07 13:08:46 +0200156weston_log_continue(const char *fmt, ...)
157{
158 int l;
159 va_list argp;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400160
Martin Minarik19e6f262012-06-07 13:08:46 +0200161 va_start(argp, fmt);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400162 l = weston_vlog_continue(fmt, argp);
Martin Minarik19e6f262012-06-07 13:08:46 +0200163 va_end(argp);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400164
Martin Minarik19e6f262012-06-07 13:08:46 +0200165 return l;
166}