Skip to content

Commit

Permalink
core: rotate log file at every restart (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerlz authored Mar 30, 2024
1 parent 3405a4c commit 143db0d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ If I want my printer to light itself on fire, I should be able to make my printe

- [core: danger_options](https://github.com/DangerKlippers/danger-klipper/pull/67)

- [core: rotate log file at every restart](https://github.com/DangerKlippers/danger-klipper/pull/181)

- [fan: normalising Fan PWM power](https://github.com/DangerKlippers/danger-klipper/pull/44) ([klipper#6307](https://github.com/Klipper3d/klipper/pull/6307))

- [fan: reverse FAN](https://github.com/DangerKlippers/danger-klipper/pull/51) ([klipper#4983](https://github.com/Klipper3d/klipper/pull/4983))
Expand Down
15 changes: 14 additions & 1 deletion klippy/klippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ def main():
dest="logfile",
help="write log to file instead of stderr",
)
opts.add_option(
"--rotate-log-at-restart",
action="store_true",
help="rotate the log file at every restart",
)
opts.add_option(
"-v", action="store_true", dest="verbose", help="enable debug messages"
)
Expand Down Expand Up @@ -485,7 +490,13 @@ def main():
bglogger = None
if options.logfile:
start_args["log_file"] = options.logfile
bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel)
bglogger = queuelogger.setup_bg_logging(
filename=options.logfile,
debuglevel=debuglevel,
rotate_log_at_restart=options.rotate_log_at_restart,
)
if options.rotate_log_at_restart:
bglogger.doRollover()
else:
logging.getLogger().setLevel(debuglevel)
logging.info("=======================")
Expand Down Expand Up @@ -532,6 +543,8 @@ def main():
main_reactor = printer = None
logging.info("Restarting printer")
start_args["start_reason"] = res
if options.rotate_log_at_restart and bglogger is not None:
bglogger.doRollover()

if bglogger is not None:
bglogger.stop()
Expand Down
20 changes: 14 additions & 6 deletions klippy/queuelogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging, logging.handlers, threading, queue, time


# Class to forward all messages through a queue to a background thread
class QueueHandler(logging.Handler):
def __init__(self, queue):
Expand All @@ -24,10 +25,15 @@ def emit(self, record):

# Class to poll a queue in a background thread and log each message
class QueueListener(logging.handlers.TimedRotatingFileHandler):
def __init__(self, filename):
logging.handlers.TimedRotatingFileHandler.__init__(
self, filename, when="midnight", backupCount=5
)
def __init__(self, filename, rotate_log_at_restart):
if rotate_log_at_restart:
logging.handlers.TimedRotatingFileHandler.__init__(
self, filename, when="S", interval=60 * 60 * 24, backupCount=5
)
else:
logging.handlers.TimedRotatingFileHandler.__init__(
self, filename, when="midnight", backupCount=5
)
self.bg_queue = queue.Queue()
self.bg_thread = threading.Thread(target=self._bg_thread)
self.bg_thread.start()
Expand Down Expand Up @@ -72,9 +78,11 @@ def doRollover(self):
MainQueueHandler = None


def setup_bg_logging(filename, debuglevel):
def setup_bg_logging(filename, debuglevel, rotate_log_at_restart):
global MainQueueHandler
ql = QueueListener(filename)
ql = QueueListener(
filename=filename, rotate_log_at_restart=rotate_log_at_restart
)
MainQueueHandler = QueueHandler(ql.bg_queue)
root = logging.getLogger()
root.addHandler(MainQueueHandler)
Expand Down

0 comments on commit 143db0d

Please sign in to comment.