From e4f055597cf4f9f9432e8495c6e4a26c581d5d26 Mon Sep 17 00:00:00 2001 From: Nikolay Vasilchuk Date: Thu, 24 Oct 2019 15:19:33 +0300 Subject: [PATCH] Logger on_message trigger (#729) * on_message * Lint fix * Lint fix (2) * Lint fix (<3) * Replace cg.int_ with int * Revert * Removed strdup Co-authored-by: Otto Winter --- esphome/codegen.py | 2 +- esphome/components/logger/__init__.py | 16 +++++++++++++++- esphome/components/logger/logger.h | 16 ++++++++++++++++ esphome/cpp_types.py | 1 + 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/esphome/codegen.py b/esphome/codegen.py index 46d652a3cd..c30b43f952 100644 --- a/esphome/codegen.py +++ b/esphome/codegen.py @@ -19,7 +19,7 @@ from esphome.cpp_helpers import ( # noqa gpio_pin_expression, register_component, build_registry_entry, build_registry_list, extract_registry_entry_config, register_parented) from esphome.cpp_types import ( # noqa - global_ns, void, nullptr, float_, double, bool_, std_ns, std_string, + global_ns, void, nullptr, float_, double, bool_, int_, std_ns, std_string, std_vector, uint8, uint16, uint32, int32, const_char_ptr, NAN, esphome_ns, App, Nameable, Component, ComponentPtr, PollingComponent, Application, optional, arduino_json_ns, JsonObject, diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index 3e07334313..850f955f65 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -5,7 +5,7 @@ import esphome.config_validation as cv from esphome import automation from esphome.automation import LambdaAction from esphome.const import CONF_ARGS, CONF_BAUD_RATE, CONF_FORMAT, CONF_HARDWARE_UART, CONF_ID, \ - CONF_LEVEL, CONF_LOGS, CONF_TAG, CONF_TX_BUFFER_SIZE + CONF_LEVEL, CONF_LOGS, CONF_ON_MESSAGE, CONF_TAG, CONF_TRIGGER_ID, CONF_TX_BUFFER_SIZE from esphome.core import CORE, EsphomeError, Lambda, coroutine_with_priority from esphome.py_compat import text_type @@ -70,6 +70,9 @@ def validate_local_no_higher_than_global(value): Logger = logger_ns.class_('Logger', cg.Component) +LoggerMessageTrigger = logger_ns.class_('LoggerMessageTrigger', + automation.Trigger.template(cg.int_, cg.const_char_ptr, + cg.const_char_ptr)) CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH = 'esp8266_store_log_strings_in_flash' CONFIG_SCHEMA = cv.All(cv.Schema({ @@ -81,6 +84,10 @@ CONFIG_SCHEMA = cv.All(cv.Schema({ cv.Optional(CONF_LOGS, default={}): cv.Schema({ cv.string: is_log_level, }), + cv.Optional(CONF_ON_MESSAGE): automation.validate_automation({ + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LoggerMessageTrigger), + cv.Optional(CONF_LEVEL, default='WARN'): is_log_level, + }), cv.SplitDefault(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH, esp8266=True): cv.All(cv.only_on_esp8266, cv.boolean), @@ -138,6 +145,13 @@ def to_code(config): # Register at end for safe mode yield cg.register_component(log, config) + for conf in config.get(CONF_ON_MESSAGE, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], log, + LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL])) + yield automation.build_automation(trigger, [(cg.int_, 'level'), + (cg.const_char_ptr, 'tag'), + (cg.const_char_ptr, 'message')], conf) + def maybe_simple_message(schema): def validator(value): diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 18196b68d2..3d129e335b 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -1,5 +1,6 @@ #pragma once +#include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" @@ -114,6 +115,21 @@ class Logger : public Component { extern Logger *global_logger; +class LoggerMessageTrigger : public Trigger { + public: + explicit LoggerMessageTrigger(Logger *parent, int level) { + this->level_ = level; + parent->add_on_log_callback([this](int level, const char *tag, const char *message) { + if (level <= this->level_) { + this->trigger(level, tag, message); + } + }); + } + + protected: + int level_; +}; + } // namespace logger } // namespace esphome diff --git a/esphome/cpp_types.py b/esphome/cpp_types.py index d3e5b2d561..4a9dce332b 100644 --- a/esphome/cpp_types.py +++ b/esphome/cpp_types.py @@ -6,6 +6,7 @@ nullptr = global_ns.namespace('nullptr') float_ = global_ns.namespace('float') double = global_ns.namespace('double') bool_ = global_ns.namespace('bool') +int_ = global_ns.namespace('int') std_ns = global_ns.namespace('std') std_string = std_ns.class_('string') std_vector = std_ns.class_('vector')