Skip to content

Commit 997766d

Browse files
committed
Fix logfile becoming corrupted when multiple RWP instances opened
Also fixed comtypes spam filter
1 parent f94f2dc commit 997766d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import ctypes
22
import logging
3+
import logging.handlers
34
import os
45
import signal
56
import sys
67
import time
78
from typing import Optional
89

9-
import named_pipe
1010
import psutil
1111
import win32con
1212
import win32gui
1313
import wx
14+
15+
import named_pipe
1416
from common import Window, XandY, load_json, local_path, match, single_call
1517
from device import DeviceChangeCallback, DeviceChangeService
1618
from gui import TaskbarIcon, WxApp, about_dialog, radio_menu
@@ -25,7 +27,7 @@ def filter(self, record):
2527
try:
2628
return not (
2729
# sometimes the record has msg, sometimes its message. Just try to catch all of them
28-
'Release ' in getattr(record, 'message', getattr(record, 'msg', '')) and record.name == 'comtypes'
30+
'Release ' in getattr(record, 'message', getattr(record, 'msg', '')) and record.name.startswith('comtypes')
2931
)
3032
except Exception:
3133
# sometimes (rarely) record doesn't have a `message` attr
@@ -213,12 +215,16 @@ def shutdown(*_):
213215

214216
if __name__ == '__main__':
215217
ctypes.windll.shcore.SetProcessDpiAwareness(2)
216-
logging.basicConfig(
217-
filename=local_path('log.txt'), filemode='w',
218-
format='[%(process)d] %(asctime)s:%(levelname)s:%(name)s:%(message)s'
218+
log_handler = logging.handlers.RotatingFileHandler(
219+
local_path('log.txt'), mode='a', maxBytes=1 * 1024 * 1024, backupCount=2, encoding='utf-8', delay=False
219220
)
220221
# filter the excessive comtypes logs
221-
logging.getLogger('comtypes').addFilter(LoggingFilter())
222+
log_handler.addFilter(LoggingFilter())
223+
logging.basicConfig(
224+
format='[%(process)d] %(asctime)s:%(levelname)s:%(name)s:%(message)s',
225+
handlers=[log_handler],
226+
level=logging.INFO
227+
)
222228
log = logging.getLogger(__name__)
223229
log.info('start')
224230

0 commit comments

Comments
 (0)