1 | /* Error & Logging functions
|
---|
2 |
|
---|
3 | Copyright (C) 2010 Travis Robinson. <libusbdotnet@gmail.com>
|
---|
4 | website: http://sourceforge.net/projects/libusb-win32
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify it
|
---|
7 | under the terms of the GNU (LGPL) General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
---|
13 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU (LGPL) General Public
|
---|
14 | License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU (LGPL) General Public License
|
---|
17 | along with this program; if not, please visit www.gnu.org.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #ifndef __ERROR_H__
|
---|
21 | #define __ERROR_H__
|
---|
22 |
|
---|
23 | #include <stdarg.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | enum USB_LOG_LEVEL
|
---|
27 | {
|
---|
28 | LOG_OFF,
|
---|
29 | LOG_ERROR,
|
---|
30 | LOG_WARNING,
|
---|
31 | LOG_INFO,
|
---|
32 | LOG_DEBUG,
|
---|
33 |
|
---|
34 | LOG_LEVEL_MAX,
|
---|
35 | LOG_LEVEL_MASK=0xff,
|
---|
36 | LOG_RAW=0x100
|
---|
37 |
|
---|
38 | };
|
---|
39 |
|
---|
40 | /* Connection timed out */
|
---|
41 | #define ETRANSFER_TIMEDOUT 116
|
---|
42 |
|
---|
43 | #define LOGBUF_SIZE 512
|
---|
44 |
|
---|
45 | // TARGETTYPEs
|
---|
46 | #define PROGRAMconsole 0
|
---|
47 | #define PROGRAMwindows 1
|
---|
48 | #define DYNLINK 2
|
---|
49 | #define DRIVER 3
|
---|
50 |
|
---|
51 | // default TARGETTYPE
|
---|
52 | #ifndef TARGETTYPE
|
---|
53 | #define TARGETTYPE PROGRAMconsole
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #define IS_DRIVER (TARGETTYPE==DRIVER)
|
---|
57 | #define IS_CONSOLE_APP (TARGETTYPE==PROGRAMconsole)
|
---|
58 | #define IS_WINDOW_APP (TARGETTYPE==PROGRAMwindows)
|
---|
59 | #define IS_APP (IS_CONSOLE_APP || IS_WINDOW_APP)
|
---|
60 | #define IS_DLL (TARGETTYPE==DYNLINK)
|
---|
61 |
|
---|
62 | // NOTE: LOG_OUTPUT_TYPEs can be combined
|
---|
63 | // writes log messages to standard error output
|
---|
64 | #define LOG_OUTPUT_TYPE_STDERR 0x001
|
---|
65 |
|
---|
66 | // writes log messages to Win32 OutputDebugString (DbgPrint for drivers)
|
---|
67 | #define LOG_OUTPUT_TYPE_DEBUGWINDOW 0x0002
|
---|
68 | #define LOG_OUTPUT_TYPE_DBGPRINT 0x0002
|
---|
69 |
|
---|
70 | // displays error log messages to a messagebox (not recommended)
|
---|
71 | #define LOG_OUTPUT_TYPE_MSGBOX 0x0004
|
---|
72 |
|
---|
73 | // writes log messages to Kernel-mode DbgPrint
|
---|
74 |
|
---|
75 | // writes log messages directly to a file
|
---|
76 | #define LOG_OUTPUT_TYPE_FILE 0x0010
|
---|
77 |
|
---|
78 | // strips all log messages except errors
|
---|
79 | #define LOG_OUTPUT_TYPE_REMOVE 0x0020
|
---|
80 |
|
---|
81 | #define LOG_OUTPUT_TYPE_DEFAULT 0x0100
|
---|
82 |
|
---|
83 | // File logging is never enabled by default.
|
---|
84 | // The LOG_OUTPUT_TYPE define must be manually
|
---|
85 | // set to enable file logging.
|
---|
86 | #if !IS_DRIVER
|
---|
87 | #ifndef LOG_DIRECTORY
|
---|
88 | #define LOG_FILE_PATH LOG_APPNAME ".log"
|
---|
89 | #else
|
---|
90 | #define LOG_FILE_PATH LOG_DIRECTORY LOG_APPNAME ".log"
|
---|
91 | #endif
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | #if (IS_DRIVER) || (IS_DLL) || (IS_WINDOW_APP)
|
---|
95 | // default logging for drivers and dlls
|
---|
96 | #define DEF_LOG_OUTPUT_TYPE LOG_OUTPUT_TYPE_DEBUGWINDOW
|
---|
97 | #else
|
---|
98 | // default logging for applications and everything else
|
---|
99 | #define DEF_LOG_OUTPUT_TYPE LOG_OUTPUT_TYPE_STDERR
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #define _usb_log_do_nothing() while(0)
|
---|
103 | // Default logging output
|
---|
104 | #ifdef LOG_OUTPUT_TYPE
|
---|
105 | // all log messages (except errors) are stripped
|
---|
106 | #if (LOG_OUTPUT_TYPE & LOG_OUTPUT_TYPE_REMOVE)
|
---|
107 | #define USBMSG(format,...) _usb_log_do_nothing()
|
---|
108 | #define USBWRN(format,...) _usb_log_do_nothing()
|
---|
109 | #define USBDBG(format,...) _usb_log_do_nothing()
|
---|
110 | #define USBRAWMSG(format,...) _usb_log_do_nothing()
|
---|
111 |
|
---|
112 | #define USBMSG0(format) _usb_log_do_nothing()
|
---|
113 | #define USBWRN0(format) _usb_log_do_nothing()
|
---|
114 | #define USBDBG0(format) _usb_log_do_nothing()
|
---|
115 | #define USBRAWMSG0(format) _usb_log_do_nothing()
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | #if (LOG_OUTPUT_TYPE & LOG_OUTPUT_TYPE_DEFAULT)
|
---|
119 | #define _LOG_OUTPUT_TYPE ((LOG_OUTPUT_TYPE & 0xff)|DEF_LOG_OUTPUT_TYPE)
|
---|
120 | #else
|
---|
121 | #define _LOG_OUTPUT_TYPE (LOG_OUTPUT_TYPE)
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | #else
|
---|
125 | // if the LOG_OUTPUT_TYPE has not been manually set use
|
---|
126 | // the as defaults.
|
---|
127 | #define _LOG_OUTPUT_TYPE DEF_LOG_OUTPUT_TYPE
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | // always keep error messages
|
---|
131 | #define USBERR(format,...) usb_err(__FUNCTION__,format,__VA_ARGS__)
|
---|
132 | #define USBERR0(format) usb_err(__FUNCTION__,"%s",format)
|
---|
133 |
|
---|
134 | // only keep debug log messages in debug builds
|
---|
135 | #if !(defined(_DEBUG) || defined(DEBUG) || defined(DBG)) && !defined(USBDBG)
|
---|
136 | #define USBDBG(format,...) _usb_log_do_nothing()
|
---|
137 | #define USBDBG0(format) _usb_log_do_nothing()
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | // if USBMSG has not been defined as empty (see above)
|
---|
141 | // then keep all the info and warning log messages
|
---|
142 | #ifndef USBMSG
|
---|
143 | #define USBMSG(format,...) usb_msg(__FUNCTION__,format,__VA_ARGS__)
|
---|
144 | #define USBWRN(format,...) usb_wrn(__FUNCTION__,format,__VA_ARGS__)
|
---|
145 | #define USBRAWMSG(format,...) usb_log(LOG_INFO|LOG_RAW,__FUNCTION__,format,__VA_ARGS__)
|
---|
146 |
|
---|
147 | #define USBMSG0(format) usb_msg(__FUNCTION__,"%s",format)
|
---|
148 | #define USBWRN0(format) usb_wrn(__FUNCTION__,"%s",format)
|
---|
149 | #define USBRAWMSG0(format) usb_log(LOG_INFO|LOG_RAW,__FUNCTION__,"%s",format)
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | // if USBDBG has not been defined as empty (see above)
|
---|
153 | // then keep all the debug log messages
|
---|
154 | #ifndef USBDBG
|
---|
155 | #define USBDBG(format,...) usb_dbg(__FUNCTION__,format,__VA_ARGS__)
|
---|
156 | #define USBDBG0(format) usb_dbg(__FUNCTION__,"%s",format)
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | typedef enum
|
---|
160 | {
|
---|
161 | USB_ERROR_TYPE_NONE = 0,
|
---|
162 | USB_ERROR_TYPE_STRING,
|
---|
163 | USB_ERROR_TYPE_ERRNO,
|
---|
164 | } usb_error_type_t;
|
---|
165 |
|
---|
166 | typedef int (*log_hander_t)(enum USB_LOG_LEVEL level, const char*,const char*,const char*, int, char*, int);
|
---|
167 |
|
---|
168 | #if (!IS_DRIVER)
|
---|
169 | const char *usb_win_error_to_string(void);
|
---|
170 | int usb_win_error_to_errno(void);
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | void usb_log_set_level(enum USB_LOG_LEVEL level);
|
---|
174 | int usb_log_get_level(void);
|
---|
175 | void usb_log_set_handler(log_hander_t log_hander);
|
---|
176 | log_hander_t usb_log_get_handler(void);
|
---|
177 |
|
---|
178 | // these are the core logging functions used by the logging macros
|
---|
179 | // (not used directly)
|
---|
180 | void usb_err (const char* function, const char* format, ...);
|
---|
181 | void usb_wrn (const char* function, const char* format, ...);
|
---|
182 | void usb_msg (const char* function, const char* format, ...);
|
---|
183 | void usb_dbg (const char* function, const char* format, ...);
|
---|
184 | void usb_log (enum USB_LOG_LEVEL level, const char* function, const char* format, ...);
|
---|
185 |
|
---|
186 | #endif /* _ERROR_H_ */
|
---|
187 |
|
---|