mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
disk io:logging improvements
This commit is contained in:
@@ -40,26 +40,36 @@ def mylog(requestedDebugLevel, n):
|
|||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# Queue for log messages
|
# Queue for log messages
|
||||||
log_queue = queue.Queue()
|
log_queue = queue.Queue(maxsize=1000) # Increase size to handle spikes
|
||||||
|
|
||||||
# Dedicated thread for writing logs
|
# Dedicated thread for writing logs
|
||||||
log_thread = None # Will hold the thread reference
|
log_thread = None # Will hold the thread reference
|
||||||
|
|
||||||
def log_writer():
|
def log_writer():
|
||||||
|
buffer = []
|
||||||
while True:
|
while True:
|
||||||
log_entry = log_queue.get()
|
try:
|
||||||
if log_entry is None: # Graceful exit signal
|
log_entry = log_queue.get(timeout=1) # Wait for 1 second for logs
|
||||||
break
|
if log_entry is None: # Graceful exit signal
|
||||||
with open(logPath + "/app.log", 'a') as log_file:
|
break
|
||||||
log_file.write(log_entry + '\n')
|
buffer.append(log_entry)
|
||||||
|
if len(buffer) >= 10: # Write in batches of 10
|
||||||
|
with open(logPath + "/app.log", 'a') as log_file:
|
||||||
|
log_file.write('\n'.join(buffer) + '\n')
|
||||||
|
buffer.clear()
|
||||||
|
except queue.Empty:
|
||||||
|
# Flush buffer periodically if no new logs
|
||||||
|
if buffer:
|
||||||
|
with open(logPath + "/app.log", 'a') as log_file:
|
||||||
|
log_file.write('\n'.join(buffer) + '\n')
|
||||||
|
buffer.clear()
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# Function to start the log writer thread if it doesn't exist
|
# Function to start the log writer thread if it doesn't exist
|
||||||
def start_log_writer_thread():
|
def start_log_writer_thread():
|
||||||
global log_thread
|
global log_thread
|
||||||
if log_thread is None or not log_thread.is_alive():
|
if log_thread is None or not log_thread.is_alive():
|
||||||
print("Starting log writer thread...")
|
log_thread = threading.Thread(target=log_writer, daemon=True)
|
||||||
log_thread = threading.Thread(target=log_writer, args=(), daemon=True)
|
|
||||||
log_thread.start()
|
log_thread.start()
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
@@ -80,9 +90,9 @@ def file_print(*args):
|
|||||||
# Function to append to the file with a timeout
|
# Function to append to the file with a timeout
|
||||||
def append_to_file_with_timeout(data, timeout):
|
def append_to_file_with_timeout(data, timeout):
|
||||||
try:
|
try:
|
||||||
log_queue.put(data, timeout=timeout)
|
log_queue.put_nowait(data)
|
||||||
except queue.Full:
|
except queue.Full:
|
||||||
print("Appending to file timed out")
|
print("Log queue is full, dropping log entry:" + data)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def print_log(pText):
|
def print_log(pText):
|
||||||
|
|||||||
Reference in New Issue
Block a user