Skip to content

Commit

Permalink
FEAT: introducing system log function for common system trace output
Browse files Browse the repository at this point in the history
Also using it for now with ZIP codec, instead of using local log functions.
  • Loading branch information
Oldes committed Mar 20, 2019
1 parent e3e33bb commit 94508ee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
53 changes: 20 additions & 33 deletions src/mezz/codec-zip.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,14 @@ register-codec [
cmp-size:
unc-size: 0

log-info: func[msg][
if block? msg [msg: reform msg]
print rejoin [" ^[[1;33m[ZIP] ^[[36m" msg "^[[0m"]
]
log-more: func[msg][
if block? msg [msg: reform msg]
print rejoin [" ^[[33m[ZIP] ^[[0;36m" msg "^[[0m"]
]
log-debug: func[msg][
if block? msg [msg: reform msg]
print rejoin [" ^[[33m[ZIP] ^[[0;32m" msg "^[[0m"]
]

decompress*: func [
data [binary! ]
validate [logic!]
/local output crc2
][
if verbose > 0 [
log-info [
"Decompressing: ^[[33m" name
sys/log/info 'ZIP [
"Extracting: ^[[33m" name
" ^[[0mbytes:^[[33m" cmp-size "^[[0m->^[[33m" unc-size
]
]
Expand Down Expand Up @@ -86,7 +73,7 @@ register-codec [
zip-data: read zip-data
]
if verbose > 0 [
print ["^[[1;32mDecode ZIP data^[[m (^[[1m" length? zip-data "^[[mbytes )"]
sys/log/info 'ZIP ["^[[1;32mDecode ZIP data^[[m (^[[1m" length? zip-data "^[[mbytes )"]
]
bin: binary zip-data

Expand All @@ -101,23 +88,23 @@ register-codec [
binary/read bin [AT :pos type: UI32LE]
switch/default type [
134695760 [ ;#{08074B50}
if verbose > 1 [log-more "Data Descriptor"]
if verbose > 1 [sys/log/more 'ZIP "Data Descriptor"]
binary/read bin [
crc: SI32LE
cmp-size: UI32LE ; compressed size
unc-size: UI32LE ; uncompressed size
]

if all [only not find files name][
if verbose > 1 [log-debug "not extracting"]
if verbose > 1 [sys/log/debug 'ZIP "not extracting"]
continue
]

either all [name data-pos > 0] [
data: decompress* at zip-data :data-pos any [validate validate-crc?]
repend result [name reduce [modified crc data]]
][
if verbose > 0 [log-info ["Decompressing: ^[[33m" name]]
if verbose > 0 [sys/log/info 'ZIP ["Extracting: ^[[33m" name]]
repend result [name none]
]

Expand All @@ -127,7 +114,7 @@ register-codec [
]
]
67324752 [ ;#{04034B50}
if verbose > 1 [log-more "Local file header"]
if verbose > 1 [sys/log/more 'ZIP "Local file header"]
header: binary/read bin [
UI16LE ; version
flags: BITSET16 ; flags
Expand All @@ -142,7 +129,7 @@ register-codec [
extr: BYTES :len-extr
data-pos: INDEX
]
if verbose > 2 [log-debug mold header]
if verbose > 2 [sys/log/debug 'ZIP mold header]
name: to file! name
if all [
flags/12 ; bit 3
Expand All @@ -152,20 +139,20 @@ register-codec [
][
; The correct values are put in the data descriptor
; immediately following the compressed data.
if verbose > 1 [log-debug "waiting for Data Descriptor"]
if verbose > 1 [sys/log/debug 'ZIP "waiting for Data Descriptor"]
continue
]

if all [only not find files name][
if verbose > 1 [log-debug "not extracting"]
if verbose > 1 [sys/log/debug 'ZIP "not extracting"]
continue
]

either all [unc-size > 0] [
data: decompress* bin/buffer any [validate validate-crc?]
repend result [name reduce [modified crc data]]
][
if verbose > 0 [log-info ["Decompressing: ^[[33m" name]]
if verbose > 0 [sys/log/info 'ZIP ["Extracting: ^[[33m" name]]
repend result [name none]
]
if only [
Expand All @@ -174,7 +161,7 @@ register-codec [
]
]
33639248 [ ;#{02014B50}
if verbose > 1 [log-more "Central directory structure"]
if verbose > 1 [sys/log/more 'ZIP "Central directory structure"]
cheader: binary/read bin [
UI16LE ; version made by
UI16LE ; version needed to extract
Expand All @@ -195,11 +182,11 @@ register-codec [
extr: BYTES :len-extr
comm: BYTES :len-comm
]
if verbose > 2 [log-debug mold cheader]
unless empty? comm [log-info ["Comment: ^[[33m" to-string comm "^[[0m" mold to file! name]]
if verbose > 2 [sys/log/debug 'ZIP mold cheader]
unless empty? comm [sys/log/info 'ZIP ["Comment: ^[[33m" to-string comm "^[[0m" mold to file! name]]
]
101010256 [ ;#{06054B50}
if verbose > 1 [log-more "End of central directory record"]
if verbose > 1 [sys/log/more 'ZIP "End of central directory record"]
data: binary/read bin [
UI16LE ; number of this disk
UI16LE ; number of the disk with the start of the central directory
Expand All @@ -210,10 +197,10 @@ register-codec [
len: UI16LE ; .ZIP file comment length
BYTES :len ; .ZIP file comment
]
if verbose > 2 [log-debug mold data]
if verbose > 2 [sys/log/debug 'ZIP mold data]
]
101075792 [ ;#{06064b50}
if verbose > 1 [log-more "Zip64 end of central directory record"]
if verbose > 1 [sys/log/more 'ZIP "Zip64 end of central directory record"]
data: binary/read bin [
UI64LE ; directory record
UI16LE ; version made by
Expand All @@ -226,10 +213,10 @@ register-codec [
UI64LE ; offset of start of central directory with respect to the starting disk number
;@@BYTES ?? ; zip64 extensible data sector (variable size)
]
if verbose > 2 [log-debug mold data]
if verbose > 2 [sys/log/debug 'ZIP mold data]
]
][
if verbose > 1 [log-more ["Unknown ZIP signature:" mold skip to-binary type 4]]
if verbose > 1 [sys/log/more 'ZIP ["Unknown ZIP signature:" mold skip to-binary type 4]]
]
]
buffer: none ; cleanup
Expand All @@ -251,6 +238,6 @@ register-codec [
]
]
validate-crc?: true
verbose: 1
verbose: 3
level: 9
]
17 changes: 17 additions & 0 deletions src/mezz/sys-codec.r
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,21 @@ encoding?: function [
none
]

log: func [
"Prints out debug message"
id [word!] "Source of the log message"
msg "Output message"
/info
/more
/debug
][
if block? msg [msg: form reduce :msg]
case [
info [ print ajoin [" ^[[1;33m[" id "] ^[[36m" msg "^[[0m"]]
more [ print ajoin [" ^[[33m[" id "] ^[[0;36m" msg "^[[0m"]]
debug [ print ajoin [" ^[[33m[" id "] ^[[0;32m" msg "^[[0m"]]
true [ print ajoin [" ^[[33m[" id "] " msg "^[[0m"]]
]
]

export [register-codec decode encode encoding?]

0 comments on commit 94508ee

Please sign in to comment.