|
1 | | -/* |
2 | | - * Generic debug and trace macro. Print to 'stdout' if 'trace_level' |
3 | | - * is sufficiently high. And do it in colours. |
4 | | - */ |
5 | | -#include <stdio.h> |
6 | | -#include <string.h> |
7 | | - |
8 | | -#include "trace.h" |
9 | | -#include "version.h" |
10 | | - |
11 | | -static BOOL show_version = FALSE; |
12 | | -static BOOL show_winusb = FALSE; |
13 | | -static CONSOLE_SCREEN_BUFFER_INFO console_info; |
14 | | -static HANDLE console_hnd = INVALID_HANDLE_VALUE; |
15 | | -static FILE *console_file = NULL; |
16 | | -static CRITICAL_SECTION console_cs; |
17 | | -static void set_color (unsigned short col); |
18 | | - |
19 | | -static void trace_exit (void) |
20 | | -{ |
21 | | - set_color (0); |
22 | | - console_hnd = INVALID_HANDLE_VALUE; |
23 | | - console_file = NULL; |
24 | | - DeleteCriticalSection (&console_cs); |
25 | | -} |
26 | | - |
27 | | -static int trace_init (void) |
28 | | -{ |
29 | | - char *env = getenv ("RTLSDR_TRACE"); |
30 | | - char *winusb = env ? strstr (env, ",winusb") : NULL; |
31 | | - int rc = 0; |
32 | | - |
33 | | - InitializeCriticalSection (&console_cs); |
34 | | - console_hnd = trace_file (FALSE); |
35 | | - atexit (trace_exit); |
36 | | - |
37 | | - /* Supported syntax: "level[,winsub]" |
38 | | - */ |
39 | | - if (env) |
40 | | - { |
41 | | - if (winusb >= env + 1) |
42 | | - { |
43 | | - show_winusb = TRUE; |
44 | | - *winusb = '\0'; |
45 | | - } |
46 | | - rc = atoi (env); |
47 | | - if (rc > 0) |
48 | | - show_version = TRUE; |
49 | | - } |
50 | | - return (rc); |
51 | | -} |
52 | | - |
53 | | -int trace_level (void) |
54 | | -{ |
55 | | - static int level = -1; |
56 | | - |
57 | | - if (level == -1) |
58 | | - level = trace_init(); |
59 | | - return (level); |
60 | | -} |
61 | | - |
62 | | -HANDLE trace_file (BOOL use_stderr) |
63 | | -{ |
64 | | - console_file = (use_stderr ? stderr : stdout); |
65 | | - console_hnd = GetStdHandle (use_stderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE); |
66 | | - GetConsoleScreenBufferInfo (console_hnd, &console_info); |
67 | | - return (console_hnd); |
68 | | -} |
69 | | - |
70 | | -static void set_color (unsigned short col) |
71 | | -{ |
72 | | - fflush (console_file); |
73 | | - |
74 | | - if (col == 0) |
75 | | - SetConsoleTextAttribute (console_hnd, console_info.wAttributes); |
76 | | - else SetConsoleTextAttribute (console_hnd, (console_info.wAttributes & ~7) | col); |
77 | | -} |
78 | | - |
79 | | -/** |
80 | | - * Return err-number and string for 'err'. |
81 | | - * Only use this with `GetLastError()`. |
82 | | - */ |
83 | | -const char *trace_strerror (DWORD err) |
84 | | -{ |
85 | | - static char buf [512+20]; |
86 | | - char err_buf [512], *p; |
87 | | - |
88 | | - if (err == ERROR_SUCCESS) |
89 | | - strcpy (err_buf, "No error."); |
90 | | - else |
91 | | - if (!FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, |
92 | | - LANG_NEUTRAL, err_buf, sizeof(err_buf)-1, NULL)) |
93 | | - strcpy (err_buf, "Unknown error."); |
94 | | - |
95 | | - snprintf (buf, sizeof(buf), "%lu: %s", (u_long)err, err_buf); |
96 | | - p = strchr (buf, '\0'); |
97 | | - if (p[-2] == '\r') |
98 | | - p[-2] = '\0'; |
99 | | - return (buf); |
100 | | -} |
101 | | - |
102 | | -/** |
103 | | - * Strip drive-letter, directory and suffix from a filename. |
104 | | - */ |
105 | | -static const char *basename (const char *fname) |
106 | | -{ |
107 | | - const char *base = fname; |
108 | | - |
109 | | - if (fname && *fname) |
110 | | - { |
111 | | - if (fname[0] && fname[1] == ':') |
112 | | - { |
113 | | - fname += 2; |
114 | | - base = fname; |
115 | | - } |
116 | | - while (*fname) |
117 | | - { |
118 | | - if (*fname == '\\' || *fname == '/') |
119 | | - base = fname + 1; |
120 | | - fname++; |
121 | | - } |
122 | | - } |
123 | | - return (base); |
124 | | -} |
125 | | - |
126 | | -void trace_printf (const char *fname, unsigned line, const char *fmt, ...) |
127 | | -{ |
128 | | - va_list args; |
129 | | - |
130 | | - EnterCriticalSection (&console_cs); |
131 | | - |
132 | | - set_color (FOREGROUND_INTENSITY | 3); /* bright cyan */ |
133 | | - fprintf (console_file, "%s(%u): ", basename(fname), line); |
134 | | - |
135 | | - if (show_version) /* show the version-info on 1st call only */ |
136 | | - { |
137 | | - fprintf (console_file, "Version \"%d.%d.%d.%d\". Compiled \"%s\". ", |
138 | | - RTLSDR_MAJOR, RTLSDR_MINOR, RTLSDR_MICRO, RTLSDR_NANO, __DATE__); |
139 | | - show_version = FALSE; |
140 | | - } |
141 | | - |
142 | | - va_start (args, fmt); |
143 | | - |
144 | | - if (*fmt == '!') |
145 | | - { |
146 | | - set_color (FOREGROUND_INTENSITY | 4); /* bright red */ |
147 | | - fmt++; |
148 | | - } |
149 | | - else if (*fmt == '|') |
150 | | - { |
151 | | - set_color (FOREGROUND_INTENSITY | 2); /* bright green */ |
152 | | - fmt++; |
153 | | - } |
154 | | - else |
155 | | - set_color (FOREGROUND_INTENSITY | 7); /* bright white */ |
156 | | - |
157 | | - vfprintf (console_file, fmt, args); |
158 | | - va_end (args); |
159 | | - set_color (0); |
160 | | - LeaveCriticalSection (&console_cs); |
161 | | -} |
162 | | - |
163 | | -void trace_winusb (const char *file, unsigned line, const char *func, DWORD win_err) |
164 | | -{ |
165 | | - int level = trace_level(); |
166 | | - |
167 | | - if (level <= 0) |
168 | | - return; |
169 | | - |
170 | | - if (level >= 1 && win_err != ERROR_SUCCESS) |
171 | | - trace_printf (file, line, "!%s() failed with %s\n", func, trace_strerror(win_err)); |
172 | | - |
173 | | - else if ((level >= 2 || show_winusb) && win_err == ERROR_SUCCESS) |
174 | | - trace_printf (file, line, "|%s(), OK.\n", func); |
175 | | -} |
| 1 | +/* |
| 2 | + * Generic debug and trace macro. Print to 'stdout' if 'trace_level' |
| 3 | + * is sufficiently high. And do it in colours. |
| 4 | + */ |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include "trace.h" |
| 9 | +#include "version.h" |
| 10 | + |
| 11 | +static BOOL show_version = FALSE; |
| 12 | +static BOOL show_winusb = FALSE; |
| 13 | +static CONSOLE_SCREEN_BUFFER_INFO console_info; |
| 14 | +static HANDLE console_hnd = INVALID_HANDLE_VALUE; |
| 15 | +static FILE *console_file = NULL; |
| 16 | +static CRITICAL_SECTION console_cs; |
| 17 | +static void set_color (unsigned short col); |
| 18 | + |
| 19 | +static void trace_exit (void) |
| 20 | +{ |
| 21 | + set_color (0); |
| 22 | + console_hnd = INVALID_HANDLE_VALUE; |
| 23 | + console_file = NULL; |
| 24 | + DeleteCriticalSection (&console_cs); |
| 25 | +} |
| 26 | + |
| 27 | +static int trace_init (void) |
| 28 | +{ |
| 29 | + char *env = getenv ("RTLSDR_TRACE"); |
| 30 | + char *winusb = env ? strstr (env, ",winusb") : NULL; |
| 31 | + int rc = 0; |
| 32 | + |
| 33 | + InitializeCriticalSection (&console_cs); |
| 34 | + console_hnd = trace_file (FALSE); |
| 35 | + atexit (trace_exit); |
| 36 | + |
| 37 | + /* Supported syntax: "level[,winsub]" |
| 38 | + */ |
| 39 | + if (env) |
| 40 | + { |
| 41 | + if (winusb >= env + 1) |
| 42 | + { |
| 43 | + show_winusb = TRUE; |
| 44 | + *winusb = '\0'; |
| 45 | + } |
| 46 | + rc = atoi (env); |
| 47 | + if (rc > 0) |
| 48 | + show_version = TRUE; |
| 49 | + } |
| 50 | + return (rc); |
| 51 | +} |
| 52 | + |
| 53 | +int trace_level (void) |
| 54 | +{ |
| 55 | + static int level = -1; |
| 56 | + |
| 57 | + if (level == -1) |
| 58 | + level = trace_init(); |
| 59 | + return (level); |
| 60 | +} |
| 61 | + |
| 62 | +HANDLE trace_file (BOOL use_stderr) |
| 63 | +{ |
| 64 | + console_file = (use_stderr ? stderr : stdout); |
| 65 | + console_hnd = GetStdHandle (use_stderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE); |
| 66 | + GetConsoleScreenBufferInfo (console_hnd, &console_info); |
| 67 | + return (console_hnd); |
| 68 | +} |
| 69 | + |
| 70 | +static void set_color (unsigned short col) |
| 71 | +{ |
| 72 | + fflush (console_file); |
| 73 | + |
| 74 | + if (col == 0) |
| 75 | + SetConsoleTextAttribute (console_hnd, console_info.wAttributes); |
| 76 | + else SetConsoleTextAttribute (console_hnd, (console_info.wAttributes & ~7) | col); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Return err-number and string for 'err'. |
| 81 | + * Only use this with `GetLastError()`. |
| 82 | + */ |
| 83 | +const char *trace_strerror (DWORD err) |
| 84 | +{ |
| 85 | + static char buf [512+20]; |
| 86 | + char err_buf [512], *p; |
| 87 | + |
| 88 | + if (err == ERROR_SUCCESS) |
| 89 | + strcpy (err_buf, "No error."); |
| 90 | + else |
| 91 | + if (!FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, |
| 92 | + LANG_NEUTRAL, err_buf, sizeof(err_buf)-1, NULL)) |
| 93 | + strcpy (err_buf, "Unknown error."); |
| 94 | + |
| 95 | + snprintf (buf, sizeof(buf), "%lu: %s", (u_long)err, err_buf); |
| 96 | + p = strchr (buf, '\0'); |
| 97 | + if (p[-2] == '\r') |
| 98 | + p[-2] = '\0'; |
| 99 | + return (buf); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Strip drive-letter, directory and suffix from a filename. |
| 104 | + */ |
| 105 | +static const char *basename (const char *fname) |
| 106 | +{ |
| 107 | + const char *base = fname; |
| 108 | + |
| 109 | + if (fname && *fname) |
| 110 | + { |
| 111 | + if (fname[0] && fname[1] == ':') |
| 112 | + { |
| 113 | + fname += 2; |
| 114 | + base = fname; |
| 115 | + } |
| 116 | + while (*fname) |
| 117 | + { |
| 118 | + if (*fname == '\\' || *fname == '/') |
| 119 | + base = fname + 1; |
| 120 | + fname++; |
| 121 | + } |
| 122 | + } |
| 123 | + return (base); |
| 124 | +} |
| 125 | + |
| 126 | +void trace_printf (const char *fname, unsigned line, const char *fmt, ...) |
| 127 | +{ |
| 128 | + va_list args; |
| 129 | + |
| 130 | + EnterCriticalSection (&console_cs); |
| 131 | + |
| 132 | + set_color (FOREGROUND_INTENSITY | 3); /* bright cyan */ |
| 133 | + fprintf (console_file, "%s(%u): ", basename(fname), line); |
| 134 | + |
| 135 | + if (show_version) /* show the version-info on 1st call only */ |
| 136 | + { |
| 137 | + fprintf (console_file, "Version \"%d.%d.%d.%d\". Compiled \"%s\". ", |
| 138 | + RTLSDR_MAJOR, RTLSDR_MINOR, RTLSDR_MICRO, RTLSDR_NANO, __DATE__); |
| 139 | + show_version = FALSE; |
| 140 | + } |
| 141 | + |
| 142 | + va_start (args, fmt); |
| 143 | + |
| 144 | + if (*fmt == '!') |
| 145 | + { |
| 146 | + set_color (FOREGROUND_INTENSITY | 4); /* bright red */ |
| 147 | + fmt++; |
| 148 | + } |
| 149 | + else if (*fmt == '|') |
| 150 | + { |
| 151 | + set_color (FOREGROUND_INTENSITY | 2); /* bright green */ |
| 152 | + fmt++; |
| 153 | + } |
| 154 | + else |
| 155 | + set_color (FOREGROUND_INTENSITY | 7); /* bright white */ |
| 156 | + |
| 157 | + vfprintf (console_file, fmt, args); |
| 158 | + va_end (args); |
| 159 | + set_color (0); |
| 160 | + LeaveCriticalSection (&console_cs); |
| 161 | +} |
| 162 | + |
| 163 | +void trace_winusb (const char *file, unsigned line, const char *func, DWORD win_err) |
| 164 | +{ |
| 165 | + int level = trace_level(); |
| 166 | + |
| 167 | + if (level <= 0) |
| 168 | + return; |
| 169 | + |
| 170 | + if (level >= 1 && win_err != ERROR_SUCCESS) |
| 171 | + trace_printf (file, line, "!%s() failed with %s\n", func, trace_strerror(win_err)); |
| 172 | + |
| 173 | + else if ((level >= 2 || show_winusb) && win_err == ERROR_SUCCESS) |
| 174 | + trace_printf (file, line, "|%s(), OK.\n", func); |
| 175 | +} |
0 commit comments