1 /*****************************************************************************
2 * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
3 *
4 * This copy is licensed under the Apache License, Version 2.0 (the "License");
5 * You may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and limitations under the License.
11
12 ****************************************************************************/
13
14 /**
15 * @file
16 * $Author$
17 * $Revision$
18 * $Date$
19 *
20 * Interface to log facilities.
21 *
22 */
23
24 #ifndef LOG_H
25 #define LOG_H
26
27 #ifdef ENABLE_LOGGING
28
29 # include "compiler.h"
30 # include <stdarg.h>
31
32 # define LOG_ESC "\001"
33 # define LOG_ERR LOG_ESC "0"
34 # define LOG_WARN LOG_ESC "1"
35 # define LOG_NOTICE LOG_ESC "2"
36 # define LOG_INFO LOG_ESC "3"
37 # define LOG_DEBUG LOG_ESC "4"
38
39 # define err(fmt, ...) say(ctx, LOG_ERR fmt, ## __VA_ARGS__)
40 # define warn(fmt, ...) say(ctx, LOG_WARN fmt, ## __VA_ARGS__)
41 # define notice(fmt, ...) say(ctx, LOG_NOTICE fmt, ## __VA_ARGS__)
42 # define info(fmt, ...) say(ctx, LOG_INFO fmt, ## __VA_ARGS__)
43 # ifdef ENABLE_DEBUG
44 # define dbg(fmt, ...) say(ctx, LOG_DEBUG fmt, ## __VA_ARGS__)
45 # else
46 # define dbg(fmt, ...) nosay(ctx, LOG_DEBUG fmt, ## __VA_ARGS__)
47 # endif
48
49 void vsay(struct se_gto_ctx *ctx, const char *fmt, va_list args);
50
51 static inline void
52 check_printf(2, 3)
say(struct se_gto_ctx * ctx,const char * fmt,...)53 say(struct se_gto_ctx *ctx, const char *fmt, ...) {
54 va_list args;
55
56 va_start(args, fmt);
57 vsay(ctx, fmt, args);
58 va_end(args);
59 }
60
61 #else /* ifdef ENABLE_LOGGING */
62
63 # define err(fmt, ...) nosay(ctx, fmt, ## __VA_ARGS__)
64 # define warn(fmt, ...) nosay(ctx, fmt, ## __VA_ARGS__)
65 # define notice(fmt, ...) nosay(ctx, fmt, ## __VA_ARGS__)
66 # define info(fmt, ...) nosay(ctx, fmt, ## __VA_ARGS__)
67 # define dbg(fmt, ...) nosay(ctx, fmt, ## __VA_ARGS__)
68
69 #endif /* ifdef ENABLE_LOGGING */
70
71 #if 0 /* when disabling logging compilation fail on nosay() definition */
72 static inline void check_printf(2, 3)
73 nosay(struct se_gto_ctx *ctx, const char *fmt, ...) {}
74 #else
75 static inline void
nosay(struct se_gto_ctx * ctx,const char * fmt,...)76 nosay(struct se_gto_ctx *ctx, const char *fmt, ...) {}
77 #endif
78
79 void log_teardown(struct se_gto_ctx *ctx);
80
81 #endif /* ifndef LOG_H */
82