Skip to content

Commit 41acadb

Browse files
committed
Logging cleanup
1 parent de5465e commit 41acadb

File tree

8 files changed

+91
-89
lines changed

8 files changed

+91
-89
lines changed

src/devices/base.toit

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import i2c
22
import gpio
33
import uart
44
import io
5+
import log
56
import .i2c
67

78
/*
@@ -37,12 +38,14 @@ abstract class LightbugDevice implements Device:
3738
i2c-writer_ /Writer
3839

3940
name_ /string
41+
logger_ /log.Logger
4042

41-
constructor name/string i2c-sda/int=I2C-SDA i2c-scl/int=I2C-SCL:
43+
constructor name/string i2c-sda/int=I2C-SDA i2c-scl/int=I2C-SCL --logger/log.Logger=(log.default.with-name "lb-device"):
4244
name_ = name
45+
logger_ = logger
4346
i2c-device_ = LBI2CDevice --sda=i2c-sda --scl=i2c-scl
44-
i2c-reader_ = Reader i2c-device_
45-
i2c-writer_ = Writer i2c-device_
47+
i2c-reader_ = Reader i2c-device_ --logger=logger_
48+
i2c-writer_ = Writer i2c-device_ --logger=logger_
4649

4750
name -> string:
4851
return name_

src/devices/fake.toit

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class FakeReader extends io.Reader with io.InMixin:
2020
constructor:
2121

2222
read_ -> ByteArray?:
23-
// log.debug "FakeReader: Simulating read operation"
2423
return #[]
2524

2625
class FakeWriter extends io.Writer with io.OutMixin:

src/devices/i2c.toit

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,29 @@ LBI2CDevice --sda/int --scl/int -> i2c.Device:
2121
class Reader extends io.Reader:
2222
device /i2c.Device
2323
finishWhenEmpty_ /bool
24+
logger_ /log.Logger
2425

25-
constructor .device --finishWhenEmpty=false:
26+
constructor .device --finishWhenEmpty=false --logger/log.Logger:
2627
// XXX: --finishWhenEmpty is not used since factoring out into the lightbug package
2728
// TODO: Decide if we want to keep it, refactor it, or remove it...
2829
finishWhenEmpty_ = finishWhenEmpty
30+
logger_ = logger
2931

3032
/**
3133
Reads the next bytes.
3234
There are no yields in this function, so it will block until there are bytes to read,
3335
as we want to empty the buffer as soon as possible into our own memory.
3436
*/
3537
read_ -> ByteArray?:
36-
// log.debug "calling read_ in LB Reader for i2c"
38+
// logger_.debug "calling read_ in LB Reader for i2c"
3739
all := #[]
3840
all-expected := 0
3941
loops := 0
4042
// Read from the buffer as fast as possible (as our buffer is bigger)
4143
// At most 5*(tx buffer), so 5*1000 = 5KB
4244
while loops <= 5:
4345
loops++
44-
// log.debug "Getting number of bytes available to read, loop $loops"
46+
// logger_.debug "Getting number of bytes available to read, loop $loops"
4547
len-bytes := device.read-address #[I2C-COMMAND-LIGHTBUG-READABLE-BYTES] 2
4648
len-int := LITTLE-ENDIAN.uint16 len-bytes 0
4749
all-expected = all-expected + len-int
@@ -50,39 +52,41 @@ class Reader extends io.Reader:
5052
// uart does this with a read state, for now we will just sleep a bit...
5153
if len-int == 0:
5254
if finishWhenEmpty_:
53-
log.debug "No bytes to read, finishing"
55+
logger_.debug "No bytes to read, finishing"
5456
return null
55-
// log.debug "No bytes to read, yielding"
57+
// logger_.debug "No bytes to read, yielding"
5658
break // Leave the while loop
5759
58-
log.debug "Got $len-int bytes to read"
60+
logger_.debug "Got $len-int bytes to read"
5961

6062
while len-int > 0:
6163
chunkSize := min len-int 254
62-
log.debug "Reading chunk of $chunkSize bytes stage 1"
64+
logger_.debug "Reading chunk of $chunkSize bytes stage 1"
6365
device.write #[I2C-COMMAND-LIGHTBUG-READ, chunkSize]
64-
log.debug "Reading chunk of $chunkSize bytes stage 2"
66+
logger_.debug "Reading chunk of $chunkSize bytes stage 2"
6567
b := device.read chunkSize
6668
if b.size != chunkSize:
67-
log.error "Failed to read chunk $chunkSize bytes, got $b.size bytes"
69+
logger_.error "Failed to read chunk $chunkSize bytes, got $b.size bytes"
6870
return null
6971
all += b
7072
len-int -= chunkSize
7173

7274
if all.size != all-expected:
73-
log.error "Failed to read $all-expected bytes, got $all.size bytes"
75+
logger_.error "Failed to read $all-expected bytes, got $all.size bytes"
7476
return null
7577

76-
log.debug "Read $all.size bytes after $loops loops"
78+
logger_.debug "Read $all.size bytes after $loops loops"
7779

7880
yield // They are in our buffer now, so yield briefly before returning
7981
return all
8082

8183
class Writer extends io.Writer:
8284
device /i2c.Device
8385
can-write-bytes /int := 0
86+
logger_ /log.Logger
8487

85-
constructor .device:
88+
constructor .device --logger/log.Logger:
89+
logger_ = logger
8690

8791
/**
8892
Writes the given $data to this writer.
@@ -97,21 +101,21 @@ class Writer extends io.Writer:
97101
bytes = ByteArray.from data
98102

99103
bytes-in-window := to - from
100-
log.debug "Going to write $bytes-in-window bytes"
101-
log.debug "Bytes: $bytes"
104+
logger_.debug "Going to write $bytes-in-window bytes"
105+
logger_.debug "Bytes: $bytes"
102106

103107
// Check the receiver has enough space for our bytes before sending...
104108
// TODO could refactor this to send in smaller chunks if needed?!
105109
while can-write-bytes == 0:
106-
log.debug "Updating or waiting for writeable bytes"
110+
logger_.debug "Updating or waiting for writeable bytes"
107111
len-bytes := device.read-address #[I2C-COMMAND-LIGHTBUG-WRITEABLE_BYTES] 2
108112
can-write-bytes = LITTLE-ENDIAN.uint16 len-bytes 0
109-
log.debug "Can write $can-write-bytes bytes"
113+
logger_.debug "Can write $can-write-bytes bytes"
110114
if can-write-bytes == 0:
111-
log.debug "Waiting for some bytes to be writeable"
115+
logger_.debug "Waiting for some bytes to be writeable"
112116
sleep (Duration --ms=50)
113117
else:
114-
log.debug "Can write $can-write-bytes bytes, continuing"
118+
logger_.debug "Can write $can-write-bytes bytes, continuing"
115119

116120
current-index := from
117121
read-to-index := 0
@@ -121,14 +125,14 @@ class Writer extends io.Writer:
121125
writing := min (to - current-index) (min can-write-bytes 255)
122126
read-to-index = current-index + writing
123127

124-
log.debug "Writing bytes $current-index to $read-to-index, $writing bytes"
125-
log.debug "Bytes: $bytes[current-index..read-to-index]"
128+
logger_.debug "Writing bytes $current-index to $read-to-index, $writing bytes"
129+
logger_.debug "Bytes: $bytes[current-index..read-to-index]"
126130
send-len := #[0]
127131
LITTLE-ENDIAN.put-uint8 send-len 0 writing
128132
device.write-address #[I2C-COMMAND-LIGHTBUG-WRITE] send-len + bytes[current-index..read-to-index]
129133
written += writing
130134
can-write-bytes -= writing
131135
current-index = read-to-index
132136

133-
log.debug "Wrote $written bytes"
137+
logger_.debug "Wrote $written bytes"
134138
return written

src/protocol/data.toit

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,47 @@ class Data:
1919
if bytes.size < 2:
2020
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read fields, expected 2 bytes but only have " + bytes.size.stringify
2121
fields := (bytes[1] << 8) + bytes[0]
22-
// log.debug "Data fields: " + fields.stringify
2322
if bytes.size < 2 + fields:
2423
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data types, expected " + (2 + fields).stringify + " bytes but only have " + bytes.size.stringify
2524
// read data types
2625
for i := 0; i < fields; i++:
2726
dataType := bytes[2 + i]
2827
dataTypes_.add dataType
29-
// log.debug "Data data type: " + dataType.stringify
3028
// read data (each is a uint8 length, then that number of bytes)
3129
index := 2 + fields
3230
for i := 0; i < fields; i++:
3331
if index >= bytes.size:
3432
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data length, expected " + index.stringify + " bytes but only have " + bytes.size.stringify
3533
length := bytes[index]
36-
// log.debug "Data data length: " + length.stringify
3734
index += 1
3835
if index + length > bytes.size:
3936
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data, expected " + (index + length).stringify + " bytes but only have " + bytes.size.stringify
4037
index += length
41-
// log.debug "Data data bytes: " + bytes[index - length..index].stringify
4238
dataField := DataField (list-to-byte-array bytes[index - length..index])
4339
data_.add dataField
44-
// log.debug "Data data: " + dataField.stringify
4540

4641
constructor.from-bytes bytes/ByteArray:
4742
if bytes.size < 2:
4843
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read fields, expected 2 bytes but only have " + bytes.size.stringify
4944
fields := LITTLE-ENDIAN.uint16 bytes 0
50-
// log.debug "Data fields: " + fields.stringify
5145
if bytes.size < 2 + fields:
5246
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data types, expected " + (2 + fields).stringify + " bytes but only have " + bytes.size.stringify
5347
// read data types
5448
for i := 0; i < fields; i++:
5549
dataType := bytes[2 + i]
5650
dataTypes_.add dataType
57-
// log.debug "Data data type: " + dataType.stringify
5851
// read data (each is a uint8 length, then that number of bytes)
5952
index := 2 + fields
6053
for i := 0; i < fields; i++:
6154
if index >= bytes.size:
6255
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data length, expected " + index.stringify + " bytes but only have " + bytes.size.stringify
6356
length := bytes[index]
64-
// log.debug "Data data length: " + length.stringify
6557
index += 1
6658
if index + length > bytes.size:
6759
throw "V3 OUT_OF_BOUNDS: Not enough bytes to read data, expected " + (index + length).stringify + " bytes but only have " + bytes.size.stringify
6860
index += length
69-
// log.debug "Data data bytes: " + bytes[index - length..index].stringify
7061
dataField := DataField (bytes[index - length..index])
7162
data_.add dataField
72-
// log.debug "Data data: " + dataField.stringify
7363

7464
stringify -> string:
7565
s := ""
@@ -113,7 +103,7 @@ class Data:
113103
else if data < 1099511627776:
114104
add-data-uint64 dataType data
115105
else:
116-
log.error "Data too large for uintn: " + data.stringify
106+
log.error "Data too large for uintn: $(data)"
117107

118108
add-data-float32 dataType/int data/float -> none:
119109
b := #[0,0,0,0]
@@ -135,7 +125,7 @@ class Data:
135125
return true
136126
return false
137127
if e:
138-
log.warn "Failed to check for data: " + e.stringify
128+
log.warn "Failed to check for data: $(e)"
139129
return false
140130

141131
remove-data dataType/int -> none:
@@ -146,7 +136,7 @@ class Data:
146136
data_.remove --at=i
147137
return
148138
if e:
149-
log.warn "Failed to remove data: " + e.stringify
139+
log.warn "Failed to remove data: $(e)"
150140

151141
// returns the data for the type, or an empty list if not found
152142
// will never throw an error
@@ -157,7 +147,7 @@ class Data:
157147
return data_[i].dataBytes_
158148
return #[]
159149
if e:
160-
log.warn "Failed to get data: " + e.stringify
150+
log.warn "Failed to get data: $(e)"
161151
return #[]
162152

163153
get-data-ascii dataType/int -> string:
@@ -168,7 +158,7 @@ class Data:
168158
// TODO use LITTLE-ENDIAN? (When we have a byte array not a list?)
169159
data := get-data dataType
170160
if data.size == 0:
171-
log.warn "No data for datatype " + dataType.stringify
161+
log.warn "No data for datatype $(dataType)"
172162
return 0
173163
return data[0]
174164

@@ -184,7 +174,7 @@ class Data:
184174
get-data-uint64 dataType/int -> int:
185175
data := get-data dataType
186176
if data.size < 8:
187-
log.warn "No data for datatype " + dataType.stringify
177+
log.warn "No data for datatype $(dataType)"
188178
return 0
189179
return (data[7] << 56) + (data[6] << 48) + (data[5] << 40) + (data[4] << 32) + (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0]
190180

@@ -197,7 +187,7 @@ class Data:
197187
get-data-list-uint16 dataType/int -> List:
198188
data := get-data dataType
199189
if data.size % 2 != 0:
200-
log.error "Data size not a multiple of 2 for datatype " + dataType.stringify
190+
log.error "Data size not a multiple of 2 for datatype $(dataType)"
201191
return []
202192
l := []
203193
for i := 0; i < data.size; i += 2:
@@ -219,7 +209,7 @@ class Data:
219209
get-data-list-uint32 dataType/int -> List:
220210
data := get-data dataType
221211
if data.size % 4 != 0:
222-
log.error "Data size not a multiple of 4 for datatype " + dataType.stringify
212+
log.error "Data size not a multiple of 4 for datatype $(dataType)"
223213
return []
224214
l := []
225215
for i := 0; i < data.size; i += 4:
@@ -236,7 +226,7 @@ class Data:
236226
get-data-list-int32-pairs dataType/int -> List:
237227
data := get-data dataType
238228
if data.size % 8 != 0:
239-
log.error "Data size not a multiple of 8 for datatype " + dataType.stringify
229+
log.error "Data size not a multiple of 8 for datatype $(dataType)"
240230
return []
241231
l := []
242232
for i := 0; i < data.size; i += 8:
@@ -246,7 +236,7 @@ class Data:
246236
get-data-list-coordinates dataType/int -> List:
247237
data := get-data dataType
248238
if data.size % 8 != 0:
249-
log.error "Data size not a multiple of 8 for datatype " + dataType.stringify
239+
log.error "Data size not a multiple of 8 for datatype $(dataType)"
250240
return []
251241
l := []
252242
for i := 0; i < data.size; i += 8:
@@ -266,7 +256,7 @@ class Data:
266256
// etc
267257
data := get-data dataType
268258
if data.size == 0:
269-
log.warn "No data for datatype " + dataType.stringify
259+
log.warn "No data for datatype $(dataType)"
270260
return 0
271261
if data.size == 1:
272262
return LITTLE-ENDIAN.uint8 data 0
@@ -285,7 +275,7 @@ class Data:
285275
if data.size == 8:
286276
// toit doesnt actually support uint64, so this will always be represented as int64
287277
return (data[7] << 56) + (data[6] << 48) + (data[5] << 40) + (data[4] << 32) + (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0]
288-
log.error "Data size too large for uintn: " + data.size.stringify
278+
log.error "Data size too large for uintn: $(data.size)"
289279
return 0
290280

291281
get-data-intn dataType/int -> int:
@@ -296,7 +286,7 @@ class Data:
296286
// etc
297287
data := get-data dataType
298288
if data.size == 0:
299-
log.warn "No data for datatype " + dataType.stringify
289+
log.warn "No data for datatype $(dataType)"
300290
return 0
301291
if data.size == 1:
302292
return LITTLE-ENDIAN.int8 data 0
@@ -315,7 +305,7 @@ class Data:
315305
if data.size == 8:
316306
// toit doesnt actually support int64, so this will always be represented as int64
317307
return (data[7] << 56) + (data[6] << 48) + (data[5] << 40) + (data[4] << 32) + (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0]
318-
log.error "Data size too large for intn: " + data.size.stringify
308+
log.error "Data size too large for intn: $(data.size)"
319309
return 0
320310

321311
size -> int:

0 commit comments

Comments
 (0)