-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsp-logging.tcl
278 lines (235 loc) · 7.95 KB
/
tsp-logging.tcl
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#########################################################
# add an error for the compUnit
#
proc ::tsp::addError {compUnitDict errorMsg} {
upvar $compUnitDict compUnit
dict lappend compUnit errors "[dict get $compUnit lineNum]: $errorMsg"
}
#########################################################
# get all of the logged warnings for the compUnit
#
proc ::tsp::getErrors {compUnitDict} {
upvar $compUnitDict compUnit
return [dict get $compUnit errors]
}
#########################################################
# get all of the logged errors for the compUnit with
# proc name and filename information
#
proc ::tsp::getLoggedErrors {compUnitDict} {
upvar $compUnitDict compUnit
set errors [dict get $compUnit errors]
if {[llength $errors] == 0} {
return [list]
}
set filename [dict get $compUnit file]
set name [dict get $compUnit name]
set result [list]
foreach error $errors {
lappend result "$filename:$name - $error"
}
return $result
}
#########################################################
# add a warning for the compUnit
#
proc ::tsp::addWarning {compUnitDict warningMsg} {
upvar $compUnitDict compUnit
dict lappend compUnit warnings "[dict get $compUnit lineNum]: $warningMsg"
}
#########################################################
# get all of the current logged warnings for the compUnit
proc ::tsp::getWarnings {compUnitDict} {
upvar $compUnitDict compUnit
return [dict get $compUnit warnings]
}
#########################################################
# get all of the logged warnings for the compUnit with
# proc name and filename information
#
proc ::tsp::getLoggedWarnings {compUnitDict} {
upvar $compUnitDict compUnit
set warnings [dict get $compUnit warnings]
if {[llength $warnings] == 0} {
return [list]
}
set filename [dict get $compUnit file]
set name [dict get $compUnit name]
set result [list]
foreach warning $warnings {
lappend result "$filename:$name - $warning"
}
return $result
}
#########################################################
# add the compUnit as a known compiled proc
#
proc ::tsp::addCompiledProc {compUnitDict} {
upvar $compUnitDict compUnit
set name [dict get $compUnit name]
set returns [dict get $compUnit returns]
set argTypes [dict get $compUnit argTypes]
if {$argTypes eq "invalid"} {
# invalid was just a placeholder, make it empty list
set argTypes ""
}
set compiledReference [dict get $compUnit compiledReference]
dict set ::tsp::COMPILED_PROCS $name [list $returns $argTypes $compiledReference]
}
#########################################################
# get the names of the compile proces
#
proc ::tsp::getCompiledProcs {} {
return [dict keys $::tsp::COMPILED_PROCS]
}
#########################################################
# format file, proc name, line number
#
proc ::tsp::currentLine {compUnitDict} {
upvar $compUnitDict compUnit
set lineNum [dict get $compUnit lineNum]
append result "file: [dict get $compUnit file]"
append result " proc: [dict get $compUnit name]"
append result " line: $lineNum"
append result " text: [lindex [split [dict get $compUnit body] \n] $lineNum]"
return $result
}
#########################################################
# log all of the errors and warnings from a compilation
# last compilation has index of "_"
#
proc ::tsp::logErrorsWarnings {compUnitDict} {
upvar $compUnitDict compUnit
set errors [::tsp::getLoggedErrors compUnit]
set warnings [::tsp::getLoggedWarnings compUnit]
set filename [dict get $compUnit file]
set name [dict get $compUnit name]
set logDict [dict create filename $filename errors $errors warnings $warnings]
dict set ::tsp::COMPILER_LOG $name $logDict
dict set ::tsp::COMPILER_LOG _ $logDict
if {$::tsp::DEBUG_DIR eq ""} {
return
}
set path [file join $::tsp::DEBUG_DIR $name.log]
set fd [open $path w]
::tsp::printLog $fd $name
close $fd
}
#########################################################
# log the compilable source, only if debug directory is set
#
proc ::tsp::logCompilable {compUnitDict compilable} {
if {$::tsp::DEBUG_DIR eq ""} {
return
}
upvar $compUnitDict compUnit
set filename [dict get $compUnit file]
set name [dict get $compUnit name]
set path [file join $::tsp::DEBUG_DIR $name.$::tsp::PLATFORM]
set fd [open $path w]
puts $fd $compilable
close $fd
}
#########################################################
# print errors and warnings to a filehandle
# optional filehandle, defaults to stderr
# optional proc name pattern, defaults to *
#
proc ::tsp::printLog {{fd stderr} {patt *}} {
puts $fd [::tsp::log $patt]
}
#########################################################
# format errors and warnings
# optional filehandle, defaults to stderr
# optional proc name pattern, defaults to *
#
proc ::tsp::log {{patt *}} {
set result ""
set keys [lsort [dict keys $::tsp::COMPILER_LOG]]
foreach key $keys {
if {[string match $patt $key]} {
append result "$key (file: [dict get $::tsp::COMPILER_LOG $key filename])---------------------------------------------------------" \n
append result " ERRORS --------------------------------------------------" \n
foreach err [dict get $::tsp::COMPILER_LOG $key errors] {
append result " $err" \n
}
append result " WARNINGS ------------------------------------------------" \n
foreach warn [dict get $::tsp::COMPILER_LOG $key warnings] {
append result " $warn" \n
}
}
}
return $result
}
#########################################################
# get last compile
#
proc ::tsp::lastLog {} {
return [::tsp::log _]
}
#########################################################
# make a tmp directory, partially borrowed from wiki.tcl.tk/772
#
proc ::tsp::mktmpdir {} {
if {[catch {set tmp $::env(java.io.tmpdir)}] && \
[catch {set tmp $::env(TMP)}] && \
[catch {set tmp $::env(TEMP)}]} {
if {$::tcl_platform(platform) eq "windows"} {
set tmp C:/temp
} else {
set tmp /tmp
}
}
set chars abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
for {set i 0} {$i < 10} {incr i} {
set path $tmp/tcl_
for {set j 0} {$j < 10} {incr j} {
append path [string index $chars [expr {int(rand() * 62)}]]
}
if {![file exists $path]} {
file mkdir $path
return $path
}
}
error "failed to find an unused temporary directory name"
}
#########################################################
# set a directory for debug
#
proc ::tsp::debug {{dir ""}} {
if {$::tsp::DEBUG_DIR ne ""} {
error "debug directory already set as: $::tsp::DEBUG_DIR"
}
if {$dir eq ""} {
set dir [::tsp::mktmpdir]
} else {
if {! [file isdirectory $dir] || ! [file writable $dir]} {
error "debug pathname \"$dir\" not writable, or is not a directory"
}
}
set ::tsp::DEBUG_DIR $dir
set ::tsp::TRACE_FD [open $dir/traces.[clock seconds] w]
return $dir
}
#########################################################
#
# get an abbreviated stack trace, for internal errors
#
proc ::tsp::error_stacktrace {} {
set stack "Stack trace:\n"
set indent 1
for {set i 1} {$i < [info level]} {incr i} {
set lvl [info level -$i]
set pname [lindex $lvl 0]
append stack [string repeat " " $indent]$pname
incr indent
foreach value [lrange $lvl 1 end] arg [info args $pname] {
if {$value eq ""} {
info default $pname $arg value
}
append stack " $arg='[string range $value 0 20][expr {[string length $value] > 20 ? " ..." : ""}]'"
}
append stack \n
}
return $stack
}