@@ -35,6 +35,10 @@ def timestamp():
3535 return int (time .time ())
3636
3737
38+ def log_level_enabled (level ):
39+ return logging .getLogger ().isEnabledFor (level )
40+
41+
3842class ArduinoCloudObject (SenmlRecord ):
3943 def __init__ (self , name , ** kwargs ):
4044 self .on_read = kwargs .pop ("on_read" , None )
@@ -97,10 +101,11 @@ def value(self, value):
97101 )
98102 self ._updated = True
99103 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+ )
104109 self ._value = value
105110
106111 def __getattr__ (self , attr ):
@@ -239,14 +244,16 @@ def update_systime(self, server, timeout):
239244 except ImportError :
240245 pass # No ntptime module.
241246 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 } ." )
243249
244250 def create_task (self , name , coro , * args , ** kwargs ):
245251 if callable (coro ):
246252 coro = coro (* args )
247253 if self .started :
248254 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." )
250257 else :
251258 # Defer task creation until there's a running event loop.
252259 self .tasks [name ] = coro
@@ -275,12 +282,15 @@ def senml_generic_callback(self, record, **kwargs):
275282 # This callback catches all unknown/umatched sub/records that were not part of the pack.
276283 rname , sname = record .name .split (":" ) if ":" in record .name else [record .name , None ]
277284 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 } " )
279287 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 } " )
281290
282291 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 ]} ..." )
284294 self .senmlpack .clear ()
285295 for record in self .records .values ():
286296 # 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):
318328 self .mqtt .connect ()
319329 break
320330 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" )
322333 await asyncio .sleep (interval )
323334 interval = min (interval * backoff , 4.0 )
324335
@@ -339,8 +350,9 @@ async def mqtt_task(self, interval=0.100):
339350 record .add_to_pack (self .senmlpack , push = True )
340351 if len (self .senmlpack ._data ):
341352 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 ]} ..." )
344356 self .mqtt .publish (self .topic_out , self .senmlpack .to_cbor (), qos = 1 )
345357 self .last_ping = timestamp ()
346358 elif self .keepalive and (timestamp () - self .last_ping ) > self .keepalive :
@@ -375,9 +387,9 @@ async def run(self):
375387 if task .done ():
376388 self .tasks .pop (name )
377389 self .records .pop (name , None )
378- if isinstance (task_except , DoneException ):
390+ if isinstance (task_except , DoneException ) and log_level_enabled ( logging . INFO ) :
379391 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 ) :
381393 logging .error (f"task: { name } raised exception: { str (task_except )} ." )
382394 if name == "mqtt_task" :
383395 self .create_task ("conn_task" , self .conn_task )
0 commit comments