@@ -35,6 +35,10 @@ def timestamp():
35
35
return int (time .time ())
36
36
37
37
38
+ def log_level_enabled (level ):
39
+ return logging .getLogger ().isEnabledFor (level )
40
+
41
+
38
42
class ArduinoCloudObject (SenmlRecord ):
39
43
def __init__ (self , name , ** kwargs ):
40
44
self .on_read = kwargs .pop ("on_read" , None )
@@ -97,10 +101,11 @@ def value(self, value):
97
101
)
98
102
self ._updated = True
99
103
self .timestamp = timestamp ()
100
- logging .debug (
101
- f"%s: { self .name } value: { value } ts: { self .timestamp } "
102
- % ("Init" if self .value is None else "Update" )
103
- )
104
+ if log_level_enabled (logging .DEBUG ):
105
+ logging .debug (
106
+ f"%s: { self .name } value: { value } ts: { self .timestamp } "
107
+ % ("Init" if self .value is None else "Update" )
108
+ )
104
109
self ._value = value
105
110
106
111
def __getattr__ (self , attr ):
@@ -239,14 +244,16 @@ def update_systime(self, server, timeout):
239
244
except ImportError :
240
245
pass # No ntptime module.
241
246
except Exception as e :
242
- logging .error (f"Failed to set RTC time from NTP: { e } ." )
247
+ if log_level_enabled (logging .ERROR ):
248
+ logging .error (f"Failed to set RTC time from NTP: { e } ." )
243
249
244
250
def create_task (self , name , coro , * args , ** kwargs ):
245
251
if callable (coro ):
246
252
coro = coro (* args )
247
253
if self .started :
248
254
self .tasks [name ] = asyncio .create_task (coro )
249
- logging .info (f"task: { name } created." )
255
+ if log_level_enabled (logging .INFO ):
256
+ logging .info (f"task: { name } created." )
250
257
else :
251
258
# Defer task creation until there's a running event loop.
252
259
self .tasks [name ] = coro
@@ -275,12 +282,15 @@ def senml_generic_callback(self, record, **kwargs):
275
282
# This callback catches all unknown/umatched sub/records that were not part of the pack.
276
283
rname , sname = record .name .split (":" ) if ":" in record .name else [record .name , None ]
277
284
if rname in self .records :
278
- logging .info (f"Ignoring cloud initialization for record: { record .name } " )
285
+ if log_level_enabled (logging .INFO ):
286
+ logging .info (f"Ignoring cloud initialization for record: { record .name } " )
279
287
else :
280
- logging .warning (f"Unkown record found: { record .name } value: { record .value } " )
288
+ if log_level_enabled (logging .WARNING ):
289
+ logging .warning (f"Unkown record found: { record .name } value: { record .value } " )
281
290
282
291
def mqtt_callback (self , topic , message ):
283
- logging .debug (f"mqtt topic: { topic [- 8 :]} ... message: { message [:8 ]} ..." )
292
+ if log_level_enabled (logging .DEBUG ):
293
+ logging .debug (f"mqtt topic: { topic [- 8 :]} ... message: { message [:8 ]} ..." )
284
294
self .senmlpack .clear ()
285
295
for record in self .records .values ():
286
296
# If the object is uninitialized, updates are always allowed even if it's a read-only
@@ -318,7 +328,8 @@ async def conn_task(self, interval=1.0, backoff=1.2):
318
328
self .mqtt .connect ()
319
329
break
320
330
except Exception as e :
321
- logging .warning (f"Connection failed { e } , retrying after { interval } s" )
331
+ if log_level_enabled (logging .WARNING ):
332
+ logging .warning (f"Connection failed { e } , retrying after { interval } s" )
322
333
await asyncio .sleep (interval )
323
334
interval = min (interval * backoff , 4.0 )
324
335
@@ -339,8 +350,9 @@ async def mqtt_task(self, interval=0.100):
339
350
record .add_to_pack (self .senmlpack , push = True )
340
351
if len (self .senmlpack ._data ):
341
352
logging .debug ("Pushing records to Arduino IoT cloud:" )
342
- for record in self .senmlpack ._data :
343
- logging .debug (f" ==> record: { record .name } value: { str (record .value )[:48 ]} ..." )
353
+ if log_level_enabled (logging .DEBUG ):
354
+ for record in self .senmlpack ._data :
355
+ logging .debug (f" ==> record: { record .name } value: { str (record .value )[:48 ]} ..." )
344
356
self .mqtt .publish (self .topic_out , self .senmlpack .to_cbor (), qos = 1 )
345
357
self .last_ping = timestamp ()
346
358
elif self .keepalive and (timestamp () - self .last_ping ) > self .keepalive :
@@ -375,9 +387,9 @@ async def run(self):
375
387
if task .done ():
376
388
self .tasks .pop (name )
377
389
self .records .pop (name , None )
378
- if isinstance (task_except , DoneException ):
390
+ if isinstance (task_except , DoneException ) and log_level_enabled ( logging . INFO ) :
379
391
logging .info (f"task: { name } complete." )
380
- elif task_except is not None :
392
+ elif task_except is not None and log_level_enabled ( logging . ERROR ) :
381
393
logging .error (f"task: { name } raised exception: { str (task_except )} ." )
382
394
if name == "mqtt_task" :
383
395
self .create_task ("conn_task" , self .conn_task )
0 commit comments