Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
hishizuka committed Feb 27, 2024
1 parent 14f0eff commit 397d15d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
36 changes: 19 additions & 17 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class StreamToLogger:
logger = None
level = None
buffer = ""
error_char = 0
pre_char = ""
pre_spaces = False

def __init__(self, logger, level):
self.logger = logger
Expand All @@ -20,30 +21,31 @@ def __init__(self, logger, level):
def write(self, buf):

for line in buf.splitlines():
#self.logger.log(self.level, f"# ({len(line)}):'{line}'") # for debug
if line.startswith((
"<class 'usb.core.USBError'>",
"<class 'usb.core.USBTimeoutError'>",
)):
continue
elif line == "" :
if self.error_char > 0:
self.logger.log(self.level, self.buffer)

if self.pre_char in [" ", "^"] and line in [" ", "^"]:
self.buffer += line
elif len(line) > 1 and line.isspace():
self.buffer += line
self.pre_spaces = True
elif line not in [" ", "^"]:
if len(self.buffer):
if self.pre_spaces:
self.logger.log(self.level, self.buffer+line)
self.pre_spaces = False
else:
self.logger.log(self.level, f" {self.buffer}") # need space
self.buffer = ""
self.error_char += 1
if self.error_char == 3:
self.error_char = 0
continue
elif len(line):
self.logger.log(self.level, line)

if self.error_char == 0:
if line.isspace():
self.buffer += line
else:
self.logger.log(self.level, self.buffer+line)
self.buffer = ""
else:
self.buffer += line
self.pre_char = line


def flush(self):
pass

Expand Down
19 changes: 11 additions & 8 deletions modules/logger_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,12 @@ def count_up(self):
self.count_up_lock = False

def start_and_stop_manual(self):
time_str = datetime.now().strftime("%Y%m%d %H:%M:%S")
popup_extra = ""
pre_status = self.config.G_MANUAL_STATUS
display_flash_short = True

if self.config.G_MANUAL_STATUS != "START":
app_logger.info(f"->M START {time_str}")
app_logger.info(f"->M START{self.get_time_str()}")
self.start_and_stop("STOP")
self.config.G_MANUAL_STATUS = "START"
if self.config.gui is not None:
Expand All @@ -334,7 +333,7 @@ def start_and_stop_manual(self):

elif self.config.G_MANUAL_STATUS == "START":
display_flash_short = False
app_logger.info(f"->M STOP {time_str}")
app_logger.info(f"->M STOP{self.get_time_str()}")
self.start_and_stop("START")
self.config.G_MANUAL_STATUS = "STOP"
if self.config.gui is not None:
Expand All @@ -358,13 +357,18 @@ def start_and_stop_manual(self):
def start_and_stop(self, status=None):
if status is not None:
self.config.G_STOPWATCH_STATUS = status
time_str = datetime.now().strftime("%Y%m%d %H:%M:%S")
if self.config.G_STOPWATCH_STATUS != "START":
self.config.G_STOPWATCH_STATUS = "START"
app_logger.info(f"->START {time_str}")
app_logger.info(f"->START{self.get_time_str()}")
elif self.config.G_STOPWATCH_STATUS == "START":
self.config.G_STOPWATCH_STATUS = "STOP"
app_logger.info(f"->STOP {time_str}")
app_logger.info(f"->STOP{self.get_time_str()}")

def get_time_str(self):
if not self.config.G_LOG_OUTPUT_FILE:
return datetime.now().strftime(" %Y%m%d %H:%M:%S")
else:
return ""

def count_laps(self):
if self.values["count"] == 0 or self.values["count_lap"] == 0:
Expand All @@ -381,8 +385,7 @@ def count_laps(self):
self.average["lap"][k2]["count"] = 0
self.average["lap"][k2]["sum"] = 0
asyncio.create_task(self.record_log())
time_str = datetime.now().strftime("%Y%m%d %H:%M:%S")
app_logger.info(f"->LAP:{self.values['lap']} {time_str}")
app_logger.info(f"->LAP:{self.values['lap']}{self.get_time_str()}")

# show message
pre_lap_avg = self.record_stats["pre_lap_avg"]
Expand Down
13 changes: 9 additions & 4 deletions modules/utils/time.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from datetime import datetime

from timezonefinder import TimezoneFinder
_TIMEZONE_FINDER = False
try:
from timezonefinder import TimezoneFinder
_TIMEZONE_FINDER =True
except:
pass

from modules.utils.cmd import exec_cmd, exec_cmd_return_value
from logger import app_logger
Expand Down Expand Up @@ -31,6 +36,9 @@ def set_time(time_info):


async def set_timezone(lat, lon):
if not _TIMEZONE_FINDER:
return

app_logger.info("try to modify timezone by gps...")

tz_finder = TimezoneFinder()
Expand All @@ -49,10 +57,7 @@ async def set_timezone(lat, lon):
app_logger.warning(f"Timezone {tz_str} be could not set: {ret_code}")
else:
app_logger.info(f"success: {tz_str}")
return True
except TypeError as e:
app_logger.exception(f"Incorrect lat, lon passed: {e}")
return False
except Exception as e:
app_logger.warning(f"Could not set timezone: {e}")
return False

0 comments on commit 397d15d

Please sign in to comment.