Skip to content

Commit

Permalink
feat: add option to limit log file size
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Jul 2, 2022
1 parent d80bec3 commit 71c4fdc
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 9 deletions.
21 changes: 16 additions & 5 deletions gridplayer/dialogs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ def __init__(self, parent):
"misc/mouse_hide_timeout": self.timeoutMouseHide,
"logging/log_level": self.logLevel,
"logging/log_level_vlc": self.logLevelVLC,
"logging/log_limit": self.logLimit,
"logging/log_limit_size": self.logLimitSize,
"logging/log_limit_backups": self.logLimitBackups,
"internal/opaque_hw_overlay": self.miscOpaqueHWOverlay,
}

self.ui_customize()
self.ui_fill()

self.ui_connect()
self.ui_set_limits()

self.load_settings()

Expand Down Expand Up @@ -145,18 +149,23 @@ def ui_fill(self):
self.fill_streamQuality()
self.fill_playlistSeekSyncMode()

def ui_customize_dynamic(self):
self.driver_selected(self.playerVideoDriver.currentIndex())
self.timeoutMouseHide.setEnabled(self.timeoutMouseHideFlag.isChecked())
self.timeoutOverlay.setEnabled(self.timeoutOverlayFlag.isChecked())

def ui_set_limits(self):
self.playerVideoDriverPlayers.setRange(1, MAX_VLC_PROCESSES)
self.timeoutOverlay.setRange(1, 60)
self.timeoutMouseHide.setRange(1, 60)
self.logLimitSize.setRange(1, 1024 * 1024)
self.logLimitBackups.setRange(1, 1000)

self.gridSize.setRange(0, 1000)
self.gridSize.setSpecialValueText(self.tr("Auto"))

def ui_customize_dynamic(self):
self.driver_selected(self.playerVideoDriver.currentIndex())
self.timeoutMouseHide.setEnabled(self.timeoutMouseHideFlag.isChecked())
self.timeoutOverlay.setEnabled(self.timeoutOverlayFlag.isChecked())
self.logLimitSize.setEnabled(self.logLimit.isChecked())
self.logLimitBackups.setEnabled(self.logLimit.isChecked())

self.switch_page(None)

def ui_connect(self):
Expand All @@ -167,6 +176,8 @@ def ui_connect(self):
(self.logFileOpen.clicked, self.open_logfile),
(self.section_index.currentTextChanged, self.switch_page),
(self.section_index.itemSelectionChanged, self.keep_index_selection),
(self.logLimit.stateChanged, self.logLimitSize.setEnabled),
(self.logLimit.stateChanged, self.logLimitBackups.setEnabled),
)

def keep_index_selection(self):
Expand Down
51 changes: 50 additions & 1 deletion gridplayer/dialogs/settings_dialog_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,48 @@ def setupUi(self, SettingsDialog):
self.lay_page_misc_logging = QtWidgets.QVBoxLayout(self.page_misc_logging)
self.lay_page_misc_logging.setContentsMargins(0, 0, 0, 0)
self.lay_page_misc_logging.setObjectName("lay_page_misc_logging")
self.logLimit = QtWidgets.QCheckBox(self.page_misc_logging)
self.logLimit.setObjectName("logLimit")
self.lay_page_misc_logging.addWidget(self.logLimit)
self.formLayout_6 = QtWidgets.QFormLayout()
self.formLayout_6.setFieldGrowthPolicy(
QtWidgets.QFormLayout.FieldsStayAtSizeHint
)
self.formLayout_6.setObjectName("formLayout_6")
self.logLimitSizeLabel = QtWidgets.QLabel(self.page_misc_logging)
self.logLimitSizeLabel.setObjectName("logLimitSizeLabel")
self.formLayout_6.setWidget(
0, QtWidgets.QFormLayout.LabelRole, self.logLimitSizeLabel
)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.logLimitSize = QtWidgets.QSpinBox(self.page_misc_logging)
self.logLimitSize.setObjectName("logLimitSize")
self.horizontalLayout_3.addWidget(self.logLimitSize)
self.label_5 = QtWidgets.QLabel(self.page_misc_logging)
self.label_5.setObjectName("label_5")
self.horizontalLayout_3.addWidget(self.label_5)
self.formLayout_6.setLayout(
0, QtWidgets.QFormLayout.FieldRole, self.horizontalLayout_3
)
self.logLimitBackupsLabel = QtWidgets.QLabel(self.page_misc_logging)
self.logLimitBackupsLabel.setObjectName("logLimitBackupsLabel")
self.formLayout_6.setWidget(
1, QtWidgets.QFormLayout.LabelRole, self.logLimitBackupsLabel
)
self.logLimitBackups = QtWidgets.QSpinBox(self.page_misc_logging)
self.logLimitBackups.setObjectName("logLimitBackups")
self.formLayout_6.setWidget(
1, QtWidgets.QFormLayout.FieldRole, self.logLimitBackups
)
self.lay_page_misc_logging.addLayout(self.formLayout_6)
self.label_6 = QtWidgets.QLabel(self.page_misc_logging)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.lay_page_misc_logging.addWidget(self.label_6)
self.formLayout_5 = QtWidgets.QFormLayout()
self.formLayout_5.setFieldGrowthPolicy(
QtWidgets.QFormLayout.FieldsStayAtSizeHint
Expand Down Expand Up @@ -372,7 +414,7 @@ def setupUi(self, SettingsDialog):
self.lay_main.setStretch(0, 1)

self.retranslateUi(SettingsDialog)
self.section_page.setCurrentIndex(1)
self.section_page.setCurrentIndex(5)
self.buttonBox.accepted.connect(SettingsDialog.accept)
self.buttonBox.rejected.connect(SettingsDialog.reject)
QtCore.QMetaObject.connectSlotsByName(SettingsDialog)
Expand Down Expand Up @@ -472,6 +514,13 @@ def retranslateUi(self, SettingsDialog):
self.miscOpaqueHWOverlay.setText(
_translate("SettingsDialog", "Opaque overlay (fix black screen)")
)
self.logLimit.setText(_translate("SettingsDialog", "Limit log file size"))
self.logLimitSizeLabel.setText(_translate("SettingsDialog", "Log file size"))
self.label_5.setText(_translate("SettingsDialog", "MB"))
self.logLimitBackupsLabel.setText(
_translate("SettingsDialog", "Log files to keep")
)
self.label_6.setText(_translate("SettingsDialog", "Logging levels"))
self.logLevelLabel.setText(_translate("SettingsDialog", "Log level"))
self.logLevelVLCLabel.setText(_translate("SettingsDialog", "Log level (VLC)"))
self.logFileOpen.setText(_translate("SettingsDialog", "Open log file"))
Expand Down
14 changes: 13 additions & 1 deletion gridplayer/main/init_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
def init_log():
log_path = get_app_data_dir() / f"{__app_name__.lower()}.log"

log_config.config_log(log_path, Settings().get("logging/log_level"))
if Settings().get("logging/log_limit"):
max_log_size = max(Settings().get("logging/log_limit_size"), 1) * 1024 * 1024
max_log_backups = max(Settings().get("logging/log_limit_backups"), 1)
else:
max_log_size = None
max_log_backups = None

log_config.config_log(
log_path=log_path,
log_level=Settings().get("logging/log_level"),
max_log_size=max_log_size,
max_log_backups=max_log_backups,
)

log_qt = log_config.QtLogHandler()
qInstallMessageHandler(log_qt.handle)
3 changes: 3 additions & 0 deletions gridplayer/player/managers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def _is_reload_needed(self, previous_settings):
def _is_restart_needed(self, previous_settings):
checks = {
"player/language",
"logging/log_limit",
"logging/log_limit_size",
"logging/log_limit_backups",
}

return self._setting_changes(previous_settings, checks)
Expand Down
3 changes: 3 additions & 0 deletions gridplayer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def default_language():
"misc/mouse_hide_timeout": 5,
"logging/log_level": logging.WARNING,
"logging/log_level_vlc": DISABLED,
"logging/log_limit": False,
"logging/log_limit_size": 10,
"logging/log_limit_backups": 1,
"internal/opaque_hw_overlay": False,
}

Expand Down
13 changes: 12 additions & 1 deletion gridplayer/utils/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from logging.config import dictConfig
from logging.handlers import QueueHandler, QueueListener
from pathlib import Path
from typing import Optional

from PyQt5 import QtCore

Expand Down Expand Up @@ -68,7 +69,12 @@ def handle(self, mode, context, message):
self._log.log(log_level, message.strip())


def config_log(log_path: Path, log_level: int):
def config_log(
log_path: Path,
log_level: int,
max_log_size: Optional[int] = None,
max_log_backups: Optional[int] = None,
):
config = {
"version": 1,
"formatters": {
Expand Down Expand Up @@ -103,6 +109,11 @@ def config_log(log_path: Path, log_level: int):
},
}

if max_log_size is not None and max_log_backups is not None:
config["handlers"]["file"]["class"] = "logging.handlers.RotatingFileHandler"
config["handlers"]["file"]["maxBytes"] = max_log_size
config["handlers"]["file"]["backupCount"] = max_log_backups

dictConfig(config)


Expand Down
60 changes: 59 additions & 1 deletion resources/ui/settings_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
</size>
</property>
<property name="currentIndex">
<number>1</number>
<number>5</number>
</property>
<widget class="QWidget" name="page_general_player">
<layout class="QVBoxLayout" name="lay_section_player">
Expand Down Expand Up @@ -610,6 +610,64 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="logLimit">
<property name="text">
<string>Limit log file size</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_6">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::FieldsStayAtSizeHint</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="logLimitSizeLabel">
<property name="text">
<string>Log file size</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QSpinBox" name="logLimitSize"/>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>MB</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="logLimitBackupsLabel">
<property name="text">
<string>Log files to keep</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="logLimitBackups"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_6">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Logging levels</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_5">
<property name="fieldGrowthPolicy">
Expand Down

0 comments on commit 71c4fdc

Please sign in to comment.