Implement the log() function.

Refs #5846
This commit is contained in:
Gunnar Beutner 2014-03-22 09:47:29 +01:00
parent 3afad7a96d
commit ddbbd42c4b
5 changed files with 9 additions and 119 deletions

View File

@ -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.
### <a id="operators"></a> Dictionary Operators

View File

@ -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.");
}
}

View File

@ -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<AExpression>& expr);
AValue(AValueType type, const Value& value);
Value Evaluate(const Dictionary::Ptr& locals) const;
private:
AValueType m_Type;
Value m_Value;
shared_ptr<AExpression> m_Expression;
};
}
#endif /* AVALUE_H */

View File

@ -23,6 +23,7 @@
#include "base/convert.h"
#include "base/array.h"
#include "base/dictionary.h"
#include "base/logger_fwd.h"
#include <boost/regex.hpp>
#include <algorithm>
#include <set>
@ -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<Value>& arguments)
return result;
}
void UtilityFuncs::Log(const String& message)
{
::Log(LogInformation, "config", message);
}

View File

@ -37,6 +37,7 @@ public:
static int Len(const Value& value);
static Array::Ptr Union(const std::vector<Value>& arguments);
static Array::Ptr Intersection(const std::vector<Value>& arguments);
static void Log(const String& message);
private:
UtilityFuncs(void);