1 /* Simple Plugin API
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef SPA_LOG_IMPL_H
26 #define SPA_LOG_IMPL_H
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #include <stdio.h>
33
34 #include <spa/utils/type.h>
35 #include <spa/support/log.h>
36
spa_log_impl_logv(void * object,enum spa_log_level level,const char * file,int line,const char * func,const char * fmt,va_list args)37 static inline SPA_PRINTF_FUNC(6, 0) void spa_log_impl_logv(void *object,
38 enum spa_log_level level,
39 const char *file,
40 int line,
41 const char *func,
42 const char *fmt,
43 va_list args)
44 {
45 char text[512], location[1024];
46 static const char *levels[] = { "-", "E", "W", "I", "D", "T" };
47
48 vsnprintf(text, sizeof(text), fmt, args);
49 snprintf(location, sizeof(location), "[%s][%s:%i %s()] %s\n",
50 levels[level], strrchr(file, '/') + 1, line, func, text);
51 fputs(location, stderr);
52 }
spa_log_impl_log(void * object,enum spa_log_level level,const char * file,int line,const char * func,const char * fmt,...)53 static inline SPA_PRINTF_FUNC(6,7) void spa_log_impl_log(void *object,
54 enum spa_log_level level,
55 const char *file,
56 int line,
57 const char *func,
58 const char *fmt, ...)
59 {
60 va_list args;
61 va_start(args, fmt);
62 spa_log_impl_logv(object, level, file, line, func, fmt, args);
63 va_end(args);
64 }
65
66 #define SPA_LOG_IMPL_DEFINE(name) \
67 struct { \
68 struct spa_log log; \
69 struct spa_log_methods methods; \
70 } name
71
72 #define SPA_LOG_IMPL_INIT(name) \
73 { { { SPA_TYPE_INTERFACE_Log, SPA_VERSION_LOG, \
74 SPA_CALLBACKS_INIT(&name.methods, &name) }, \
75 SPA_LOG_LEVEL_INFO, }, \
76 { SPA_VERSION_LOG_METHODS, \
77 spa_log_impl_log, \
78 spa_log_impl_logv,} }
79
80 #define SPA_LOG_IMPL(name) \
81 SPA_LOG_IMPL_DEFINE(name) = SPA_LOG_IMPL_INIT(name)
82
83 #ifdef __cplusplus
84 } /* extern "C" */
85 #endif
86 #endif /* SPA_LOG_IMPL_H */
87