From 2c17305536c596b7b9909a30a66166b5c5621dc9 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 1 Apr 2014 09:33:54 +0200 Subject: [PATCH] Implement additional arguments for log(). Fixes #5902 --- doc/4.1-configuration-syntax.md | 1 + lib/base/logger.cpp | 10 ++++++++++ lib/base/logger.h | 2 ++ lib/base/scriptfunctionwrapper.cpp | 7 +++++++ lib/base/scriptfunctionwrapper.h | 6 ++++++ lib/base/scriptutils.cpp | 24 +++++++++++++++++++++--- lib/base/scriptutils.h | 2 +- 7 files changed, 48 insertions(+), 4 deletions(-) diff --git a/doc/4.1-configuration-syntax.md b/doc/4.1-configuration-syntax.md index 73c3227f9..05ec479e2 100644 --- a/doc/4.1-configuration-syntax.md +++ b/doc/4.1-configuration-syntax.md @@ -205,6 +205,7 @@ 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(value) | Writes a message to the log. Non-string values are converted to a JSON string. +log(severity, facility, value) | Writes a message to the log. `severity` can be one of `LogDebug`, `LogInformation`, `LogWarning` and `LogCritical`. Non-string values are converted to a JSON string. exit(integer) | Terminates the application. ### Dictionary Operators diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 47f2f8bed..b841fbd08 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -25,6 +25,7 @@ #include "base/objectlock.h" #include "base/context.h" #include "base/convert.h" +#include "base/scriptvariable.h" #include #include #include @@ -32,11 +33,20 @@ using namespace icinga; REGISTER_TYPE(Logger); +INITIALIZE_ONCE(&Logger::StaticInitialize); std::set Logger::m_Loggers; boost::mutex Logger::m_Mutex; bool Logger::m_ConsoleLogEnabled = true; +void Logger::StaticInitialize(void) +{ + ScriptVariable::Set("LogDebug", LogDebug, true, true); + ScriptVariable::Set("LogInformation", LogInformation, true, true); + ScriptVariable::Set("LogWarning", LogWarning, true, true); + ScriptVariable::Set("LogCritical", LogCritical, true, true); +} + /** * Constructor for the Logger class. */ diff --git a/lib/base/logger.h b/lib/base/logger.h index 82023dd53..6c0fcfad8 100644 --- a/lib/base/logger.h +++ b/lib/base/logger.h @@ -69,6 +69,8 @@ public: static void DisableConsoleLog(void); static bool IsConsoleLogEnabled(void); + static void StaticInitialize(void); + protected: virtual void Start(void); virtual void Stop(void); diff --git a/lib/base/scriptfunctionwrapper.cpp b/lib/base/scriptfunctionwrapper.cpp index d5d1efb5d..d18a6a623 100644 --- a/lib/base/scriptfunctionwrapper.cpp +++ b/lib/base/scriptfunctionwrapper.cpp @@ -28,6 +28,13 @@ Value icinga::ScriptFunctionWrapperVV(void (*function)(void), const std::vector< return Empty; } +Value icinga::ScriptFunctionWrapperVA(void (*function)(const std::vector&), const std::vector& arguments) +{ + function(arguments); + + return Empty; +} + boost::function& arguments)> icinga::WrapScriptFunction(void (*function)(void)) { return boost::bind(&ScriptFunctionWrapperVV, function, _1); diff --git a/lib/base/scriptfunctionwrapper.h b/lib/base/scriptfunctionwrapper.h index c629a2e53..7704ae723 100644 --- a/lib/base/scriptfunctionwrapper.h +++ b/lib/base/scriptfunctionwrapper.h @@ -30,6 +30,7 @@ namespace icinga { Value ScriptFunctionWrapperVV(void (*function)(void), const std::vector& arguments); +Value ScriptFunctionWrapperVA(void (*function)(const std::vector&), const std::vector& arguments); boost::function& arguments)> I2_BASE_API WrapScriptFunction(void (*function)(void)); @@ -273,6 +274,11 @@ boost::function& arguments)> WrapScriptFunction(TR return boost::bind(function, _1); } +inline boost::function& arguments)> WrapScriptFunction(void (*function)(const std::vector&)) +{ + return boost::bind(&ScriptFunctionWrapperVA, function, _1); +} + } #endif /* SCRIPTFUNCTION_H */ diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 0828f75ee..4eaacff9a 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -104,12 +104,30 @@ Array::Ptr ScriptUtils::Intersection(const std::vector& arguments) return result; } -void ScriptUtils::Log(const Value& message) +void ScriptUtils::Log(const std::vector& arguments) { + if (arguments.size() != 1 && arguments.size() != 3) + BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid number of arguments for log()")); + + LogSeverity severity; + String facility; + Value message; + + if (arguments.size() == 1) { + severity = LogInformation; + facility = "config"; + message = arguments[0]; + } else { + int sval = static_cast(arguments[0]); + severity = static_cast(sval); + facility = arguments[1]; + message = arguments[2]; + } + if (message.IsString()) - ::Log(LogInformation, "config", message); + ::Log(severity, facility, message); else - ::Log(LogInformation, "config", JsonSerialize(message)); + ::Log(severity, facility, JsonSerialize(message)); } void ScriptUtils::Exit(int code) diff --git a/lib/base/scriptutils.h b/lib/base/scriptutils.h index 043b12cbb..904706ea1 100644 --- a/lib/base/scriptutils.h +++ b/lib/base/scriptutils.h @@ -37,7 +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 Value& message); + static void Log(const std::vector& arguments); static void Exit(int code); private: -- 2.40.0