Skip to content

Commit 29bac69

Browse files
committed
Fix logs and updater
1 parent 520a5dc commit 29bac69

2 files changed

Lines changed: 76 additions & 45 deletions

File tree

talkito/logs.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,41 @@ def log_message(level: str, message: str, logger_name: Optional[str] = None) ->
145145

146146
logger = get_logger(logger_name or __name__)
147147

148-
# Map custom levels to standard ones
149-
if level == "BUFFER":
150-
logger.info(f"[BUFFER] {message}")
151-
elif level == "FILTER":
152-
logger.debug(f"[FILTER] {message}")
153-
elif level.upper() == "DEBUG":
154-
logger.debug(message)
155-
else:
156-
getattr(logger, level.lower(), logger.info)(message)
148+
# Get caller information
149+
import inspect
150+
frame = inspect.currentframe()
151+
try:
152+
# Go up the call stack to find the actual caller
153+
caller_frame = frame.f_back
154+
155+
# If the caller is a wrapper function (like in update.py), go one more level up
156+
if caller_frame and caller_frame.f_code.co_name == 'log_message':
157+
caller_frame = caller_frame.f_back
158+
159+
if caller_frame:
160+
filename = caller_frame.f_code.co_filename
161+
line_number = caller_frame.f_lineno
162+
# Get just the filename without the full path
163+
filename = filename.split('/')[-1]
164+
caller_info = f"{filename}:{line_number}"
165+
166+
# Format message with caller info
167+
formatted_message = f"[{caller_info}] {message}"
168+
else:
169+
# Fallback if we can't get caller info
170+
formatted_message = message
171+
172+
# Map custom levels to standard ones
173+
if level == "BUFFER":
174+
logger.info(f"[BUFFER] {formatted_message}")
175+
elif level == "FILTER":
176+
logger.debug(f"[FILTER] {formatted_message}")
177+
elif level.upper() == "DEBUG":
178+
logger.debug(formatted_message)
179+
else:
180+
getattr(logger, level.lower(), logger.info)(formatted_message)
181+
finally:
182+
del frame
157183

158184
def is_logging_enabled() -> bool:
159185
"""Check if logging is enabled."""

talkito/update.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class TalkitoUpdater:
4444

4545
GITHUB_REPO_URL = "https://github.com/robdmac/talkito"
4646
UPDATE_CHECK_URL = "https://talkito-app-updates.robbomacrae.workers.dev/check-update"
47-
UPDATE_CHECK_INTERVAL = 3600*24 # Check every day
4847

4948
def __init__(self):
5049
self.current_version = __version__
@@ -255,16 +254,6 @@ def _save_state(self, state):
255254
except Exception as e:
256255
log_message("ERROR", f"Failed to save update state: {e}")
257256

258-
def should_check_for_updates(self):
259-
"""Check if enough time has passed since last update check"""
260-
if os.environ.get('TALKITO_AUTO_UPDATE', 'true').lower() == 'false':
261-
return False
262-
263-
state = self._load_state()
264-
last_check = state.get('last_check', 0)
265-
now = time.time()
266-
267-
return (now - last_check) > self.UPDATE_CHECK_INTERVAL
268257

269258
def stage_update(self, version):
270259
"""Download and stage an update for next restart"""
@@ -345,31 +334,28 @@ def apply_staged_update(self):
345334
log_message("ERROR", f"Failed to apply staged update: {e}")
346335
return False
347336

348-
def _background_check_loop(self):
349-
"""Background thread that periodically checks for updates"""
350-
while not self._stop_event.is_set():
351-
try:
352-
if self.should_check_for_updates():
353-
latest_version, update_available = self.check_for_updates()
354-
355-
if update_available:
356-
log_message("INFO", f"Update available: {latest_version}")
357-
# Stage the update in background
358-
if self.stage_update(latest_version):
359-
log_message("INFO", f"Update {latest_version} staged for next restart")
360-
361-
# Update last check time
362-
state = self._load_state()
363-
state['last_check'] = time.time()
364-
self._save_state(state)
365-
366-
except Exception as e:
367-
log_message("ERROR", f"Error in background update check: {e}")
337+
def _background_check_once(self):
338+
"""Background thread that checks for updates once on startup"""
339+
try:
340+
log_message("INFO", f"Checking for updates (current: {self.current_version}, method: {self.get_update_method()})")
341+
latest_version, update_available = self.check_for_updates()
342+
343+
if latest_version:
344+
if update_available:
345+
log_message("INFO", f"Update available: {self.current_version} -> {latest_version}")
346+
# Stage the update in background
347+
if self.stage_update(latest_version):
348+
log_message("INFO", f"Update {latest_version} staged for next restart")
349+
else:
350+
log_message("INFO", f"Up to date (current: {self.current_version}, latest: {latest_version})")
368351

369-
# Sleep for a while, but check stop event regularly
370-
for _ in range(60): # Check every minute
371-
if self._stop_event.wait(60):
372-
break
352+
# Update last check time
353+
state = self._load_state()
354+
state['last_check'] = time.time()
355+
self._save_state(state)
356+
357+
except Exception as e:
358+
log_message("ERROR", f"Error in background update check: {e}")
373359

374360
def start_background_updates(self):
375361
"""Start background update checking"""
@@ -379,7 +365,7 @@ def start_background_updates(self):
379365

380366
if self._background_thread is None or not self._background_thread.is_alive():
381367
self._background_thread = threading.Thread(
382-
target=self._background_check_loop,
368+
target=self._background_check_once,
383369
daemon=True,
384370
name="TalkitoUpdateChecker"
385371
)
@@ -406,6 +392,25 @@ def check_and_apply_staged_update():
406392
return False
407393

408394

395+
def check_for_updates_now():
396+
"""Do an immediate update check and log the result"""
397+
try:
398+
updater = TalkitoUpdater()
399+
log_message("INFO", f"Immediate update check (current: {updater.current_version}, method: {updater.get_update_method()})")
400+
latest_version, update_available = updater.check_for_updates()
401+
402+
if latest_version:
403+
if update_available:
404+
log_message("INFO", f"Update available: {updater.current_version} -> {latest_version}")
405+
else:
406+
log_message("INFO", f"Up to date (current: {updater.current_version}, latest: {latest_version})")
407+
408+
return latest_version, update_available
409+
except Exception as e:
410+
log_message("ERROR", f"Failed immediate update check: {e}")
411+
return None, False
412+
413+
409414
def start_background_update_checker():
410415
"""Start the background update checker (called on startup)"""
411416
try:

0 commit comments

Comments
 (0)