]> granicus.if.org Git - icinga2/commitdiff
Implement additional arguments for log().
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 1 Apr 2014 07:33:54 +0000 (09:33 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 1 Apr 2014 07:33:54 +0000 (09:33 +0200)
Fixes #5902

doc/4.1-configuration-syntax.md
lib/base/logger.cpp
lib/base/logger.h
lib/base/scriptfunctionwrapper.cpp
lib/base/scriptfunctionwrapper.h
lib/base/scriptutils.cpp
lib/base/scriptutils.h

index 73c3227f9c71ec8c6a37df7a23e154454327803e..05ec479e2ff50d4137e21713e2ae0c602850b598 100644 (file)
@@ -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.
 
 ### <a id="operators"></a> Dictionary Operators
index 47f2f8bedd076fb4e813df0becc43b6dc860f1d3..b841fbd08d96dc6d1a46938c5e93da6a6f934eb9 100644 (file)
@@ -25,6 +25,7 @@
 #include "base/objectlock.h"
 #include "base/context.h"
 #include "base/convert.h"
+#include "base/scriptvariable.h"
 #include <boost/make_shared.hpp>
 #include <boost/foreach.hpp>
 #include <iostream>
 using namespace icinga;
 
 REGISTER_TYPE(Logger);
+INITIALIZE_ONCE(&Logger::StaticInitialize);
 
 std::set<Logger::Ptr> 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.
  */
index 82023dd53b5b2b7515418dea643f4caafce33350..6c0fcfad819fb3105bc74ec4862039b29e4c4469 100644 (file)
@@ -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);
index d5d1efb5dce0d3c3a71efe2a8204f9bade14b77b..d18a6a6239cbbfa2ff7e7058064c77447be14735 100644 (file)
@@ -28,6 +28,13 @@ Value icinga::ScriptFunctionWrapperVV(void (*function)(void), const std::vector<
        return Empty;
 }
 
+Value icinga::ScriptFunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments)
+{
+       function(arguments);
+
+       return Empty;
+}
+
 boost::function<Value (const std::vector<Value>& arguments)> icinga::WrapScriptFunction(void (*function)(void))
 {
        return boost::bind(&ScriptFunctionWrapperVV, function, _1);
index c629a2e538457aea645747d603d55a4e63030628..7704ae72320f8cb07051e8759a8e86219ced54bd 100644 (file)
@@ -30,6 +30,7 @@ namespace icinga
 {
 
 Value ScriptFunctionWrapperVV(void (*function)(void), const std::vector<Value>& arguments);
+Value ScriptFunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments);
 
 boost::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapScriptFunction(void (*function)(void));
 
@@ -273,6 +274,11 @@ boost::function<TR (const std::vector<Value>& arguments)> WrapScriptFunction(TR
        return boost::bind(function, _1);
 }
 
+inline boost::function<Value (const std::vector<Value>& arguments)> WrapScriptFunction(void (*function)(const std::vector<Value>&))
+{
+       return boost::bind(&ScriptFunctionWrapperVA, function, _1);
+}
+
 }
 
 #endif /* SCRIPTFUNCTION_H */
index 0828f75eea56f573a7cf7f2473236c72a186c407..4eaacff9a00d39cc8e8d9fc1beaafd1887e14d8f 100644 (file)
@@ -104,12 +104,30 @@ Array::Ptr ScriptUtils::Intersection(const std::vector<Value>& arguments)
        return result;
 }
 
-void ScriptUtils::Log(const Value& message)
+void ScriptUtils::Log(const std::vector<Value>& 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<int>(arguments[0]);
+               severity = static_cast<LogSeverity>(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)
index 043b12cbba83220bfa4bfd66239083bfbe370252..904706ea1b58bdc3a0a41993ea5a0c5655a235c0 100644 (file)
@@ -37,7 +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 Value& message);
+       static void Log(const std::vector<Value>& arguments);
        static void Exit(int code);
 
 private: