char *JSMain::GetStackInfo(Isolate *isolate, int *lineNumber) { HandleScope handle_scope(isolate); const char *file = __FILE__; /* Use current filename if we can't find the correct from JS stack */ int line = __LINE__; /* Use current line number if we can't find the correct from JS stack */ char *ret = NULL; /* Try to get the current stack trace (script file) */ Local<StackTrace> stFile = StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kScriptName); if (!stFile.IsEmpty()) { Local<StackFrame> sf = stFile->GetFrame(0); if (!sf.IsEmpty()) { Local<String> fn = sf->GetScriptName(); if (!fn.IsEmpty()) { String::Utf8Value str(fn); if (*str) { js_strdup(ret, *str); // We must dup here } } } } /* dup current filename if we got nothing from stack */ if (ret == NULL) { js_strdup(ret, file); } /* Try to get the current stack trace (line number) */ if (lineNumber) { *lineNumber = 0; Local<StackTrace> stLine = StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kLineNumber); if (!stLine.IsEmpty()) { Local<StackFrame> sf = stLine->GetFrame(0); if (!sf.IsEmpty()) { *lineNumber = sf->GetLineNumber(); } } /* Use current file number if we got nothing from stack */ if (*lineNumber == 0) { *lineNumber = line; } } /* Return dup'ed value - this must be freed by the calling function */ return ret; }
bool MetadataNode::GetExtendLocation(string& extendLocation) { stringstream extendLocationStream; Local<StackTrace> stackTrace = StackTrace::CurrentStackTrace(Isolate::GetCurrent(), 1, StackTrace::kOverview); if (!stackTrace.IsEmpty()) { Handle<StackFrame> frame = stackTrace->GetFrame(0); if (!frame.IsEmpty()) { auto scriptName = frame->GetScriptName(); if (scriptName.IsEmpty()) { extendLocationStream << "unkown location"; extendLocation = extendLocationStream.str(); return false; } string srcFileName = ConvertToString(scriptName); string hardcodedPathToSkip = Constants::APP_ROOT_FOLDER_PATH; int startIndex = hardcodedPathToSkip.length(); int strToTakeLen = (srcFileName.length() - startIndex - 3); // 3 refers to .js at the end of file name string fullPathToFile = srcFileName.substr(startIndex, strToTakeLen); std::replace(fullPathToFile.begin(), fullPathToFile.end(), '/', '_'); std::replace(fullPathToFile.begin(), fullPathToFile.end(), '.', '_'); int lineNumber = frame->GetLineNumber(); if (lineNumber < 0) { extendLocationStream << fullPathToFile.c_str() << " unkown line number"; extendLocation = extendLocationStream.str(); return false; } if (lineNumber > Constants::MODULE_LINES_OFFSET) { lineNumber -= Constants::MODULE_LINES_OFFSET; } int column = frame->GetColumn(); if (column < 0) { extendLocationStream << fullPathToFile.c_str() << " line:" << lineNumber << " unkown column number"; extendLocation = extendLocationStream.str(); return false; } extendLocationStream << "f" << fullPathToFile.c_str() << "_l" << lineNumber << "_c" << column << "__"; //DEBUG_WRITE("EXTEND_LOCATION %s", extendLocationStream.str().c_str()); } } extendLocation = extendLocationStream.str(); return true; }
void DumpJSStack() { Local<StackTrace> st = StackTrace::CurrentStackTrace(25); OutputDebugStringA("\n\n--- Javascript Stack ---\n"); for( int i = 0; i < st->GetFrameCount(); i++ ) { Local<StackFrame> sf = st->GetFrame(i); char szOut[8 * 1024]; String::Utf8Value scriptName(sf->GetScriptName()); String::Utf8Value funcName(sf->GetFunctionName()); sprintf(szOut, "%s (%d,%d):%s\n", *scriptName, sf->GetLineNumber(), sf->GetColumn(), *funcName); OutputDebugStringA(szOut); } }
Handle<Value> LogJs::log(const Arguments& args, Log::WarningLevel level) { HandleScope scope; Context::Scope context_scope(Context::GetCurrent()); if (level >= Log::getInstance().getLevel()) { Local<StackTrace> stack = StackTrace::CurrentStackTrace(1); Local<StackFrame> frame = stack->GetFrame(0); int lineNumber = -1; QString script("<unknown>"); QString functionName("<unknown>"); if (stack->GetFrameCount() >= 1) { lineNumber = frame->GetLineNumber(); script = toString(frame->GetScriptName()); functionName = toString(frame->GetFunctionName()); } std::stringstream rMessage; for (int i = 0; i < args.Length(); i++) { if (i != 0) { rMessage << " "; } rMessage << args[i]; } QString message = QString::fromUtf8(rMessage.str().data()); int logLimit = ConfigOptions().getOgrLogLimit(); int messageCount = getLogCount(message); if (messageCount == logLimit) { message = QString("Received %1 of the same message. Silencing: ").arg(messageCount) + message; } if (messageCount <= logLimit) { Log::getInstance().log(level, message, script, functionName, lineNumber); } } return scope.Close(Undefined()); }