From 047a35fc691a348008cbfbf4c3d7722a6ec4f93e Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Mon, 12 Dec 2016 16:19:14 +0100 Subject: Custom logging for interpreters --- apps/uscxml-browser.cpp | 4 +- apps/uscxml-transform.cpp | 4 +- src/uscxml/Interpreter.cpp | 12 ++++-- src/uscxml/Interpreter.h | 6 +++ src/uscxml/debug/DebugSession.cpp | 2 +- src/uscxml/debug/DebuggerServlet.cpp | 17 +++++++++ src/uscxml/debug/DebuggerServlet.h | 37 ++++-------------- src/uscxml/interpreter/BasicContentExecutor.cpp | 6 +-- src/uscxml/interpreter/ContentExecutorImpl.h | 2 + src/uscxml/interpreter/InterpreterImpl.cpp | 2 +- src/uscxml/interpreter/InterpreterImpl.h | 7 ++++ src/uscxml/interpreter/Logging.h | 6 ++- src/uscxml/interpreter/MicroStepImpl.h | 1 + src/uscxml/messages/Data.cpp | 8 ++-- src/uscxml/plugins/DataModelImpl.h | 2 + .../datamodel/ecmascript/v8/V8DataModel.cpp | 26 ++++++------- src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp | 2 +- .../plugins/datamodel/promela/PromelaDataModel.cpp | 2 +- .../plugins/invoker/dirmon/DirMonInvoker.cpp | 11 +++--- src/uscxml/plugins/invoker/dirmon/DirMonInvoker.h | 5 +++ src/uscxml/server/HTTPServer.cpp | 16 ++++---- src/uscxml/transform/ChartToPromela.cpp | 2 +- src/uscxml/util/DOM.cpp | 2 +- src/uscxml/util/URL.cpp | 44 +++++++++++----------- test/src/test-gen-c.cpp | 4 ++ test/src/test-stress.cpp | 2 +- test/src/test-url.cpp | 2 +- 27 files changed, 132 insertions(+), 102 deletions(-) diff --git a/apps/uscxml-browser.cpp b/apps/uscxml-browser.cpp index 99f646f..8ab5524 100644 --- a/apps/uscxml-browser.cpp +++ b/apps/uscxml-browser.cpp @@ -56,7 +56,7 @@ int main(int argc, char** argv) { InterpreterOptions* currOptions = options.interpreters[0].second; std::string documentURL = options.interpreters[0].first; - LOG(USCXML_INFO) << "Processing " << documentURL; + LOGD(USCXML_INFO) << "Processing " << documentURL; try { Interpreter interpreter = Interpreter::fromURL(documentURL); @@ -82,7 +82,7 @@ int main(int argc, char** argv) { interpreters.push_back(interpreter); } else { - LOG(USCXML_ERROR) << "Cannot create interpreter from " << documentURL; + LOGD(USCXML_ERROR) << "Cannot create interpreter from " << documentURL; } } catch (Event e) { std::cout << e << std::endl; diff --git a/apps/uscxml-transform.cpp b/apps/uscxml-transform.cpp index 0cf538c..2b440ff 100644 --- a/apps/uscxml-transform.cpp +++ b/apps/uscxml-transform.cpp @@ -211,7 +211,7 @@ int main(int argc, char** argv) { Interpreter interpreter; try { if (inputFile.size() == 0 || inputFile == "-") { - LOG(USCXML_INFO) << "Reading SCXML from STDIN"; + LOGD(USCXML_INFO) << "Reading SCXML from STDIN"; std::stringstream ss; std::string line; while (std::getline(std::cin, line)) { @@ -254,7 +254,7 @@ int main(int argc, char** argv) { } if (!interpreter) { - LOG(USCXML_ERROR) << "Cannot create interpreter from " << inputFile; + LOGD(USCXML_ERROR) << "Cannot create interpreter from " << inputFile; exit(EXIT_FAILURE); } diff --git a/src/uscxml/Interpreter.cpp b/src/uscxml/Interpreter.cpp index 190240f..1aa07df 100644 --- a/src/uscxml/Interpreter.cpp +++ b/src/uscxml/Interpreter.cpp @@ -170,13 +170,13 @@ Interpreter Interpreter::fromURL(const std::string& url) { } catch (const XERCESC_NS::SAXParseException& toCatch) { - LOG(USCXML_ERROR) << X(toCatch.getMessage()); + LOGD(USCXML_ERROR) << X(toCatch.getMessage()); } catch (const XERCESC_NS::RuntimeException& toCatch) { - LOG(USCXML_ERROR) << X(toCatch.getMessage()); + LOGD(USCXML_ERROR) << X(toCatch.getMessage()); } catch (const XERCESC_NS::XMLException& toCatch) { - LOG(USCXML_ERROR) << X(toCatch.getMessage()); + LOGD(USCXML_ERROR) << X(toCatch.getMessage()); } catch (const XERCESC_NS::DOMException& toCatch) { - LOG(USCXML_ERROR) << X(toCatch.getMessage()); + LOGD(USCXML_ERROR) << X(toCatch.getMessage()); } return interpreter; @@ -227,6 +227,10 @@ void Interpreter::removeMonitor(InterpreterMonitor* monitor) { return _impl->removeMonitor(monitor); } +Logger Interpreter::getLogger() { + return _impl->getLogger(); +} + std::list Interpreter::validate() { return InterpreterIssue::forInterpreter(_impl.get()); } diff --git a/src/uscxml/Interpreter.h b/src/uscxml/Interpreter.h index e9e472d..cf87fc4 100644 --- a/src/uscxml/Interpreter.h +++ b/src/uscxml/Interpreter.h @@ -28,6 +28,7 @@ #include #include "uscxml/interpreter/MicroStep.h" +#include "uscxml/interpreter/Logging.h" #include "uscxml/plugins/DataModel.h" #include "uscxml/plugins/Factory.h" #include "uscxml/interpreter/ContentExecutor.h" @@ -51,6 +52,7 @@ class InterpreterIssue; */ class USCXML_API ActionLanguage { public: + Logger logger; ///< The logger instance to use for messages MicroStep microStepper; ///< The microstepper instance to use DataModel dataModel; ///< The datamodel to use ContentExecutor execContent; ///< To process executable content elements @@ -194,6 +196,10 @@ public: */ void removeMonitor(InterpreterMonitor* monitor); + /** + * Return the logger associated with this interpreter + */ + Logger getLogger(); /** * Return the actual implementation of the Interperter. diff --git a/src/uscxml/debug/DebugSession.cpp b/src/uscxml/debug/DebugSession.cpp index 5b68f8e..fff6fe1 100644 --- a/src/uscxml/debug/DebugSession.cpp +++ b/src/uscxml/debug/DebugSession.cpp @@ -215,7 +215,7 @@ void DebugSession::run(void* instance) { // return; // } } - LOG(USCXML_DEBUG) << "done"; + LOG(INSTANCE->_interpreter.getLogger(), USCXML_DEBUG) << "done"; } Data DebugSession::debugStop(const Data& data) { diff --git a/src/uscxml/debug/DebuggerServlet.cpp b/src/uscxml/debug/DebuggerServlet.cpp index 016d67c..56b43d5 100644 --- a/src/uscxml/debug/DebuggerServlet.cpp +++ b/src/uscxml/debug/DebuggerServlet.cpp @@ -238,6 +238,23 @@ void DebuggerServlet::processListSessions(const HTTPServer::Request& request) { returnData(request, replyData); } +std::shared_ptr DebuggerServlet::create() { + assert(false); + return std::shared_ptr(this); +} + +void DebuggerServlet::log(LogSeverity severity, const Event& event) { + pushData(std::shared_ptr(), event.data); +} + +void DebuggerServlet::log(LogSeverity severity, const Data& data) { + pushData(std::shared_ptr(), data); +} + +void DebuggerServlet::log(LogSeverity severity, const std::string& message) { + pushData(std::shared_ptr(), Data(message)); +} + /* void DebuggerServlet::handle(const el::LogDispatchData* data) { } diff --git a/src/uscxml/debug/DebuggerServlet.h b/src/uscxml/debug/DebuggerServlet.h index 05e1397..8d487b2 100644 --- a/src/uscxml/debug/DebuggerServlet.h +++ b/src/uscxml/debug/DebuggerServlet.h @@ -21,7 +21,7 @@ #define DEBUGGERSERVLET_H_ATUMDA3G #include "uscxml/Common.h" -#include "uscxml/interpreter/Logging.h" +#include "uscxml/interpreter/LoggingImpl.h" #include "uscxml/util/BlockingQueue.h" #include "uscxml/server/HTTPServer.h" @@ -29,27 +29,8 @@ namespace uscxml { -class USCXML_API DebuggerServlet : public Debugger, public HTTPServlet, public Logger { +class USCXML_API DebuggerServlet : public Debugger, public HTTPServlet, public LoggerImpl { public: - class LogMessage : public Data { - public: -#if 0 - LogMessage(google::LogSeverity severity, const char* full_filename, - const char* base_filename, int line, - const struct ::tm* tm_time, - std::string message, std::string formatted) { - - compound["severity"] = severity; - compound["fullFilename"] = Data(full_filename, Data::VERBATIM); - compound["baseFilename"] = Data(base_filename, Data::VERBATIM); - compound["line"] = line; - compound["message"] = Data(message, Data::VERBATIM); - compound["time"] = Data(mktime((struct ::tm*)tm_time), Data::INTERPRETED); - compound["formatted"] = Data(formatted, Data::VERBATIM); - } -#endif - }; - virtual ~DebuggerServlet() {} // from Debugger @@ -85,14 +66,12 @@ public: // void processRemoveBreakPoint(const HTTPServer::Request& request); // void processPoll(const HTTPServer::Request& request); - // Logsink - /** - virtual void send(google::LogSeverity severity, const char* full_filename, - const char* base_filename, int line, - const struct ::tm* tm_time, - const char* message, size_t message_len); - void handle(const el::LogDispatchData* data); - */ + // Logger + virtual std::shared_ptr create(); + + virtual void log(LogSeverity severity, const Event& event); + virtual void log(LogSeverity severity, const Data& data); + virtual void log(LogSeverity severity, const std::string& message); protected: void serverPushData(std::shared_ptr); diff --git a/src/uscxml/interpreter/BasicContentExecutor.cpp b/src/uscxml/interpreter/BasicContentExecutor.cpp index 376561d..60a45e5 100644 --- a/src/uscxml/interpreter/BasicContentExecutor.cpp +++ b/src/uscxml/interpreter/BasicContentExecutor.cpp @@ -144,7 +144,7 @@ void BasicContentExecutor::processSend(XERCESC_NS::DOMElement* element) { } else if (delayAttr.unit.length() == 0) { // unit less delay is interpreted as milliseconds delayMs = strTo(delayAttr.value); } else { - LOG(USCXML_ERROR) << "Cannot make sense of delay value " << delay << ": does not end in 's' or 'ms'"; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "Cannot make sense of delay value " << delay << ": does not end in 's' or 'ms'"; } } } catch (Event e) { @@ -355,14 +355,14 @@ void BasicContentExecutor::process(XERCESC_NS::DOMElement* block, const X& xmlPr } else if (iequals(tagName, xmlPrefix.str() + "script")) { processScript(block); } else { - LOG(USCXML_ERROR) << tagName; + LOG(_callbacks->getLogger(), USCXML_ERROR) << tagName; assert(false); } } catch (ErrorEvent exc) { Event e(exc); _callbacks->enqueueInternal(e); - LOG(USCXML_ERROR) << exc << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << exc << std::endl; USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterExecutingContent, block); throw e; // will be catched in microstepper diff --git a/src/uscxml/interpreter/ContentExecutorImpl.h b/src/uscxml/interpreter/ContentExecutorImpl.h index 7eaebbc..dfd4c5f 100644 --- a/src/uscxml/interpreter/ContentExecutorImpl.h +++ b/src/uscxml/interpreter/ContentExecutorImpl.h @@ -24,6 +24,7 @@ #include "uscxml/Common.h" #include "uscxml/messages/Event.h" #include "uscxml/interpreter/InterpreterMonitor.h" +#include "uscxml/interpreter/Logging.h" #include #include @@ -73,6 +74,7 @@ public: /** Monitoring */ virtual std::set getMonitors() = 0; virtual Interpreter getInterpreter() = 0; + virtual Logger getLogger() = 0; }; diff --git a/src/uscxml/interpreter/InterpreterImpl.cpp b/src/uscxml/interpreter/InterpreterImpl.cpp index 85c77a8..43059bf 100644 --- a/src/uscxml/interpreter/InterpreterImpl.cpp +++ b/src/uscxml/interpreter/InterpreterImpl.cpp @@ -249,7 +249,7 @@ bool InterpreterImpl::isTrue(const std::string& expr) { } catch (ErrorEvent e) { // test 244: deliver error execution - LOG(USCXML_ERROR) << e; + LOG(getLogger(), USCXML_ERROR) << e; // test 344 enqueueInternal(e); diff --git a/src/uscxml/interpreter/InterpreterImpl.h b/src/uscxml/interpreter/InterpreterImpl.h index ade2a91..f4fe93e 100644 --- a/src/uscxml/interpreter/InterpreterImpl.h +++ b/src/uscxml/interpreter/InterpreterImpl.h @@ -225,6 +225,8 @@ public: /** --- */ void setActionLanguage(const ActionLanguage& al) { + if (al.logger) // we intialized _logger as the default logger already + _logger = al.logger; _execContent = al.execContent; _microStepper = al.microStepper; _dataModel = al.dataModel; @@ -237,6 +239,10 @@ public: _factory = factory; } + virtual Logger getLogger() { + return _logger; + } + static std::map > getInstances(); virtual XERCESC_NS::DOMDocument* getDocument() { @@ -281,6 +287,7 @@ protected: MicroStep _microStepper; DataModel _dataModel; ContentExecutor _execContent; + Logger _logger = Logger::getDefault(); InterpreterState _state; diff --git a/src/uscxml/interpreter/Logging.h b/src/uscxml/interpreter/Logging.h index dd59e95..7221d5b 100644 --- a/src/uscxml/interpreter/Logging.h +++ b/src/uscxml/interpreter/Logging.h @@ -28,8 +28,10 @@ #include -#define LOG(lvl) uscxml::Logger::getDefault().log(lvl) -#define LOG2(lvl, thing) uscxml::Logger::getDefault().log(lvl, thing); +#define LOG(logger, lvl) logger.log(lvl) +#define LOG2(logger, lvl, thing) logger.log(lvl, thing) +#define LOGD(lvl) uscxml::Logger::getDefault().log(lvl) +#define LOGD2(lvl, thing) uscxml::Logger::getDefault().log(lvl, thing); namespace uscxml { diff --git a/src/uscxml/interpreter/MicroStepImpl.h b/src/uscxml/interpreter/MicroStepImpl.h index cdb98f2..1544e78 100644 --- a/src/uscxml/interpreter/MicroStepImpl.h +++ b/src/uscxml/interpreter/MicroStepImpl.h @@ -59,6 +59,7 @@ public: /** Monitoring */ virtual std::set getMonitors() = 0; virtual Interpreter getInterpreter() = 0; + virtual Logger getLogger() = 0; }; /** diff --git a/src/uscxml/messages/Data.cpp b/src/uscxml/messages/Data.cpp index 5d7dbf2..b2d08e4 100644 --- a/src/uscxml/messages/Data.cpp +++ b/src/uscxml/messages/Data.cpp @@ -101,7 +101,7 @@ Data Data::fromJSON(const std::string& jsonString) { } t = (jsmntok_t*)malloc((nrTokens + 1) * sizeof(jsmntok_t)); if (t == NULL) { - LOG(USCXML_ERROR) << "Cannot parse JSON, ran out of memory!"; + throw ErrorEvent("Cannot parse JSON, ran out of memory!"); return data; } memset(t, 0, (nrTokens + 1) * sizeof(jsmntok_t)); @@ -112,13 +112,13 @@ Data Data::fromJSON(const std::string& jsonString) { if (rv != 0) { switch (rv) { case JSMN_ERROR_NOMEM: - LOG(USCXML_ERROR) << "Cannot parse JSON, not enough tokens were provided!"; + throw ErrorEvent("Cannot parse JSON, not enough tokens were provided!"); break; case JSMN_ERROR_INVAL: - LOG(USCXML_ERROR) << "Cannot parse JSON, invalid character inside JSON string!"; + throw ErrorEvent("Cannot parse JSON, invalid character inside JSON string!"); break; case JSMN_ERROR_PART: - LOG(USCXML_ERROR) << "Cannot parse JSON, the string is not a full JSON packet, more bytes expected!"; + throw ErrorEvent("Cannot parse JSON, the string is not a full JSON packet, more bytes expected!"); break; default: break; diff --git a/src/uscxml/plugins/DataModelImpl.h b/src/uscxml/plugins/DataModelImpl.h index 9ee1ed1..e93361d 100644 --- a/src/uscxml/plugins/DataModelImpl.h +++ b/src/uscxml/plugins/DataModelImpl.h @@ -23,6 +23,7 @@ #include "uscxml/Common.h" #include "uscxml/plugins/Invoker.h" #include "uscxml/plugins/IOProcessor.h" +#include "uscxml/interpreter/Logging.h" namespace XERCESC_NS { class DOMDocument; @@ -52,6 +53,7 @@ public: virtual bool isInState(const std::string& stateId) = 0; virtual XERCESC_NS::DOMDocument* getDocument() const = 0; virtual const std::map& getInvokers() = 0; + virtual Logger getLogger() = 0; }; class USCXML_API DataModelExtension { diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp index f916ae9..283372d 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp @@ -485,27 +485,27 @@ Data V8DataModel::getValueAsData(const v8::Local& value, std::setIsBoolean()) { data.atom = (value->ToBoolean()->Value() ? "true" : "false"); } else if (value->IsBooleanObject()) { - LOG(USCXML_ERROR) << "IsBooleanObject is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsBooleanObject is unimplemented" << std::endl; } else if (value->IsDate()) { - LOG(USCXML_ERROR) << "IsDate is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsDate is unimplemented" << std::endl; } else if (value->IsExternal()) { - LOG(USCXML_ERROR) << "IsExternal is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsExternal is unimplemented" << std::endl; } else if (value->IsFalse()) { - LOG(USCXML_ERROR) << "IsFalse is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsFalse is unimplemented" << std::endl; } else if (value->IsFunction()) { - LOG(USCXML_ERROR) << "IsFunction is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsFunction is unimplemented" << std::endl; } else if (value->IsInt32()) { int32_t prop = value->Int32Value(); data.atom = toStr(prop); } else if (value->IsNativeError()) { - LOG(USCXML_ERROR) << "IsNativeError is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsNativeError is unimplemented" << std::endl; } else if (value->IsNull()) { - LOG(USCXML_ERROR) << "IsNull is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsNull is unimplemented" << std::endl; } else if (value->IsNumber()) { v8::String::AsciiValue prop(v8::Local::Cast(v8::Local::Cast(value))); data.atom = *prop; } else if (value->IsNumberObject()) { - LOG(USCXML_ERROR) << "IsNumberObject is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsNumberObject is unimplemented" << std::endl; } else if (value->IsObject()) { // if (V8ArrayBuffer::hasInstance(value)) { @@ -529,21 +529,21 @@ Data V8DataModel::getValueAsData(const v8::Local& value, std::setIsRegExp()) { - LOG(USCXML_ERROR) << "IsRegExp is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsRegExp is unimplemented" << std::endl; } else if(value->IsString()) { v8::String::AsciiValue property(v8::Local::Cast(value)); data.atom = *property; data.type = Data::VERBATIM; } else if(value->IsStringObject()) { - LOG(USCXML_ERROR) << "IsStringObject is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsStringObject is unimplemented" << std::endl; } else if(value->IsTrue()) { - LOG(USCXML_ERROR) << "IsTrue is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsTrue is unimplemented" << std::endl; } else if(value->IsUint32()) { - LOG(USCXML_ERROR) << "IsUint32 is unimplemented" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "IsUint32 is unimplemented" << std::endl; } else if(value->IsUndefined()) { data.atom = "undefined"; } else { - LOG(USCXML_ERROR) << "Value's type is unknown!" << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << "Value's type is unknown!" << std::endl; } return data; } diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp index 55c4501..2ad89b5 100644 --- a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp +++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp @@ -204,7 +204,7 @@ std::shared_ptr LuaDataModel::create(DataModelCallbacks* callback luabridge::setGlobal(dm->_luaState, resultLxpLOM, "lxp.lom"); } } catch (luabridge::LuaException e) { - LOG(USCXML_INFO) << e.what(); + LOG(_callbacks->getLogger(), USCXML_INFO) << e.what(); } luabridge::getGlobalNamespace(dm->_luaState).beginClass("DataModel").endClass(); diff --git a/src/uscxml/plugins/datamodel/promela/PromelaDataModel.cpp b/src/uscxml/plugins/datamodel/promela/PromelaDataModel.cpp index a13dd07..a524c7e 100644 --- a/src/uscxml/plugins/datamodel/promela/PromelaDataModel.cpp +++ b/src/uscxml/plugins/datamodel/promela/PromelaDataModel.cpp @@ -193,7 +193,7 @@ void PromelaDataModel::setEvent(const Event& event) { try { PromelaParser parser(expr); } catch (Event e) { - LOG(USCXML_ERROR) << e << std::endl; + LOG(_callbacks->getLogger(), USCXML_ERROR) << e << std::endl; return false; } return true; diff --git a/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.cpp b/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.cpp index de5994a..d6d0f99 100644 --- a/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.cpp +++ b/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.cpp @@ -99,7 +99,7 @@ void DirMonInvoker::eventFromSCXML(const Event& event) { void DirMonInvoker::invoke(const std::string& source, const Event& req) { if (req.params.find("dir") == req.params.end()) { - LOG(USCXML_ERROR) << "No dir param given"; + LOG(_interpreter->getLogger(), USCXML_ERROR) << "No dir param given"; return; } @@ -137,7 +137,7 @@ void DirMonInvoker::invoke(const std::string& source, const Event& req) { URL url = URL::resolve(dirIter->second.atom, _interpreter->getBaseURL()); if (!url.isAbsolute()) { - LOG(USCXML_ERROR) << "Given directory '" << dirIter->second << "' cannot be transformed to absolute path"; + LOG(_interpreter->getLogger(), USCXML_ERROR) << "Given directory '" << dirIter->second << "' cannot be transformed to absolute path"; } else { _dir = url.path(); } @@ -145,6 +145,7 @@ void DirMonInvoker::invoke(const std::string& source, const Event& req) { } _watcher = new DirectoryWatch(_dir, _recurse); + _watcher->setLogger(_interpreter->getLogger()); _watcher->addMonitor(this); _watcher->updateEntries(true); @@ -313,7 +314,7 @@ void DirectoryWatch::updateEntries(bool reportAsExisting) { // stat directory for modification date struct stat dirStat; if (stat((_dir + _relDir).c_str(), &dirStat) != 0) { - LOG(USCXML_ERROR) << "Error with stat on directory " << _dir << ": " << strerror(errno); + LOG(_logger, USCXML_ERROR) << "Error with stat on directory " << _dir << ": " << strerror(errno); return; } @@ -327,7 +328,7 @@ void DirectoryWatch::updateEntries(bool reportAsExisting) { DIR *dp; dp = opendir((_dir + _relDir).c_str()); if (dp == NULL) { - LOG(USCXML_ERROR) << "Error opening directory " << _dir + _relDir << ": " << strerror(errno); + LOG(_logger, USCXML_ERROR) << "Error opening directory " << _dir + _relDir << ": " << strerror(errno); return; } // iterate all entries and see what changed @@ -352,7 +353,7 @@ void DirectoryWatch::updateEntries(bool reportAsExisting) { struct stat fileStat; if (stat(filename.c_str(), &fileStat) != 0) { - LOG(USCXML_ERROR) << "Error with stat on directory entry: " << filename << ": " << strerror(errno); + LOG(_logger, USCXML_ERROR) << "Error with stat on directory entry: " << filename << ": " << strerror(errno); continue; } diff --git a/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.h b/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.h index 6e13864..e6bb9aa 100644 --- a/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.h +++ b/src/uscxml/plugins/invoker/dirmon/DirMonInvoker.h @@ -73,11 +73,16 @@ public: return entries; } + void setLogger(Logger logger) { + _logger = logger; + } + protected: DirectoryWatch(const std::string& dir, const std::string& relDir) : _dir(dir), _relDir(relDir), _recurse(true), _lastChecked(0) {} std::string _dir; std::string _relDir; + Logger _logger; bool _recurse; std::map _knownEntries; diff --git a/src/uscxml/server/HTTPServer.cpp b/src/uscxml/server/HTTPServer.cpp index 82e5cef..ab16887 100644 --- a/src/uscxml/server/HTTPServer.cpp +++ b/src/uscxml/server/HTTPServer.cpp @@ -93,9 +93,9 @@ HTTPServer::HTTPServer(unsigned short port, unsigned short wsPort, SSLConfig* ss if (_port > 0) { _httpHandle = evhttp_bind_socket_with_handle(_http, NULL, _port); if (_httpHandle) { - LOG(USCXML_INFO) << "HTTP server listening on tcp/" << _port; + LOGD(USCXML_INFO) << "HTTP server listening on tcp/" << _port; } else { - LOG(USCXML_ERROR) << "HTTP server cannot bind to tcp/" << _port; + LOGD(USCXML_ERROR) << "HTTP server cannot bind to tcp/" << _port; } } @@ -103,9 +103,9 @@ HTTPServer::HTTPServer(unsigned short port, unsigned short wsPort, SSLConfig* ss if (_wsPort > 0) { _wsHandle = evws_bind_socket(_evws, _wsPort); if (_wsHandle) { - LOG(USCXML_INFO) << "WebSocket server listening on tcp/" << _wsPort; + LOGD(USCXML_INFO) << "WebSocket server listening on tcp/" << _wsPort; } else { - LOG(USCXML_ERROR) << "WebSocket server cannot bind to tcp/" << _wsPort; + LOGD(USCXML_ERROR) << "WebSocket server cannot bind to tcp/" << _wsPort; } } @@ -462,7 +462,7 @@ void HTTPServer::processByMatchingServlet(const Request& request) { matchesIter++; } - LOG(USCXML_INFO) << "Got an HTTP request at " << actualPath << " but no servlet is registered there or at a prefix"; + LOGD(USCXML_INFO) << "Got an HTTP request at " << actualPath << " but no servlet is registered there or at a prefix"; evhttp_send_error(request.evhttpReq, 404, NULL); } @@ -505,7 +505,7 @@ void HTTPServer::replyCallback(evutil_socket_t fd, short what, void *arg) { Reply* reply = (Reply*)arg; if (reply->content.size() > 0 && reply->headers.find("Content-Type") == reply->headers.end()) { - LOG(USCXML_INFO) << "Sending content without Content-Type header"; + LOGD(USCXML_INFO) << "Sending content without Content-Type header"; } std::map::const_iterator headerIter = reply->headers.begin(); @@ -565,7 +565,7 @@ bool HTTPServer::registerServlet(const std::string& path, HTTPServlet* servlet) HTTPServer* INSTANCE = getInstance(); if (!INSTANCE->_httpHandle) { - LOG(USCXML_INFO) << "Registering at unstarted HTTP Server"; + LOGD(USCXML_INFO) << "Registering at unstarted HTTP Server"; return true; // this is the culprit! } @@ -702,7 +702,7 @@ void HTTPServer::run(void* instance) { while(INSTANCE->_isRunning) { event_base_dispatch(INSTANCE->_base); } - LOG(USCXML_INFO) << "HTTP Server stopped"; + LOGD(USCXML_INFO) << "HTTP Server stopped"; } void HTTPServer::determineAddress() { diff --git a/src/uscxml/transform/ChartToPromela.cpp b/src/uscxml/transform/ChartToPromela.cpp index 48037c0..ec11a76 100644 --- a/src/uscxml/transform/ChartToPromela.cpp +++ b/src/uscxml/transform/ChartToPromela.cpp @@ -694,7 +694,7 @@ void ChartToPromela::writeVariables(std::ostream& stream) { if (allTypes.types.find(identifier) != allTypes.types.end()) { type = allTypes.types[identifier].name; } else { - LOG(USCXML_ERROR) << "Automatic or no type for '" << identifier << "' but no type resolved"; + LOGD(USCXML_ERROR) << "Automatic or no type for '" << identifier << "' but no type resolved"; continue; } } diff --git a/src/uscxml/util/DOM.cpp b/src/uscxml/util/DOM.cpp index 9540d04..bc628b7 100644 --- a/src/uscxml/util/DOM.cpp +++ b/src/uscxml/util/DOM.cpp @@ -146,7 +146,7 @@ std::string DOMUtils::xPathForNode(const DOMNode* node, const std::string& ns) { case DOMNode::DOCUMENT_NODE: return xPath; default: - LOG(USCXML_ERROR) << "Only nodes of type element supported for now"; + throw ErrorEvent("Only nodes of type element supported for now"); return ""; break; } diff --git a/src/uscxml/util/URL.cpp b/src/uscxml/util/URL.cpp index b9d7bc3..0885b47 100644 --- a/src/uscxml/util/URL.cpp +++ b/src/uscxml/util/URL.cpp @@ -276,7 +276,7 @@ CURL* URLImpl::getCurlHandle() { if (_handle == NULL) { _handle = curl_easy_init(); if (_handle == NULL) - LOG(USCXML_ERROR) << "curl_easy_init returned NULL, this is bad!"; + throw ErrorEvent("curl_easy_init returned NULL, this is bad!"); } return _handle; } @@ -613,7 +613,7 @@ void URLFetcher::fetchURL(URL& url) { std::string fromURL(url); (curlError = curl_easy_setopt(handle, CURLOPT_URL, fromURL.c_str())) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set url to " << std::string(url) << ": " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set url to " << std::string(url) << ": " << curl_easy_strerror(curlError); // (curlError = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1)) == CURLE_OK || // LOG(USCXML_ERROR) << "Cannot set curl to ignore signals: " << curl_easy_strerror(curlError); @@ -625,37 +625,37 @@ void URLFetcher::fetchURL(URL& url) { // LOG(USCXML_ERROR) << "Cannot set verbose: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_WRITEDATA, url._impl.get())) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot register this as write userdata: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot register this as write userdata: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, URLImpl::writeHandler)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set write callback: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set write callback: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, URLImpl::headerHandler)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot request header from curl: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot request header from curl: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_HEADERDATA, url._impl.get())) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot register this as header userdata: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot register this as header userdata: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot forfeit peer verification: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot forfeit peer verification: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_USERAGENT, "uscxml/" USCXML_VERSION)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set our user agent string: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set our user agent string: " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, true)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot enable follow redirects: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot enable follow redirects: " << curl_easy_strerror(curlError); if (instance->_envProxy) (curlError = curl_easy_setopt(handle, CURLOPT_PROXY, instance->_envProxy)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set curl proxy: " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set curl proxy: " << curl_easy_strerror(curlError); if (url._impl->_requestType == URLRequestType::POST) { (curlError = curl_easy_setopt(handle, CURLOPT_POST, 1)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set request type to post for " << std::string(url) << ": " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set request type to post for " << std::string(url) << ": " << curl_easy_strerror(curlError); (curlError = curl_easy_setopt(handle, CURLOPT_COPYPOSTFIELDS, url._impl->_outContent.c_str())) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set post data " << std::string(url) << ": " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set post data " << std::string(url) << ": " << curl_easy_strerror(curlError); // Disable "Expect: 100-continue" // curl_slist* disallowed_headers = 0; @@ -685,14 +685,14 @@ void URLFetcher::fetchURL(URL& url) { instance->_handlesToHeaders[handle] = headers; (curlError = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot headers for " << std::string(url) << ": " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot headers for " << std::string(url) << ": " << curl_easy_strerror(curlError); // curl_slist_free_all(headers); } else if (url._impl->_requestType == URLRequestType::GET) { (curlError = curl_easy_setopt(handle, CURLOPT_HTTPGET, 1)) == CURLE_OK || - LOG(USCXML_ERROR) << "Cannot set request type to get for " << std::string(url) << ": " << curl_easy_strerror(curlError); + LOGD(USCXML_ERROR) << "Cannot set request type to get for " << std::string(url) << ": " << curl_easy_strerror(curlError); } url._impl->downloadStarted(); @@ -742,7 +742,7 @@ void URLFetcher::run(void* instance) { while(fetcher->_isStarted) { fetcher->perform(); } - LOG(USCXML_ERROR) << "URLFetcher thread stopped!"; + LOGD(USCXML_ERROR) << "URLFetcher thread stopped!"; } void URLFetcher::perform() { @@ -759,7 +759,7 @@ void URLFetcher::perform() { } err = curl_multi_perform(_multiHandle, &stillRunning); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_perform: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_perform: " << curl_multi_strerror(err); } } @@ -783,7 +783,7 @@ void URLFetcher::perform() { std::lock_guard lock(_mutex); err = curl_multi_timeout(_multiHandle, &curlTimeOut); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_timeout: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_timeout: " << curl_multi_strerror(err); } } @@ -801,7 +801,7 @@ void URLFetcher::perform() { std::lock_guard lock(_mutex); err = curl_multi_fdset(_multiHandle, &fdread, &fdwrite, &fdexcep, &maxfd); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_fdset: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_fdset: " << curl_multi_strerror(err); } } @@ -816,7 +816,7 @@ void URLFetcher::perform() { std::lock_guard lock(_mutex); err = curl_multi_perform(_multiHandle, &stillRunning); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_perform: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_perform: " << curl_multi_strerror(err); } break; } @@ -831,7 +831,7 @@ void URLFetcher::perform() { _handlesToURLs[msg->easy_handle]._impl->downloadCompleted(); err = curl_multi_remove_handle(_multiHandle, msg->easy_handle); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_remove_handle: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_remove_handle: " << curl_multi_strerror(err); } break; @@ -839,7 +839,7 @@ void URLFetcher::perform() { _handlesToURLs[msg->easy_handle]._impl->downloadFailed(msg->data.result); err = curl_multi_remove_handle(_multiHandle, msg->easy_handle); if (err != CURLM_OK) { - LOG(USCXML_WARN) << "curl_multi_remove_handle: " << curl_multi_strerror(err); + LOGD(USCXML_WARN) << "curl_multi_remove_handle: " << curl_multi_strerror(err); } break; @@ -849,7 +849,7 @@ void URLFetcher::perform() { _handlesToHeaders.erase(msg->easy_handle); } else { - LOG(USCXML_ERROR) << "Curl reports info on unfinished download?!"; + LOGD(USCXML_ERROR) << "Curl reports info on unfinished download?!"; } } } diff --git a/test/src/test-gen-c.cpp b/test/src/test-gen-c.cpp index 2931a13..acf1246 100644 --- a/test/src/test-gen-c.cpp +++ b/test/src/test-gen-c.cpp @@ -55,6 +55,10 @@ public: init(); } + virtual Logger getLogger() { + return Logger::getDefault(); + } + const std::string& getName() { return name; } diff --git a/test/src/test-stress.cpp b/test/src/test-stress.cpp index d3253b4..afb94a6 100644 --- a/test/src/test-stress.cpp +++ b/test/src/test-stress.cpp @@ -79,7 +79,7 @@ int main(int argc, char** argv) { Interpreter interpreter = Interpreter::fromURL(std::string(argv[optind]) + PATH_SEPERATOR + entryIter->first); // Interpreter interpreter = Interpreter::fromURL("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test422.scxml"); - LOG(USCXML_INFO) << "Processing " << interpreter.getImpl()->getBaseURL(); + LOGD(USCXML_INFO) << "Processing " << interpreter.getImpl()->getBaseURL(); if (interpreter) { interpreter.addMonitor(&vm); diff --git a/test/src/test-url.cpp b/test/src/test-url.cpp index 94c47e9..4d6cee8 100644 --- a/test/src/test-url.cpp +++ b/test/src/test-url.cpp @@ -145,7 +145,7 @@ int main(int argc, char** argv) { try { testFileURLs(); } catch (Event e) { - LOG(USCXML_ERROR) << e; + LOGD(USCXML_ERROR) << e; exit(EXIT_FAILURE); } -- cgit v0.12