update and delete logs older than 24 hours

This commit is contained in:
2025-03-27 10:02:05 +02:00
parent df13e5ca8e
commit 49eb7146b4
2 changed files with 43 additions and 95 deletions

View File

@@ -1,91 +0,0 @@
2025-03-27 08:17:12 - STARTED: car_modular.jpg
2025-03-27 08:17:27 - STOPPED: car_modular.jpg
2025-03-27 08:17:27 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:17:27 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:17:27 - STARTED: harting_contactor.jpg
2025-03-27 08:17:47 - STOPPED: harting_contactor.jpg
2025-03-27 08:17:47 - STARTED: car_modular.jpg
2025-03-27 08:18:07 - STOPPED: car_modular.jpg
2025-03-27 08:18:07 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:20:58 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:20:58 - STARTED: harting_contactor.jpg
2025-03-27 08:21:18 - STOPPED: harting_contactor.jpg
2025-03-27 08:21:18 - STARTED: car_modular.jpg
2025-03-27 08:21:38 - STOPPED: car_modular.jpg
2025-03-27 08:21:38 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:22:09 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:24:29 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:24:29 - STARTED: harting_contactor.jpg
2025-03-27 08:24:49 - STOPPED: harting_contactor.jpg
2025-03-27 08:24:49 - STARTED: car_modular.jpg
2025-03-27 08:24:58 - STOPPED: car_modular.jpg
2025-03-27 08:24:58 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:25:09 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 08:25:09 - STARTED: harting_contactor.jpg
2025-03-27 09:18:55 - STARTED: car_modular.jpg
2025-03-27 09:19:11 - STOPPED: car_modular.jpg
2025-03-27 09:19:11 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:19:12 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:19:12 - STARTED: harting_contactor.jpg
2025-03-27 09:19:31 - STOPPED: harting_contactor.jpg
2025-03-27 09:19:31 - STARTED: car_modular.jpg
2025-03-27 09:19:51 - STOPPED: car_modular.jpg
2025-03-27 09:19:51 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:22:42 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:22:42 - STARTED: harting_contactor.jpg
2025-03-27 09:23:02 - STOPPED: harting_contactor.jpg
2025-03-27 09:23:02 - STARTED: car_modular.jpg
2025-03-27 09:23:22 - STOPPED: car_modular.jpg
2025-03-27 09:23:22 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:26:13 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:26:13 - STARTED: harting_contactor.jpg
2025-03-27 09:26:33 - STOPPED: harting_contactor.jpg
2025-03-27 09:26:33 - STARTED: car_modular.jpg
2025-03-27 09:26:53 - STOPPED: car_modular.jpg
2025-03-27 09:26:53 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:29:44 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:29:44 - STARTED: harting_contactor.jpg
2025-03-27 09:30:04 - STOPPED: harting_contactor.jpg
2025-03-27 09:30:04 - STARTED: car_modular.jpg
2025-03-27 09:30:24 - STOPPED: car_modular.jpg
2025-03-27 09:30:24 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:33:15 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:33:15 - STARTED: harting_contactor.jpg
2025-03-27 09:33:35 - STOPPED: harting_contactor.jpg
2025-03-27 09:33:35 - STARTED: car_modular.jpg
2025-03-27 09:33:55 - STOPPED: car_modular.jpg
2025-03-27 09:33:55 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:36:46 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:36:46 - STARTED: harting_contactor.jpg
2025-03-27 09:37:06 - STOPPED: harting_contactor.jpg
2025-03-27 09:37:06 - STARTED: car_modular.jpg
2025-03-27 09:37:26 - STOPPED: car_modular.jpg
2025-03-27 09:37:26 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:40:17 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:40:17 - STARTED: harting_contactor.jpg
2025-03-27 09:40:37 - STOPPED: harting_contactor.jpg
2025-03-27 09:40:37 - STARTED: car_modular.jpg
2025-03-27 09:40:57 - STOPPED: car_modular.jpg
2025-03-27 09:40:57 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:43:48 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:43:48 - STARTED: harting_contactor.jpg
2025-03-27 09:44:08 - STOPPED: harting_contactor.jpg
2025-03-27 09:44:08 - STARTED: car_modular.jpg
2025-03-27 09:44:28 - STOPPED: car_modular.jpg
2025-03-27 09:44:28 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:47:18 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:47:18 - STARTED: harting_contactor.jpg
2025-03-27 09:47:38 - STOPPED: harting_contactor.jpg
2025-03-27 09:47:38 - STARTED: car_modular.jpg
2025-03-27 09:47:58 - STOPPED: car_modular.jpg
2025-03-27 09:47:58 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:50:49 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:50:49 - STARTED: harting_contactor.jpg
2025-03-27 09:51:09 - STOPPED: harting_contactor.jpg
2025-03-27 09:51:09 - STARTED: car_modular.jpg
2025-03-27 09:51:29 - STOPPED: car_modular.jpg
2025-03-27 09:51:29 - STARTED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:54:20 - STOPPED: SampleVideo_1280x720_30mb.mp4
2025-03-27 09:54:20 - STARTED: harting_contactor.jpg
2025-03-27 09:54:40 - STOPPED: harting_contactor.jpg
2025-03-27 09:54:40 - STARTED: car_modular.jpg

View File

@@ -66,13 +66,52 @@ class MediaPlayer(Screen):
self.play_media() # Start playing media
def log_event(self, file_name, event):
"""Log the start or stop event of a media file."""
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Get the current timestamp
"""Log the start or stop event of a media file and clean up old logs."""
# Get the current timestamp
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_message = f"{timestamp} - {event}: {file_name}\n" # Format the log message
with open(self.log_file, 'a') as log: # Open the log file in append mode
log.write(log_message) # Write the log message
# Write the log message to the log file
with open(self.log_file, 'a') as log:
log.write(log_message)
Logger.info(f"Logged event: {log_message.strip()}") # Log the event to the console
# Clean up logs older than 24 hours
self.cleanup_old_logs()
def cleanup_old_logs(self):
"""Delete log entries older than 24 hours."""
try:
# Read all log entries
if os.path.exists(self.log_file):
with open(self.log_file, 'r') as log:
lines = log.readlines()
# Get the current time
now = datetime.datetime.now()
# Filter out log entries older than 24 hours
filtered_lines = []
for line in lines:
try:
# Extract the timestamp from the log entry
timestamp_str = line.split(' - ')[0]
log_time = datetime.datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
# Keep the log entry if it's within the last 24 hours
if (now - log_time).total_seconds() <= 86400: # 24 hours in seconds
filtered_lines.append(line)
except (ValueError, IndexError):
# If the log entry is malformed, skip it
Logger.warning(f"Malformed log entry skipped: {line.strip()}")
# Write the filtered log entries back to the log file
with open(self.log_file, 'w') as log:
log.writelines(filtered_lines)
Logger.info("Old log entries cleaned up successfully.")
except Exception as e:
Logger.error(f"Failed to clean up old logs: {e}")
def play_media(self):
"""Play the current media in the playlist."""
if self.playlist: