99from types import ModuleType
1010
1111import argparse
12+
1213argcomplete : Union [None , ModuleType ] = None
1314try :
1415 import argcomplete # type: ignore
6263from meshtastic .protobuf import channel_pb2 , config_pb2 , portnums_pb2 , mesh_pb2
6364from meshtastic .version import get_active_version
6465
66+ logger = logging .getLogger (__name__ )
67+
6568def onReceive (packet , interface ) -> None :
6669 """Callback invoked when a packet arrives"""
6770 args = mt_config .args
6871 try :
6972 d = packet .get ("decoded" )
70- logging .debug (f"in onReceive() d:{ d } " )
73+ logger .debug (f"in onReceive() d:{ d } " )
7174
7275 # Exit once we receive a reply
7376 if (
@@ -101,7 +104,7 @@ def onConnection(interface, topic=pub.AUTO_TOPIC) -> None: # pylint: disable=W0
101104def checkChannel (interface : MeshInterface , channelIndex : int ) -> bool :
102105 """Given an interface and channel index, return True if that channel is non-disabled on the local node"""
103106 ch = interface .localNode .getChannelByChannelIndex (channelIndex )
104- logging .debug (f"ch:{ ch } " )
107+ logger .debug (f"ch:{ ch } " )
105108 return ch and ch .role != channel_pb2 .Channel .Role .DISABLED
106109
107110
@@ -114,7 +117,7 @@ def _printSetting(config_type, uni_name, pref_value, repeated):
114117 else :
115118 pref_value = meshtastic .util .toStr (pref_value )
116119 print (f"{ str (config_type .name )} .{ uni_name } : { str (pref_value )} " )
117- logging .debug (f"{ str (config_type .name )} .{ uni_name } : { str (pref_value )} " )
120+ logger .debug (f"{ str (config_type .name )} .{ uni_name } : { str (pref_value )} " )
118121
119122 name = splitCompoundName (comp_name )
120123 wholeField = name [0 ] == name [1 ] # We want the whole field
@@ -123,8 +126,8 @@ def _printSetting(config_type, uni_name, pref_value, repeated):
123126 # Note: protobufs has the keys in snake_case, so snake internally
124127 snake_name = meshtastic .util .camel_to_snake (name [1 ])
125128 uni_name = camel_name if mt_config .camel_case else snake_name
126- logging .debug (f"snake_name:{ snake_name } camel_name:{ camel_name } " )
127- logging .debug (f"use camel:{ mt_config .camel_case } " )
129+ logger .debug (f"snake_name:{ snake_name } camel_name:{ camel_name } " )
130+ logger .debug (f"use camel:{ mt_config .camel_case } " )
128131
129132 # First validate the input
130133 localConfig = node .localConfig
@@ -198,8 +201,8 @@ def setPref(config, comp_name, raw_val) -> bool:
198201 snake_name = meshtastic .util .camel_to_snake (name [- 1 ])
199202 camel_name = meshtastic .util .snake_to_camel (name [- 1 ])
200203 uni_name = camel_name if mt_config .camel_case else snake_name
201- logging .debug (f"snake_name:{ snake_name } " )
202- logging .debug (f"camel_name:{ camel_name } " )
204+ logger .debug (f"snake_name:{ snake_name } " )
205+ logger .debug (f"camel_name:{ camel_name } " )
203206
204207 objDesc = config .DESCRIPTOR
205208 config_part = config
@@ -223,7 +226,7 @@ def setPref(config, comp_name, raw_val) -> bool:
223226 val = meshtastic .util .fromStr (raw_val )
224227 else :
225228 val = raw_val
226- logging .debug (f"valStr:{ raw_val } val:{ val } " )
229+ logger .debug (f"valStr:{ raw_val } val:{ val } " )
227230
228231 if snake_name == "wifi_psk" and len (str (raw_val )) < 8 :
229232 print ("Warning: network.wifi_psk must be 8 or more characters." )
@@ -608,7 +611,7 @@ def onConnected(interface):
608611 time .sleep (1 )
609612 if interface .gotResponse :
610613 break
611- logging .debug (f"end of gpio_rd" )
614+ logger .debug (f"end of gpio_rd" )
612615
613616 if args .gpio_watch :
614617 bitmask = int (args .gpio_watch , 16 )
@@ -1064,7 +1067,7 @@ def setSimpleConfig(modem_preset):
10641067 # Even if others said we could close, stay open if the user asked for a tunnel
10651068 closeNow = False
10661069 if interface .noProto :
1067- logging .warning (f"Not starting Tunnel - disabled by noProto" )
1070+ logger .warning (f"Not starting Tunnel - disabled by noProto" )
10681071 else :
10691072 if args .tunnel_net :
10701073 tunnel .Tunnel (interface , subnet = args .tunnel_net )
@@ -1255,14 +1258,14 @@ def create_power_meter():
12551258 meter = SimPowerSupply ()
12561259
12571260 if meter and v :
1258- logging .info (f"Setting power supply to { v } volts" )
1261+ logger .info (f"Setting power supply to { v } volts" )
12591262 meter .v = v
12601263 meter .powerOn ()
12611264
12621265 if args .power_wait :
12631266 input ("Powered on, press enter to continue..." )
12641267 else :
1265- logging .info ("Powered-on, waiting for device to boot" )
1268+ logger .info ("Powered-on, waiting for device to boot" )
12661269 time .sleep (5 )
12671270
12681271
@@ -1276,6 +1279,10 @@ def common():
12761279 format = "%(levelname)s file:%(filename)s %(funcName)s line:%(lineno)s %(message)s" ,
12771280 )
12781281
1282+ # set all meshtastic loggers to DEBUG
1283+ if not (args .debug or args .listen ) and args .debuglib :
1284+ logging .getLogger ('meshtastic' ).setLevel (logging .DEBUG )
1285+
12791286 if len (sys .argv ) == 1 :
12801287 parser .print_help (sys .stderr )
12811288 meshtastic .util .our_exit ("" , 1 )
@@ -1317,7 +1324,7 @@ def common():
13171324 args .seriallog = "none" # assume no debug output in this case
13181325
13191326 if args .deprecated is not None :
1320- logging .error (
1327+ logger .error (
13211328 "This option has been deprecated, see help below for the correct replacement..."
13221329 )
13231330 parser .print_help (sys .stderr )
@@ -1336,18 +1343,18 @@ def common():
13361343 logfile = sys .stdout
13371344 elif args .seriallog == "none" :
13381345 args .seriallog = None
1339- logging .debug ("Not logging serial output" )
1346+ logger .debug ("Not logging serial output" )
13401347 logfile = None
13411348 else :
1342- logging .info (f"Logging serial output to { args .seriallog } " )
1349+ logger .info (f"Logging serial output to { args .seriallog } " )
13431350 # Note: using "line buffering"
13441351 # pylint: disable=R1732
13451352 logfile = open (args .seriallog , "w+" , buffering = 1 , encoding = "utf8" )
13461353 mt_config .logfile = logfile
13471354
13481355 subscribe ()
13491356 if args .ble_scan :
1350- logging .debug ("BLE scan starting" )
1357+ logger .debug ("BLE scan starting" )
13511358 for x in BLEInterface .scan ():
13521359 print (f"Found: name='{ x .name } ' address='{ x .address } '" )
13531360 meshtastic .util .our_exit ("BLE scan finished" , 0 )
@@ -1438,7 +1445,7 @@ def common():
14381445 while True :
14391446 time .sleep (1000 )
14401447 except KeyboardInterrupt :
1441- logging .info ("Exiting due to keyboard interrupt" )
1448+ logger .info ("Exiting due to keyboard interrupt" )
14421449
14431450 # don't call exit, background threads might be running still
14441451 # sys.exit(0)
@@ -2045,6 +2052,10 @@ def initParser():
20452052 "--debug" , help = "Show API library debug log messages" , action = "store_true"
20462053 )
20472054
2055+ group .add_argument (
2056+ "--debuglib" , help = "Show only API library debug log messages" , action = "store_true"
2057+ )
2058+
20482059 group .add_argument (
20492060 "--test" ,
20502061 help = "Run stress test against all connected Meshtastic devices" ,
0 commit comments