Implement additional arguments for log().

Fixes #5902
This commit is contained in:
Gunnar Beutner 2014-04-01 09:33:54 +02:00
parent baef781a85
commit 2c17305536
7 changed files with 48 additions and 4 deletions

View 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

View 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>
@ -32,11 +33,20 @@
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.
*/

View 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);

View 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);

View 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 */

View 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)

View 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: