esphome/esphomeyaml/components/logger.py

62 lines
2.3 KiB
Python
Raw Normal View History

2018-04-07 01:23:03 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
2018-06-02 22:22:20 +02:00
from esphomeyaml.const import CONF_BAUD_RATE, CONF_ID, CONF_LEVEL, CONF_LOGS, \
CONF_TX_BUFFER_SIZE
2018-04-07 01:23:03 +02:00
from esphomeyaml.core import ESPHomeYAMLError
2018-05-20 12:41:52 +02:00
from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, global_ns
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
LOG_LEVELS = {
'NONE': global_ns.ESPHOMELIB_LOG_LEVEL_NONE,
'ERROR': global_ns.ESPHOMELIB_LOG_LEVEL_ERROR,
'WARN': global_ns.ESPHOMELIB_LOG_LEVEL_WARN,
'INFO': global_ns.ESPHOMELIB_LOG_LEVEL_INFO,
'DEBUG': global_ns.ESPHOMELIB_LOG_LEVEL_DEBUG,
'VERBOSE': global_ns.ESPHOMELIB_LOG_LEVEL_VERBOSE,
'VERY_VERBOSE': global_ns.ESPHOMELIB_LOG_LEVEL_VERY_VERBOSE,
}
LOG_LEVEL_SEVERITY = ['NONE', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'VERBOSE', 'VERY_VERBOSE']
2018-04-07 01:23:03 +02:00
# pylint: disable=invalid-name
2018-05-20 12:41:52 +02:00
is_log_level = vol.All(vol.Upper, cv.one_of(*LOG_LEVELS))
def validate_local_no_higher_than_global(value):
global_level = value.get(CONF_LEVEL, 'DEBUG')
for tag, level in value.get(CONF_LOGS, {}).iteritems():
if LOG_LEVEL_SEVERITY.index(level) > LOG_LEVEL_SEVERITY.index(global_level):
raise ESPHomeYAMLError(u"The local log level {} for {} must be less severe than the "
u"global log level {}.".format(level, tag, global_level))
return value
2018-04-07 01:23:03 +02:00
2018-06-02 22:22:20 +02:00
LogComponent = esphomelib_ns.LogComponent
2018-05-20 12:41:52 +02:00
CONFIG_SCHEMA = vol.All(vol.Schema({
2018-06-02 22:22:20 +02:00
cv.GenerateID(): cv.declare_variable_id(LogComponent),
vol.Optional(CONF_BAUD_RATE, default=115200): cv.positive_int,
vol.Optional(CONF_TX_BUFFER_SIZE): cv.validate_bytes,
2018-04-07 01:23:03 +02:00
vol.Optional(CONF_LEVEL): is_log_level,
vol.Optional(CONF_LOGS): vol.Schema({
cv.string: is_log_level,
})
2018-05-20 12:41:52 +02:00
}), validate_local_no_higher_than_global)
2018-04-07 01:23:03 +02:00
def to_code(config):
rhs = App.init_log(config.get(CONF_BAUD_RATE))
2018-06-02 22:22:20 +02:00
log = Pvariable(config[CONF_ID], rhs)
2018-04-07 01:23:03 +02:00
if CONF_TX_BUFFER_SIZE in config:
add(log.set_tx_buffer_size(config[CONF_TX_BUFFER_SIZE]))
if CONF_LEVEL in config:
2018-05-20 12:41:52 +02:00
add(log.set_global_log_level(LOG_LEVELS[config[CONF_LEVEL]]))
2018-04-07 01:23:03 +02:00
for tag, level in config.get(CONF_LOGS, {}).iteritems():
2018-05-20 12:41:52 +02:00
add(log.set_log_level(tag, LOG_LEVELS[level]))
2018-04-07 01:23:03 +02:00
def required_build_flags(config):
2018-04-07 01:23:03 +02:00
if CONF_LEVEL in config:
2018-05-20 12:41:52 +02:00
return u'-DESPHOMELIB_LOG_LEVEL={}'.format(str(LOG_LEVELS[config[CONF_LEVEL]]))
return None