netif_utils: correctly close fd on read error
[project/ustp.git] / log.h
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3 Copyright (c) 2011 Factor-SPE
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 The full GNU General Public License is included in this distribution in the
20 file called LICENSE.
21
22 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 Authors: Vitalii Demianets <dvitasgs@gmail.com>
24
25 ******************************************************************************/
26
27 #ifndef LOG_H
28 #define LOG_H
29
30 #include <stdio.h>
31 #include <stdarg.h>
32
33 #define LOG_LEVEL_NONE 0
34 #define LOG_LEVEL_ERROR 1
35 #define LOG_LEVEL_INFO 2
36 #define LOG_LEVEL_DEBUG 3
37 #define LOG_LEVEL_STATE_MACHINE_TRANSITION 4
38
39 #ifdef DEBUG
40 #define LOG_LEVEL_MAX 100
41 #else
42 #define LOG_LEVEL_MAX LOG_LEVEL_INFO
43 #endif
44
45 #define LOG_LEVEL_DEFAULT LOG_LEVEL_ERROR
46
47 extern void Dprintf(int level, const char *fmt, ...);
48 extern int log_level;
49
50 #define PRINT(_level, _fmt, _args...) \
51 ({ \
52 if ((_level) <= LOG_LEVEL_MAX) \
53 Dprintf(_level, _fmt, ##_args); \
54 })
55
56 #define TSTM(x, y, _fmt, _args...) \
57 do if(!(x)) \
58 { \
59 PRINT(LOG_LEVEL_ERROR, "Error in %s at %s:%d verifying %s. " _fmt, \
60 __PRETTY_FUNCTION__, __FILE__, __LINE__, #x, ##_args); \
61 return y; \
62 } while (0)
63
64 #define TST(x, y) TSTM(x, y, "")
65
66 #define LOG(_fmt, _args...) \
67 PRINT(LOG_LEVEL_DEBUG, "%s: " _fmt, __PRETTY_FUNCTION__, ##_args)
68
69 #define INFO(_fmt, _args...) \
70 PRINT(LOG_LEVEL_INFO, "%s: " _fmt, __PRETTY_FUNCTION__, ##_args)
71
72 #define ERROR(_fmt, _args...) \
73 PRINT(LOG_LEVEL_ERROR, "%s: " _fmt, __PRETTY_FUNCTION__, ##_args)
74
75 static inline void dump_hex(void *b, int l)
76 {
77 unsigned char *buf = b;
78 char logbuf[80];
79 int i, j;
80 for (i = 0; i < l; i += 16) {
81 for (j = 0; j < 16 && i + j < l; ++j)
82 sprintf(logbuf + j * 3, " %02x", buf[i + j]);
83 PRINT(LOG_LEVEL_INFO, "%s", logbuf);
84 }
85 PRINT(LOG_LEVEL_INFO, "\n");
86 }
87
88 #endif /* LOG_H */