11"""code logging power consumption of meshtastic devices."""
22
33import atexit
4+ import io
45import logging
56import os
67import re
78import threading
8- import io
99import time
1010from dataclasses import dataclass
1111from datetime import datetime
@@ -100,10 +100,17 @@ def __init__(self, client: MeshInterface, dir_path: str) -> None:
100100 """
101101 self .client = client
102102 self .writer = FeatherWriter (f"{ dir_path } /slog" )
103- self .raw_file : Optional [io .TextIOWrapper ] = open ( # pylint: disable=consider-using-with
103+ self .raw_file : Optional [
104+ io .TextIOWrapper
105+ ] = open ( # pylint: disable=consider-using-with
104106 f"{ dir_path } /raw.txt" , "w" , encoding = "utf8"
105107 )
106- self .listener = pub .subscribe (self ._onLogMessage , TOPIC_MESHTASTIC_LOG_LINE )
108+
109+ # We need a closure here because the subscription API is very strict about exact arg matching
110+ def listen_glue (line , interface ): # pylint: disable=unused-argument
111+ self ._onLogMessage (line )
112+
113+ self .listener = pub .subscribe (listen_glue , TOPIC_MESHTASTIC_LOG_LINE )
107114
108115 def close (self ) -> None :
109116 """Stop logging."""
@@ -113,9 +120,7 @@ def close(self) -> None:
113120 self .raw_file = None # mark that we are shutting down
114121 f .close () # Close the raw.txt file
115122
116- def _onLogMessage (
117- self , line : str , interface : MeshInterface # pylint: disable=unused-argument
118- ) -> None :
123+ def _onLogMessage (self , line : str ) -> None :
119124 """Handle log messages.
120125
121126 line (str): the line of log output
@@ -126,17 +131,20 @@ def _onLogMessage(
126131 args = m .group (2 )
127132 args += " " # append a space so that if the last arg is an empty str it will still be accepted as a match
128133 logging .debug (f"SLog { src } , reason: { args } " )
129- d = log_defs .get (src )
130- if d :
131- r = d .format .parse (args ) # get the values with the correct types
132- if r :
133- di = r .named
134- di ["time" ] = datetime .now ()
135- self .writer .add_row (di )
136- else :
137- logging .warning (f"Failed to parse slog { line } with { d .format } " )
134+ if (src != "PM" ):
135+ logging .warning (f"Not yet handling structured log { src } (FIXME)" )
138136 else :
139- logging .warning (f"Unknown Structured Log: { line } " )
137+ d = log_defs .get (src )
138+ if d :
139+ r = d .format .parse (args ) # get the values with the correct types
140+ if r :
141+ di = r .named
142+ di ["time" ] = datetime .now ()
143+ self .writer .add_row (di )
144+ else :
145+ logging .warning (f"Failed to parse slog { line } with { d .format } " )
146+ else :
147+ logging .warning (f"Unknown Structured Log: { line } " )
140148 if self .raw_file :
141149 self .raw_file .write (line + "\n " ) # Write the raw log
142150
0 commit comments