This repository has been archived by the owner on Dec 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
debug.axi
186 lines (163 loc) · 4.45 KB
/
debug.axi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
program_name='debug'
#if_not_defined __NCL_LIB_DEBUG
#define __NCL_LIB_DEBUG
include 'io'
define_constant
char DEBUG_OFF = 0 // Available debug verbosity levels
char DEBUG_ERROR = 1
char DEBUG_WARN = 2
char DEBUG_INFO = 3
char DEBUG_DEBUG = 4
char DEBUG_MAX_LEVEL = 4
char DEBUG_LEVEL_STRINGS[5][16] = {
'Off',
'Error',
'Warn',
'Info',
'Debug'
}
define_variable
persistent char debug_level // Current system debug level
/**
* Returns a string representing the debug level passed.
*
* @param x an char specifying the debug level
* @return a string representing the level
*/
define_function char[5] debug_get_level_string(char x)
{
return DEBUG_LEVEL_STRINGS[x + 1]
}
/**
* Gets a numerical debug level based on it's equivalent string representation.
*
* @param x a character array containing the string to parse
* @return a character containing the numerical debug level represented
* by the content of x
*/
define_function char debug_get_level_from_string(char x[]) {
stack_var char lvl;
if (length_string(x) == 1) {
lvl = atoi(x);
if (lvl > 4) {
lvl = DEBUG_OFF;
}
}
for (lvl = length_array(DEBUG_LEVEL_STRINGS); lvl; lvl--) {
if (lower_string(x) == lower_string(DEBUG_LEVEL_STRINGS[lvl])) {
lvl = lvl - 1;
break;
}
}
return lvl;
}
/**
* Sets the current system debugging level for controlling debug message
* verbosity.
*
* @param x a char specifying the debug level to set
*/
define_function debug_set_level(char x)
{
if (x >= DEBUG_OFF && x <= DEBUG_MAX_LEVEL) {
println("'Debug level set to ', debug_get_level_string(x)")
debug_level = x
} else {
debug_msg(DEBUG_WARN, "'Invalid debug level, defaulting to ',
debug_get_level_string(DEBUG_ERROR)")
debug_set_level(DEBUG_ERROR)
}
}
/**
* Voices a debug message if required by the current debug level. All system
* messages should pass through here.
*
* If the ability to dump to a file or netorked logging service is required it
* can be added here.
*
* @param msg_level a char specifying the debug level of the message
* @param msg a string containing the debug message
*/
define_function debug_msg(char msg_level, char msg[])
{
stack_var long i,l;
stack_var char c;
stack_var char out[255];
stack_var char in[255];
if (msg_level < DEBUG_ERROR || msg_level > DEBUG_MAX_LEVEL) {
debug_msg(DEBUG_ERROR, "'invalid debug level specified - ', msg")
return
}
if (msg_level <= debug_level) {
if (FIND_STRING(msg, "$00", 1) > 0){
in = msg
out = ""
l = LENGTH_STRING(in)
for (i = 0; i < l; i++){
c = GET_BUFFER_CHAR(in)
if(c == "$00"){
out = "out,'$00'"
}else{
out = "out,c"
}
}
println("upper_string(debug_get_level_string(msg_level)),': ', out")
}else{
println("upper_string(debug_get_level_string(msg_level)),': ', msg")
}
}
}
/**
* Prints a debug message forced to hex - this avoids situations where a hex
* value is a valid ascii character
*
* @param msg_level a char specifying the debug level of the message
* @param msg a string containing the debug message to be printed as hex
*/
define_function debug_hex(char msg_level, char msg[])
{
stack_var long i,l;
stack_var char c;
stack_var char out[255];
stack_var char in[255];
if (msg_level < DEBUG_ERROR || msg_level > DEBUG_MAX_LEVEL) {
debug_msg(DEBUG_ERROR, "'invalid debug level specified - ', msg")
return
}
if (msg_level <= debug_level) {
in = msg
out = ""
l = LENGTH_STRING(in)
for (i = 0; i < l; i++){
c = GET_BUFFER_CHAR(in)
out = "out, '$', itohex(c),','"
}
println("upper_string(debug_get_level_string(msg_level)),': ',out")
}
}
/**
* Prints a debug message forced to decimal
*
* @param msg_level a char specifying the debug level of the message
* @param msg a string containing the debug message to be printed as decimal
*/
define_function debug_dec(char msg_level, char msg[])
{
stack_var long i,l;
stack_var char c;
stack_var char in[255];
if (msg_level < DEBUG_ERROR || msg_level > DEBUG_MAX_LEVEL) {
debug_msg(DEBUG_ERROR, "'invalid debug level specified - ', msg")
return
}
if (msg_level <= debug_level) {
in = msg
l = LENGTH_STRING(in)
println("'message length: ',itoa(l)")
for (i = 1; i <= l; i++){
c = GET_BUFFER_CHAR(in)
println("'[',itoa(i),'] ',itoa(c)")
}
}
}
#end_if