Commit cbe7ae4ce656428b90ba8320869c95400ffa1b11
- Diff rendering mode:
- inline
- side by side
core/jshandler.cpp
(2 / 2)
|   | |||
| 297 | 297 | } // ctor | |
| 298 | 298 | ||
| 299 | 299 | ScriptContext::~ScriptContext() { | |
| 300 | scrubVector(m_AttachedObjects); | ||
| 300 | JS_GC(m_pContext); | ||
| 301 | 301 | if(!m_AttachedObjects.empty()) { | |
| 302 | 302 | Logger::getInstance()->log("Still have some attached objects (" + intToString(m_AttachedObjects.size()) + "). Memory leak?", lsError); | |
| 303 | 303 | } | |
| 304 | // JS_GC(m_pContext); | ||
| 304 | scrubVector(m_AttachedObjects); | ||
| 305 | 305 | JS_SetContextPrivate(m_pContext, NULL); | |
| 306 | 306 | JS_DestroyContext(m_pContext); | |
| 307 | 307 | m_pContext = NULL; |
core/scripting/jslogger.cpp
(28 / 37)
|   | |||
| 1 | 1 | /* | |
| 2 | 2 | Copyright (c) 2010 digitalSTROM.org, Zurich, Switzerland | |
| 3 | 3 | ||
| 4 | Author: Sergey 'Jin' Bostandzhyan <jin@dev.digitalstrom.org> | ||
| 4 | Authors: Sergey 'Jin' Bostandzhyan <jin@dev.digitalstrom.org>, | ||
| 5 | Patrick Staehlin <pstaehlin@futurelab.ch> | ||
| 5 | 6 | ||
| 6 | 7 | This file is part of digitalSTROM Server. | |
| 7 | 8 | ||
| … | … | ||
| 33 | 33 | #include <boost/bind.hpp> | |
| 34 | 34 | ||
| 35 | 35 | #define LOG_OBJECT_IDENTIFIER "logfile" | |
| 36 | |||
| 36 | 37 | namespace dss { | |
| 38 | |||
| 37 | 39 | const std::string ScriptLoggerExtensionName = "scriptloggerextension"; | |
| 38 | 40 | ||
| 39 | 41 | const std::string LoggerObjectName = "ScriptLoggerContextWrapper"; | |
| 40 | class ScriptLoggerContextWrapper : public ScriptContextAttachedObject { | ||
| 42 | class ScriptLoggerContextWrapper { | ||
| 41 | 43 | public: | |
| 42 | ScriptLoggerContextWrapper(ScriptContext* _pContext, boost::shared_ptr<ScriptLogger> _logger) | ||
| 43 | : ScriptContextAttachedObject(_pContext, LoggerObjectName + _logger->getLogName()), | ||
| 44 | m_ScriptLogger(_logger) | ||
| 44 | ScriptLoggerContextWrapper(boost::shared_ptr<ScriptLogger> _logger) | ||
| 45 | : m_ScriptLogger(_logger) | ||
| 45 | 46 | { } | |
| 46 | 47 | ||
| 47 | 48 | boost::shared_ptr<ScriptLogger> getLogger() { return m_ScriptLogger; } | |
| … | … | ||
| 74 | 74 | return JS_TRUE; | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 77 | jsval v; | ||
| 78 | if (JS_GetProperty(cx, obj, LOG_OBJECT_IDENTIFIER, &v) == JS_TRUE) { | ||
| 79 | if(v != JSVAL_VOID) { | ||
| 80 | JSString *logfile = JSVAL_TO_STRING(v); | ||
| 81 | std::string logfileStr = JS_GetStringBytes(logfile); | ||
| 82 | ScriptContextAttachedObject* attachedObj = ctx->getAttachedObjectByName(LoggerObjectName + logfileStr); | ||
| 83 | ScriptLoggerContextWrapper* wrapper = | ||
| 84 | dynamic_cast<ScriptLoggerContextWrapper*>(attachedObj); | ||
| 85 | if(wrapper != NULL) { | ||
| 86 | Logger::getInstance()->log(JS_GetStringBytes(str)); | ||
| 87 | if(newline) { | ||
| 88 | wrapper->getLogger()->logln(JS_GetStringBytes(str)); | ||
| 89 | } else { | ||
| 90 | wrapper->getLogger()->log(JS_GetStringBytes(str)); | ||
| 91 | } | ||
| 92 | } else { | ||
| 93 | Logger::getInstance()->log("Could not find logger named: " + LoggerObjectName + logfileStr, lsWarning); | ||
| 94 | } | ||
| 77 | ScriptLoggerContextWrapper* wrapper = static_cast<ScriptLoggerContextWrapper*>(JS_GetPrivate(cx, obj)); | ||
| 78 | if(wrapper != NULL) { | ||
| 79 | Logger::getInstance()->log(JS_GetStringBytes(str)); | ||
| 80 | if(newline) { | ||
| 81 | wrapper->getLogger()->logln(JS_GetStringBytes(str)); | ||
| 82 | } else { | ||
| 83 | wrapper->getLogger()->log(JS_GetStringBytes(str)); | ||
| 95 | 84 | } | |
| 85 | } else { | ||
| 86 | Logger::getInstance()->log("ScriptLoggerExtension_log_common: wrapper is null!", lsFatal); | ||
| 87 | return JS_FALSE; | ||
| 96 | 88 | } | |
| 97 | 89 | return JS_TRUE; | |
| 98 | 90 | } | |
| … | … | ||
| 118 | 118 | } | |
| 119 | 119 | ||
| 120 | 120 | boost::shared_ptr<ScriptLogger> pLogger = ext->getLogger(JS_GetStringBytes(str)); | |
| 121 | ctx->attachObject(new ScriptLoggerContextWrapper(ctx, pLogger)); | ||
| 122 | jsval v = STRING_TO_JSVAL(str); | ||
| 123 | JS_SetProperty(cx, obj, LOG_OBJECT_IDENTIFIER, &v); | ||
| 124 | JSBool foundp; | ||
| 125 | JS_SetPropertyAttributes(cx, obj, LOG_OBJECT_IDENTIFIER, JSPROP_READONLY, &foundp); | ||
| 121 | ScriptLoggerContextWrapper* wrapper = new ScriptLoggerContextWrapper(pLogger); | ||
| 122 | JS_SetPrivate(cx, obj, wrapper); | ||
| 126 | 123 | return JS_TRUE; | |
| 127 | |||
| 128 | 124 | } catch(const ScriptException& e) { | |
| 129 | 125 | Logger::getInstance()->log(std::string("ScriptLogger: Caught script exception: ") + e.what()); | |
| 130 | 126 | } | |
| … | … | ||
| 131 | 131 | return JS_FALSE; | |
| 132 | 132 | } | |
| 133 | 133 | ||
| 134 | void ScriptLogger_finalize(JSContext *cx, JSObject *obj) { | ||
| 135 | ScriptLoggerContextWrapper* pWrapper = static_cast<ScriptLoggerContextWrapper*>(JS_GetPrivate(cx, obj)); | ||
| 136 | Logger::getInstance()->log("Finalizing ScriptLogger"); | ||
| 137 | JS_SetPrivate(cx, obj, NULL); | ||
| 138 | delete pWrapper; | ||
| 139 | } // finalize_set | ||
| 140 | |||
| 134 | 141 | static JSClass ScriptLogger_class = { | |
| 135 | 142 | "Logger", JSCLASS_HAS_PRIVATE, | |
| 136 | 143 | JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
| 137 | 144 | JS_EnumerateStandardClasses, | |
| 138 | 145 | JS_ResolveStub, | |
| 139 | JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS | ||
| 146 | JS_ConvertStub, ScriptLogger_finalize, JSCLASS_NO_OPTIONAL_MEMBERS | ||
| 140 | 147 | }; | |
| 141 | 148 | ||
| 142 | 149 | static JSFunctionSpec ScriptLogger_methods[] = { | |
| … | … | ||
| 152 | 152 | {NULL, NULL, 0, 0, 0}, | |
| 153 | 153 | }; | |
| 154 | 154 | ||
| 155 | static JSFunctionSpec ScriptLogger_static_methods[] = { | ||
| 156 | {"getChannel", ScriptLoggerExtension_log, 1, 0, 0}, | ||
| 157 | {NULL, NULL, 0, 0, 0}, | ||
| 158 | }; | ||
| 159 | |||
| 160 | |||
| 161 | 155 | ScriptLogger::ScriptLogger(const std::string& _filePath, | |
| 162 | 156 | const std::string& _filename, | |
| 163 | 157 | ScriptLoggerExtension* _pExtension) { | |
| … | … | ||
| 256 | 256 | JS_InitClass(_context.getJSContext(), | |
| 257 | 257 | _context.getRootObject().getJSObject(), | |
| 258 | 258 | NULL, &ScriptLogger_class, ScriptLogger_construct, 1, NULL, | |
| 259 | ScriptLogger_methods, NULL, ScriptLogger_static_methods); | ||
| 259 | ScriptLogger_methods, NULL, NULL); | ||
| 260 | 260 | ||
| 261 | 261 | } // extendContext | |
| 262 | 262 |

