OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can | |
4 * be found in the LICENSE file. | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 #include <stdarg.h> | |
10 | |
11 #include "debug_conn/debug_util.h" | |
12 #include "native_client/src/include/portability_string.h" | |
13 | |
14 // | |
15 // In the short term, set |debug_file_enable| to true and then write output | |
16 // to the FILE* |debug_fp|. It's not pretty, but it's extremely helpful | |
17 // to have a persistent log of the most recent run when debugging | |
18 // the interaction between the DebugServer (sel_ldr.exe) and the code | |
19 // that is speaking RSP. Also, it's easy to change it to false but not | |
20 // remove the code until we are more confident of RSP and have better testing | |
21 // for it. | |
22 // | |
23 static bool debug_file_enable = true; | |
24 static FILE* debug_fp = NULL; | |
25 | |
26 int debug_get_tokens(const char *in, char delim, char *out[], int max) { | |
27 char *str =STRDUP(in); | |
28 char *start= str; | |
29 char *word = str; | |
30 int cnt = 0; | |
31 | |
32 for (;*str; str++) { | |
33 if (*str == delim) { | |
34 | |
35 // Make this null, so we can copy it | |
36 *str = 0; | |
37 | |
38 // Add it to the array; | |
39 if (cnt < max) | |
40 out[cnt++] = STRDUP(word); | |
41 | |
42 // Start scanning after the delim | |
43 str++; | |
44 word = str; | |
45 } | |
46 } | |
47 | |
48 if (*word) | |
49 if (cnt < max) | |
50 out[cnt++] = STRDUP(word); | |
51 | |
52 free(start); | |
53 return cnt; | |
54 } | |
55 | |
56 void debug_free_tokens(char *strings[], int max) { | |
57 int cnt = 0; | |
58 for (cnt = 0; cnt < max; cnt++) { | |
59 if (strings[cnt]) { | |
60 free(strings[cnt]); | |
61 strings[cnt] = 0; | |
62 } | |
63 } | |
64 } | |
65 | |
66 int debug_nibble_to_int(char ch) { | |
67 if ((ch >= 'a') && (ch <= 'f')) | |
68 return (ch - 'a' + 10); | |
69 if ((ch >= '0') && (ch <= '9')) | |
70 return (ch - '0'); | |
71 if ((ch >= 'A') && (ch <= 'F')) | |
72 return (ch - 'A' + 10); | |
73 | |
74 return (-1); | |
75 } | |
76 | |
77 char debug_int_to_nibble(int nibble) { | |
78 nibble &= 0xF; | |
79 | |
80 if (nibble < 10) | |
81 return '0' + nibble; | |
82 | |
83 return 'a' + (nibble - 10); | |
84 } | |
85 | |
86 enum { | |
87 DPL_INFO = 0, | |
88 DPL_WARN = 1, | |
89 DPL_ERROR= 2, | |
90 DPL_COUNT | |
91 }; | |
92 | |
93 static const char *s_ErrStrs[DPL_COUNT] = { | |
94 "INFO", "WARN", "!ERR" | |
95 }; | |
96 | |
97 void debug_printf(int level, const char *format, va_list args) { | |
98 char buffer[4096]; | |
99 vsnprintf(buffer, sizeof(buffer), format, args); | |
100 printf("[%s] %s", s_ErrStrs[level], buffer); | |
101 if (debug_file_enable) { | |
102 // |debug_fp| is a static variable initialized to NULL. The first time | |
103 // we try to use it we need to initialize it. | |
104 if (!debug_fp) { | |
105 debug_fp = fopen("c:\\src\\debug.txt", "w"); | |
106 } | |
107 if (debug_fp) { | |
108 // If message starts with RX or TX then don't prepend error level. | |
109 // This makes log easy to read since we can spot RX/TX messages | |
110 // which are the raw transmit/receive, and then see what other | |
111 // logged data is associated with those messages. | |
112 if ((buffer[0] == 'R' || buffer[0] == 'T') && buffer[1] == 'X') { | |
113 fprintf(debug_fp, "%s", buffer); | |
114 } else { | |
115 fprintf(debug_fp, "[%s] %s", s_ErrStrs[level], buffer); | |
116 } | |
117 fflush(debug_fp); | |
118 } | |
119 } | |
120 } | |
121 | |
122 | |
123 void debug_log_info(const char *format, ...) { | |
124 va_list args; | |
125 va_start( args, format ); | |
126 | |
127 debug_printf(DPL_INFO, format, args); | |
128 } | |
129 | |
130 void debug_log_warning(const char *format, ...) { | |
131 va_list args; | |
132 va_start( args, format ); | |
133 | |
134 debug_printf(DPL_WARN, format, args); | |
135 } | |
136 | |
137 void debug_log_error(const char *format, ...) { | |
138 va_list args; | |
139 va_start( args, format ); | |
140 | |
141 | |
142 | |
143 debug_printf(DPL_ERROR, format, args); | |
144 } | |
145 | |
OLD | NEW |