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
#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.
*/
static void DisableConsoleLog(void);
static bool IsConsoleLogEnabled(void);
+ static void StaticInitialize(void);
+
protected:
virtual void Start(void);
virtual void Stop(void);
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);
{
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));
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 */
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)
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: