1 /*
2  * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef ISTGT_LOG_H
29 #define ISTGT_LOG_H
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 
34 #if !defined(__GNUC__)
35 #undef __attribute__
36 #define __attribute__(x)
37 #endif
38 
39 #ifndef ISTGT_LOG_FACILITY
40 #define ISTGT_LOG_FACILITY LOG_LOCAL7
41 #endif
42 #ifndef ISTGT_LOG_PRIORITY
43 #define ISTGT_LOG_PRIORITY LOG_NOTICE
44 #endif
45 
46 #define ISTGT_TRACE_ALL     (~0U)
47 #define ISTGT_TRACE_NONE    0U
48 #define ISTGT_TRACE_DEBUG   0x80000000U
49 #define ISTGT_TRACE_NET     0x0000000fU
50 #define ISTGT_TRACE_ISCSI   0x000000f0U
51 #define ISTGT_TRACE_SCSI    0x00000f00U
52 #define ISTGT_TRACE_LU      0x0000f000U
53 
54 #define ISTGT_LOG(...) \
55 	istgt_log(NULL, 0, NULL, __VA_ARGS__)
56 #define ISTGT_NOTICELOG(...) \
57 	istgt_noticelog(NULL, 0, NULL, __VA_ARGS__)
58 #define ISTGT_ERRLOG(...) \
59 	istgt_errlog(__FILE__, __LINE__, __func__, __VA_ARGS__)
60 #define ISTGT_WARNLOG(...)						\
61 	do {								\
62 		if (g_warn_flag != 0) {					\
63 			istgt_warnlog(__FILE__, __LINE__, __func__, __VA_ARGS__); \
64 		}							\
65 	} while (0)
66 #ifdef DEBUG
67 #define ISTGT_TRACELOG(FLAG, ...)					\
68 	do {								\
69 		if (g_trace_flag & (FLAG)) {				\
70 			istgt_tracelog((FLAG), __FILE__, __LINE__, __func__, \
71 			    __VA_ARGS__);				\
72 		}							\
73 	} while (0)
74 #define ISTGT_TRACEDUMP(FLAG, LABEL, BUF, LEN)				\
75 	do {								\
76 		if (g_trace_flag & (FLAG)) {				\
77 			istgt_trace_dump((FLAG), (LABEL), (BUF), (LEN)); \
78 		}							\
79 	} while (0)
80 #else
81 #define ISTGT_TRACELOG(FLAG, ...)
82 #define ISTGT_TRACEDUMP(FLAG, LABEL, BUF, LEN)
83 #endif /* DEBUG */
84 
85 int istgt_set_log_facility(const char *facility);
86 int istgt_set_log_priority(const char *priority);
87 void istgt_log(const char *file, const int line, const char *func, const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
88 void istgt_noticelog(const char *file, const int line, const char *func, const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
89 void istgt_tracelog(const int flag, const char *file, const int line, const char *func, const char *format, ...) __attribute__((__format__(__printf__, 5, 6)));
90 void istgt_errlog(const char *file, const int line, const char *func, const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
91 void istgt_warnlog(const char *file, const int line, const char *func, const char *format, ...) __attribute__((__format__(__printf__, 4, 5)));
92 void istgt_open_log(void);
93 void istgt_close_log(void);
94 void istgtcontrol_open_log(void);
95 void istgtcontrol_close_log(void);
96 void istgt_set_trace_flag(int flag);
97 void istgt_trace_dump(int flag, const char *label, const uint8_t *buf, size_t len);
98 
99 extern int g_trace_flag;
100 extern int g_warn_flag;
101 
102 #endif /* ISTGT_LOG_H */
103