From: Gunnar Beutner Date: Sat, 22 Mar 2014 08:47:29 +0000 (+0100) Subject: Implement the log() function. X-Git-Tag: v0.0.9~34^2~19 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ddbbd42c4bbc1f962d6c5ea05b4d77f8a6abe06b;p=icinga2 Implement the log() function. Refs #5846 --- diff --git a/doc/4.1-configuration-syntax.md b/doc/4.1-configuration-syntax.md index f602b5cd8..c644edb9e 100644 --- a/doc/4.1-configuration-syntax.md +++ b/doc/4.1-configuration-syntax.md @@ -200,6 +200,7 @@ intersection(array, array, ...) | Returns an array containing all unique element string(value) | Converts the value to a string. number(value) | Converts the value to a number. bool(value) | Converts to value to a bool. +log(string) | Writes a message to the log. ### Dictionary Operators diff --git a/lib/config/avalue.cpp b/lib/config/avalue.cpp deleted file mode 100644 index 742bbca22..000000000 --- a/lib/config/avalue.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#include "config/avalue.h" -#include "config/aexpression.h" -#include "base/scriptvariable.h" - -using namespace icinga; - -AValue::AValue(void) - : m_Type(ATSimple) -{ } - -AValue::AValue(const AExpression::Ptr& expr) - : m_Type(ATExpression), m_Expression(expr) -{ } - -AValue::AValue(AValueType type, const Value& value) - : m_Type(type), m_Value(value) -{ } - -Value AValue::Evaluate(const Dictionary::Ptr& locals) const -{ - switch (m_Type) { - case ATSimple: - return m_Value; - case ATVariable: - if (locals && locals->Contains(m_Value)) - return locals->Get(m_Value); - else - return ScriptVariable::Get(m_Value); - case ATThisRef: - VERIFY(!"Not implemented."); - case ATExpression: - return m_Expression->Evaluate(locals); - default: - ASSERT(!"Invalid type."); - } -} diff --git a/lib/config/avalue.h b/lib/config/avalue.h deleted file mode 100644 index 37969ca3f..000000000 --- a/lib/config/avalue.h +++ /dev/null @@ -1,64 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef AVALUE_H -#define AVALUE_H - -#include "config/i2-config.h" -#include "base/value.h" -#include "base/dictionary.h" - -namespace icinga -{ - -/** - * @ingroup config - */ -enum AValueType -{ - ATSimple, - ATVariable, - ATThisRef, - ATExpression -}; - -class AExpression; - -/** - * @ingroup config - */ -class I2_CONFIG_API AValue -{ -public: - AValue(void); - AValue(const shared_ptr& expr); - AValue(AValueType type, const Value& value); - - Value Evaluate(const Dictionary::Ptr& locals) const; - -private: - AValueType m_Type; - Value m_Value; - shared_ptr m_Expression; -}; - -} - -#endif /* AVALUE_H */ - diff --git a/lib/methods/utilityfuncs.cpp b/lib/methods/utilityfuncs.cpp index 4a5ae82e5..8b305c7ba 100644 --- a/lib/methods/utilityfuncs.cpp +++ b/lib/methods/utilityfuncs.cpp @@ -23,6 +23,7 @@ #include "base/convert.h" #include "base/array.h" #include "base/dictionary.h" +#include "base/logger_fwd.h" #include #include #include @@ -34,6 +35,7 @@ REGISTER_SCRIPTFUNCTION(match, &Utility::Match); REGISTER_SCRIPTFUNCTION(len, &UtilityFuncs::Len); REGISTER_SCRIPTFUNCTION(union, &UtilityFuncs::Union); REGISTER_SCRIPTFUNCTION(intersection, &UtilityFuncs::Intersection); +REGISTER_SCRIPTFUNCTION(log, &UtilityFuncs::Log); bool UtilityFuncs::Regex(const String& pattern, const String& text) { @@ -98,3 +100,8 @@ Array::Ptr UtilityFuncs::Intersection(const std::vector& arguments) return result; } + +void UtilityFuncs::Log(const String& message) +{ + ::Log(LogInformation, "config", message); +} diff --git a/lib/methods/utilityfuncs.h b/lib/methods/utilityfuncs.h index 4dbbb57f4..4bf55920d 100644 --- a/lib/methods/utilityfuncs.h +++ b/lib/methods/utilityfuncs.h @@ -37,6 +37,7 @@ public: static int Len(const Value& value); static Array::Ptr Union(const std::vector& arguments); static Array::Ptr Intersection(const std::vector& arguments); + static void Log(const String& message); private: UtilityFuncs(void);