Skip to content

Commit 94508ee

Browse files
committed
FEAT: introducing system log function for common system trace output
Also using it for now with ZIP codec, instead of using local log functions.
1 parent e3e33bb commit 94508ee

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/mezz/codec-zip.r

+20-33
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,14 @@ register-codec [
2121
cmp-size:
2222
unc-size: 0
2323

24-
log-info: func[msg][
25-
if block? msg [msg: reform msg]
26-
print rejoin [" ^[[1;33m[ZIP] ^[[36m" msg "^[[0m"]
27-
]
28-
log-more: func[msg][
29-
if block? msg [msg: reform msg]
30-
print rejoin [" ^[[33m[ZIP] ^[[0;36m" msg "^[[0m"]
31-
]
32-
log-debug: func[msg][
33-
if block? msg [msg: reform msg]
34-
print rejoin [" ^[[33m[ZIP] ^[[0;32m" msg "^[[0m"]
35-
]
36-
3724
decompress*: func [
3825
data [binary! ]
3926
validate [logic!]
4027
/local output crc2
4128
][
4229
if verbose > 0 [
43-
log-info [
44-
"Decompressing: ^[[33m" name
30+
sys/log/info 'ZIP [
31+
"Extracting: ^[[33m" name
4532
" ^[[0mbytes:^[[33m" cmp-size "^[[0m->^[[33m" unc-size
4633
]
4734
]
@@ -86,7 +73,7 @@ register-codec [
8673
zip-data: read zip-data
8774
]
8875
if verbose > 0 [
89-
print ["^[[1;32mDecode ZIP data^[[m (^[[1m" length? zip-data "^[[mbytes )"]
76+
sys/log/info 'ZIP ["^[[1;32mDecode ZIP data^[[m (^[[1m" length? zip-data "^[[mbytes )"]
9077
]
9178
bin: binary zip-data
9279

@@ -101,23 +88,23 @@ register-codec [
10188
binary/read bin [AT :pos type: UI32LE]
10289
switch/default type [
10390
134695760 [ ;#{08074B50}
104-
if verbose > 1 [log-more "Data Descriptor"]
91+
if verbose > 1 [sys/log/more 'ZIP "Data Descriptor"]
10592
binary/read bin [
10693
crc: SI32LE
10794
cmp-size: UI32LE ; compressed size
10895
unc-size: UI32LE ; uncompressed size
10996
]
11097

11198
if all [only not find files name][
112-
if verbose > 1 [log-debug "not extracting"]
99+
if verbose > 1 [sys/log/debug 'ZIP "not extracting"]
113100
continue
114101
]
115102

116103
either all [name data-pos > 0] [
117104
data: decompress* at zip-data :data-pos any [validate validate-crc?]
118105
repend result [name reduce [modified crc data]]
119106
][
120-
if verbose > 0 [log-info ["Decompressing: ^[[33m" name]]
107+
if verbose > 0 [sys/log/info 'ZIP ["Extracting: ^[[33m" name]]
121108
repend result [name none]
122109
]
123110

@@ -127,7 +114,7 @@ register-codec [
127114
]
128115
]
129116
67324752 [ ;#{04034B50}
130-
if verbose > 1 [log-more "Local file header"]
117+
if verbose > 1 [sys/log/more 'ZIP "Local file header"]
131118
header: binary/read bin [
132119
UI16LE ; version
133120
flags: BITSET16 ; flags
@@ -142,7 +129,7 @@ register-codec [
142129
extr: BYTES :len-extr
143130
data-pos: INDEX
144131
]
145-
if verbose > 2 [log-debug mold header]
132+
if verbose > 2 [sys/log/debug 'ZIP mold header]
146133
name: to file! name
147134
if all [
148135
flags/12 ; bit 3
@@ -152,20 +139,20 @@ register-codec [
152139
][
153140
; The correct values are put in the data descriptor
154141
; immediately following the compressed data.
155-
if verbose > 1 [log-debug "waiting for Data Descriptor"]
142+
if verbose > 1 [sys/log/debug 'ZIP "waiting for Data Descriptor"]
156143
continue
157144
]
158145

159146
if all [only not find files name][
160-
if verbose > 1 [log-debug "not extracting"]
147+
if verbose > 1 [sys/log/debug 'ZIP "not extracting"]
161148
continue
162149
]
163150

164151
either all [unc-size > 0] [
165152
data: decompress* bin/buffer any [validate validate-crc?]
166153
repend result [name reduce [modified crc data]]
167154
][
168-
if verbose > 0 [log-info ["Decompressing: ^[[33m" name]]
155+
if verbose > 0 [sys/log/info 'ZIP ["Extracting: ^[[33m" name]]
169156
repend result [name none]
170157
]
171158
if only [
@@ -174,7 +161,7 @@ register-codec [
174161
]
175162
]
176163
33639248 [ ;#{02014B50}
177-
if verbose > 1 [log-more "Central directory structure"]
164+
if verbose > 1 [sys/log/more 'ZIP "Central directory structure"]
178165
cheader: binary/read bin [
179166
UI16LE ; version made by
180167
UI16LE ; version needed to extract
@@ -195,11 +182,11 @@ register-codec [
195182
extr: BYTES :len-extr
196183
comm: BYTES :len-comm
197184
]
198-
if verbose > 2 [log-debug mold cheader]
199-
unless empty? comm [log-info ["Comment: ^[[33m" to-string comm "^[[0m" mold to file! name]]
185+
if verbose > 2 [sys/log/debug 'ZIP mold cheader]
186+
unless empty? comm [sys/log/info 'ZIP ["Comment: ^[[33m" to-string comm "^[[0m" mold to file! name]]
200187
]
201188
101010256 [ ;#{06054B50}
202-
if verbose > 1 [log-more "End of central directory record"]
189+
if verbose > 1 [sys/log/more 'ZIP "End of central directory record"]
203190
data: binary/read bin [
204191
UI16LE ; number of this disk
205192
UI16LE ; number of the disk with the start of the central directory
@@ -210,10 +197,10 @@ register-codec [
210197
len: UI16LE ; .ZIP file comment length
211198
BYTES :len ; .ZIP file comment
212199
]
213-
if verbose > 2 [log-debug mold data]
200+
if verbose > 2 [sys/log/debug 'ZIP mold data]
214201
]
215202
101075792 [ ;#{06064b50}
216-
if verbose > 1 [log-more "Zip64 end of central directory record"]
203+
if verbose > 1 [sys/log/more 'ZIP "Zip64 end of central directory record"]
217204
data: binary/read bin [
218205
UI64LE ; directory record
219206
UI16LE ; version made by
@@ -226,10 +213,10 @@ register-codec [
226213
UI64LE ; offset of start of central directory with respect to the starting disk number
227214
;@@BYTES ?? ; zip64 extensible data sector (variable size)
228215
]
229-
if verbose > 2 [log-debug mold data]
216+
if verbose > 2 [sys/log/debug 'ZIP mold data]
230217
]
231218
][
232-
if verbose > 1 [log-more ["Unknown ZIP signature:" mold skip to-binary type 4]]
219+
if verbose > 1 [sys/log/more 'ZIP ["Unknown ZIP signature:" mold skip to-binary type 4]]
233220
]
234221
]
235222
buffer: none ; cleanup
@@ -251,6 +238,6 @@ register-codec [
251238
]
252239
]
253240
validate-crc?: true
254-
verbose: 1
241+
verbose: 3
255242
level: 9
256243
]

src/mezz/sys-codec.r

+17
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,21 @@ encoding?: function [
106106
none
107107
]
108108

109+
log: func [
110+
"Prints out debug message"
111+
id [word!] "Source of the log message"
112+
msg "Output message"
113+
/info
114+
/more
115+
/debug
116+
][
117+
if block? msg [msg: form reduce :msg]
118+
case [
119+
info [ print ajoin [" ^[[1;33m[" id "] ^[[36m" msg "^[[0m"]]
120+
more [ print ajoin [" ^[[33m[" id "] ^[[0;36m" msg "^[[0m"]]
121+
debug [ print ajoin [" ^[[33m[" id "] ^[[0;32m" msg "^[[0m"]]
122+
true [ print ajoin [" ^[[33m[" id "] " msg "^[[0m"]]
123+
]
124+
]
125+
109126
export [register-codec decode encode encoding?]

0 commit comments

Comments
 (0)