xref: /netbsd-src/lib/librefuse/refuse_log.c (revision 64af69d8bcf2319d0ece64fd1119c52b58cc125c)
1*64af69d8Stnn /* $NetBSD: refuse_log.c,v 1.2 2022/01/29 00:03:41 tnn Exp $ */
2153b9c14Spho 
3153b9c14Spho /*
4153b9c14Spho  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5153b9c14Spho  * All rights reserved.
6153b9c14Spho  *
7153b9c14Spho  * Redistribution and use in source and binary forms, with or without
8153b9c14Spho  * modification, are permitted provided that the following conditions
9153b9c14Spho  * are met:
10153b9c14Spho  * 1. Redistributions of source code must retain the above copyright
11153b9c14Spho  *    notice, this list of conditions and the following disclaimer.
12153b9c14Spho  * 2. Redistributions in binary form must reproduce the above copyright
13153b9c14Spho  *    notice, this list of conditions and the following disclaimer in the
14153b9c14Spho  *    documentation and/or other materials provided with the distribution.
15153b9c14Spho  * 3. The name of the author may not be used to endorse or promote
16153b9c14Spho  *    products derived from this software without specific prior written
17153b9c14Spho  *    permission.
18153b9c14Spho  *
19153b9c14Spho  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20153b9c14Spho  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21153b9c14Spho  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22153b9c14Spho  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23153b9c14Spho  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24153b9c14Spho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25153b9c14Spho  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26153b9c14Spho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27153b9c14Spho  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28153b9c14Spho  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29153b9c14Spho  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30153b9c14Spho  */
31153b9c14Spho 
32153b9c14Spho #include <sys/cdefs.h>
33153b9c14Spho #if !defined(lint)
34*64af69d8Stnn __RCSID("$NetBSD: refuse_log.c,v 1.2 2022/01/29 00:03:41 tnn Exp $");
35153b9c14Spho #endif /* !lint */
36153b9c14Spho 
37153b9c14Spho #include <assert.h>
38153b9c14Spho #include <fuse_log.h>
39153b9c14Spho #if defined(MULTITHREADED_REFUSE)
40153b9c14Spho #	include <pthread.h>
41153b9c14Spho #endif
42153b9c14Spho #include <stdio.h>
43153b9c14Spho 
44*64af69d8Stnn static void  __printflike(2, 0)
default_log_func(enum fuse_log_level level,const char * fmt,va_list ap)45153b9c14Spho default_log_func(enum fuse_log_level level __attribute__((__unused__)),
46153b9c14Spho                  const char *fmt, va_list ap) {
47153b9c14Spho     /* This function needs to be thread-safe. Calling vfprintf(3)
48153b9c14Spho      * should be okay because POSIX mandates locking FILE* objects
49153b9c14Spho      * internally. */
50153b9c14Spho     vfprintf(stderr, fmt, ap);
51153b9c14Spho }
52153b9c14Spho 
53153b9c14Spho #if defined(MULTITHREADED_REFUSE)
54153b9c14Spho static pthread_mutex_t	log_func_mutex = PTHREAD_MUTEX_INITIALIZER;
55153b9c14Spho #endif
56153b9c14Spho static fuse_log_func_t	log_func = default_log_func;
57153b9c14Spho 
58153b9c14Spho void
fuse_set_log_func(fuse_log_func_t func)59153b9c14Spho fuse_set_log_func(fuse_log_func_t func) {
60153b9c14Spho #if defined(MULTITHREADED_REFUSE)
61153b9c14Spho     /* What we really need here is merely a memory barrier, but
62153b9c14Spho      * locking a mutex is the easiest way to achieve that. */
63153b9c14Spho     int rv;
64153b9c14Spho 
65153b9c14Spho     rv = pthread_mutex_lock(&log_func_mutex);
66153b9c14Spho     assert(rv == 0);
67153b9c14Spho #endif
68153b9c14Spho 
69153b9c14Spho     if (func)
70153b9c14Spho         log_func = func;
71153b9c14Spho     else
72153b9c14Spho         log_func = default_log_func;
73153b9c14Spho 
74153b9c14Spho #if defined(MULTITHREADED_REFUSE)
75153b9c14Spho     rv = pthread_mutex_unlock(&log_func_mutex);
76153b9c14Spho     assert(rv == 0);
77153b9c14Spho #endif
78153b9c14Spho }
79153b9c14Spho 
80153b9c14Spho void
fuse_log(enum fuse_log_level level,const char * fmt,...)81153b9c14Spho fuse_log(enum fuse_log_level level, const char *fmt, ...) {
82153b9c14Spho     va_list ap;
83153b9c14Spho #if defined(MULTITHREADED_REFUSE)
84153b9c14Spho     /* What we really need here is merely a memory barrier, but
85153b9c14Spho      * locking a mutex is the easiest way to achieve that. */
86153b9c14Spho     int rv;
87153b9c14Spho 
88153b9c14Spho     rv = pthread_mutex_lock(&log_func_mutex);
89153b9c14Spho     assert(rv == 0);
90153b9c14Spho #endif
91153b9c14Spho 
92153b9c14Spho     va_start(ap, fmt);
93153b9c14Spho     log_func(level, fmt, ap);
94153b9c14Spho     va_end(ap);
95153b9c14Spho 
96153b9c14Spho #if defined(MULTITHREADED_REFUSE)
97153b9c14Spho     rv = pthread_mutex_unlock(&log_func_mutex);
98153b9c14Spho     assert(rv == 0);
99153b9c14Spho #endif
100153b9c14Spho }
101