From 3f5b141c9bf97e365c8c0f7a39e28e41bb4bc084 Mon Sep 17 00:00:00 2001 From: Vadim Zubkov Date: Sun, 18 Feb 2024 02:07:52 +0300 Subject: [PATCH] feat(shared): add function name to timer/event too long warnings --- shared/V8Helpers.cpp | 18 +++++++++++------- shared/V8Helpers.h | 9 ++++++++- shared/V8ResourceImpl.cpp | 21 ++++++--------------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/shared/V8Helpers.cpp b/shared/V8Helpers.cpp index a4c38a6c6..c98afeca4 100644 --- a/shared/V8Helpers.cpp +++ b/shared/V8Helpers.cpp @@ -141,10 +141,10 @@ v8::Local V8Helpers::CreateCustomObject(v8::Isolate* isolate, return obj; } -inline static std::string GetStackFrameScriptName(v8::Local frame) +inline static std::string GetStackFrameScriptName(v8::Isolate* isolate, v8::Local frame) { v8::Local name = frame->GetScriptName(); - if(!name.IsEmpty()) return *v8::String::Utf8Value(v8::Isolate::GetCurrent(), name); + if(!name.IsEmpty()) return *v8::String::Utf8Value(isolate, name); else if(frame->IsEval()) return "[eval]"; else if(frame->IsWasm()) @@ -163,21 +163,25 @@ V8Helpers::SourceLocation V8Helpers::SourceLocation::GetCurrent(v8::Isolate* iso for(int i = 0; i < stackTrace->GetFrameCount(); i++) { v8::Local frame = stackTrace->GetFrame(isolate, i); + auto jsFuncName = frame->GetFunctionName(); + if(frame->GetScriptName().IsEmpty() && !frame->IsEval() && !frame->IsWasm() && frame->IsUserJavaScript()) continue; - std::string name = GetStackFrameScriptName(frame); + std::string name = GetStackFrameScriptName(isolate, frame); + std::string funcName = !jsFuncName.IsEmpty() ? *v8::String::Utf8Value(isolate, jsFuncName) : ""; + bool isBytecode = false; #ifdef ALT_CLIENT_API isBytecode = resource ? static_cast(resource)->GetModuleData(name).isBytecode : false; #endif int line = isBytecode ? 0 : frame->GetLineNumber(); - return SourceLocation{ std::move(name), line, ctx }; + return SourceLocation{ name, funcName, line, ctx }; } - return SourceLocation{ "[unknown]", 0, ctx }; + return SourceLocation{ "[unknown]", "", 0, ctx }; } -V8Helpers::SourceLocation::SourceLocation(std::string&& _fileName, int _line, v8::Local ctx) : fileName(_fileName), line(_line) +V8Helpers::SourceLocation::SourceLocation(const std::string& _fileName, const std::string& _funcName, int _line, v8::Local ctx) : fileName(_fileName), funcName(_funcName), line(_line) { context.Reset(ctx->GetIsolate(), ctx); } @@ -207,7 +211,7 @@ V8Helpers::StackTrace V8Helpers::StackTrace::GetCurrent(v8::Isolate* isolate, V8 { v8::Local frame = stackTrace->GetFrame(isolate, i); Frame frameData; - frameData.file = GetStackFrameScriptName(frame); + frameData.file = GetStackFrameScriptName(isolate, frame); bool isBytecode = false; #ifdef ALT_CLIENT_API isBytecode = resource ? static_cast(resource)->GetModuleData(frameData.file).isBytecode : false; diff --git a/shared/V8Helpers.h b/shared/V8Helpers.h index 78f24c31a..b93093ba6 100644 --- a/shared/V8Helpers.h +++ b/shared/V8Helpers.h @@ -48,12 +48,18 @@ namespace V8Helpers class SourceLocation { public: - SourceLocation(std::string&& fileName, int line, v8::Local ctx); + SourceLocation(const std::string& fileName, const std::string& funcName, int line, v8::Local ctx); const std::string& GetFileName() const { return fileName; } + + const std::string& GetFuncName() const + { + return funcName; + } + int GetLineNumber() const { return line; @@ -65,6 +71,7 @@ namespace V8Helpers private: CPersistent context; + std::string funcName; std::string fileName; int line = 0; }; diff --git a/shared/V8ResourceImpl.cpp b/shared/V8ResourceImpl.cpp index 884c67026..b36a5e57f 100644 --- a/shared/V8ResourceImpl.cpp +++ b/shared/V8ResourceImpl.cpp @@ -100,15 +100,8 @@ void V8ResourceImpl::OnTick() { auto& location = p.second->GetLocation(); - if(location.GetLineNumber() != 0) - { - Log::Warning << "Timer at " << resource->GetName() << ":" << location.GetFileName() << ":" << location.GetLineNumber() << " was too long " << GetTime() - time << "ms" - << Log::Endl; - } - else - { - Log::Warning << "Timer at " << resource->GetName() << ":" << location.GetFileName() << " was too long " << GetTime() - time << "ms" << Log::Endl; - } + Log::Warning << "Timer at " << location.GetFuncName() << " (" << resource->GetName() << ":" << location.GetFileName() + << ":" << location.GetLineNumber() << ") was too long " << (GetTime() - time) << "ms" << Log::Endl; } } @@ -625,13 +618,11 @@ void V8ResourceImpl::InvokeEventHandlers(const alt::CEvent* ev, const std::vecto return true; }); - if(GetTime() - time > 50 && !waitForPromiseResolve) + if (GetTime() - time > 50 && !waitForPromiseResolve) { - if(handler->location.GetLineNumber() != 0) - Log::Warning << "Event handler at " << resource->GetName() << ":" << handler->location.GetFileName() << ":" << handler->location.GetLineNumber() << " was too long " - << (GetTime() - time) << "ms" << Log::Endl; - else - Log::Warning << "Event handler at " << resource->GetName() << ":" << handler->location.GetFileName() << " was too long " << (GetTime() - time) << "ms" << Log::Endl; + Log::Warning << "Event handler at " << handler->location.GetFuncName() << " (" << resource->GetName() << ":" << handler->location.GetFileName() << ":" + << handler->location.GetLineNumber() + << ") was too long " << (GetTime() - time) << "ms" << Log::Endl; } if(handler->once) handler->removed = true;