mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-10 05:37:16 +00:00
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
import logging
|
|
|
|
from tfw.networking import Scope
|
|
|
|
from .inotify import InotifyObserver
|
|
from .supervisor import LogManager
|
|
|
|
|
|
class LogInotifyObserver(InotifyObserver, LogManager):
|
|
def __init__(self, server_connector, supervisor_uri, process_name, log_tail=0):
|
|
self._prevent_log_recursion()
|
|
self._server_connector = server_connector
|
|
self._process_name = process_name
|
|
self.log_tail = log_tail
|
|
self._procinfo = None
|
|
LogManager.__init__(self, supervisor_uri)
|
|
InotifyObserver.__init__(self, self._get_logfiles())
|
|
|
|
@staticmethod
|
|
def _prevent_log_recursion():
|
|
# This is done to prevent inotify event logs triggering themselves (infinite log recursion)
|
|
logging.getLogger('watchdog.observers.inotify_buffer').propagate = False
|
|
|
|
def _get_logfiles(self):
|
|
self._procinfo = self.supervisor.getProcessInfo(self._process_name)
|
|
return self._procinfo['stdout_logfile'], self._procinfo['stderr_logfile']
|
|
|
|
@property
|
|
def process_name(self):
|
|
return self._process_name
|
|
|
|
@process_name.setter
|
|
def process_name(self, process_name):
|
|
self._process_name = process_name
|
|
self.paths = self._get_logfiles()
|
|
|
|
def on_modified(self, event):
|
|
self._server_connector.send_message({
|
|
'key': 'processlog',
|
|
'data': {
|
|
'command': 'new_log',
|
|
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
|
|
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
|
|
}
|
|
}, Scope.BROADCAST)
|