This commit is contained in:
Fabian Affolter 2018-01-19 07:36:48 +01:00 committed by Paulus Schoutsen
parent 7fe2dafa04
commit b1fd9daf5f

View File

@ -1,36 +1,35 @@
""" """
Support for system log. Support for system log.
For more details about this platform, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/system_log/ https://home-assistant.io/components/system_log/
""" """
import re
import asyncio import asyncio
import logging
import traceback
from io import StringIO
from collections import deque from collections import deque
from io import StringIO
import logging
import re
import traceback
import voluptuous as vol import voluptuous as vol
from homeassistant import __path__ as HOMEASSISTANT_PATH from homeassistant import __path__ as HOMEASSISTANT_PATH
import homeassistant.helpers.config_validation as cv
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
import homeassistant.helpers.config_validation as cv
DOMAIN = 'system_log'
DEPENDENCIES = ['http']
SERVICE_CLEAR = 'clear'
CONF_MAX_ENTRIES = 'max_entries' CONF_MAX_ENTRIES = 'max_entries'
DEFAULT_MAX_ENTRIES = 50
DATA_SYSTEM_LOG = 'system_log' DATA_SYSTEM_LOG = 'system_log'
DEFAULT_MAX_ENTRIES = 50
DEPENDENCIES = ['http']
DOMAIN = 'system_log'
SERVICE_CLEAR = 'clear'
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({ DOMAIN: vol.Schema({
vol.Optional(CONF_MAX_ENTRIES, vol.Optional(CONF_MAX_ENTRIES, default=DEFAULT_MAX_ENTRIES):
default=DEFAULT_MAX_ENTRIES): cv.positive_int, cv.positive_int,
}), }),
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
@ -48,9 +47,9 @@ class LogErrorHandler(logging.Handler):
def emit(self, record): def emit(self, record):
"""Save error and warning logs. """Save error and warning logs.
Everyhing logged with error or warning is saved in local buffer. A Everything logged with error or warning is saved in local buffer. A
default upper limit is set to 50 (older entries are discarded) but can default upper limit is set to 50 (older entries are discarded) but can
be changed if neeeded. be changed if needed.
""" """
if record.levelno >= logging.WARN: if record.levelno >= logging.WARN:
stack = [] stack = []
@ -115,10 +114,10 @@ def _figure_out_source(record, call_stack, hass):
stack = call_stack[0:index+1] stack = call_stack[0:index+1]
# Iterate through the stack call (in reverse) and find the last call from # Iterate through the stack call (in reverse) and find the last call from
# a file in HA. Try to figure out where error happened. # a file in Home Assistant. Try to figure out where error happened.
for pathname in reversed(stack): for pathname in reversed(stack):
# Try to match with a file within HA # Try to match with a file within Home Assistant
match = re.match(r'(?:{})/(.*)'.format('|'.join(paths)), pathname) match = re.match(r'(?:{})/(.*)'.format('|'.join(paths)), pathname)
if match: if match:
return match.group(1) return match.group(1)