Skip to content

Commit

Permalink
feat(shared): add function name to timer/event too long warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vadzz-dev committed Feb 17, 2024
1 parent 166b00d commit a6f445b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
18 changes: 11 additions & 7 deletions shared/V8Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ v8::Local<v8::Object> V8Helpers::CreateCustomObject(v8::Isolate* isolate,
return obj;
}

inline static std::string GetStackFrameScriptName(v8::Local<v8::StackFrame> frame)
inline static std::string GetStackFrameScriptName(v8::Isolate* isolate, v8::Local<v8::StackFrame> frame)
{
v8::Local<v8::Value> 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())
Expand All @@ -170,21 +170,25 @@ V8Helpers::SourceLocation V8Helpers::SourceLocation::GetCurrent(v8::Isolate* iso
for(int i = 0; i < stackTrace->GetFrameCount(); i++)
{
v8::Local<v8::StackFrame> 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) : "<anonymous>";

bool isBytecode = false;
#ifdef ALT_CLIENT_API
isBytecode = resource ? static_cast<CV8ResourceImpl*>(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]", "<unknown>", 0, ctx };
}

V8Helpers::SourceLocation::SourceLocation(std::string&& _fileName, int _line, v8::Local<v8::Context> ctx) : fileName(_fileName), line(_line)
V8Helpers::SourceLocation::SourceLocation(const std::string& _fileName, const std::string& _funcName, int _line, v8::Local<v8::Context> ctx) : fileName(_fileName), funcName(_funcName), line(_line)
{
context.Reset(ctx->GetIsolate(), ctx);
}
Expand Down Expand Up @@ -214,7 +218,7 @@ V8Helpers::StackTrace V8Helpers::StackTrace::GetCurrent(v8::Isolate* isolate, V8
{
v8::Local<v8::StackFrame> 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<CV8ResourceImpl*>(resource)->GetModuleData(frameData.file).isBytecode : false;
Expand Down
9 changes: 8 additions & 1 deletion shared/V8Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ namespace V8Helpers
class SourceLocation
{
public:
SourceLocation(std::string&& fileName, int line, v8::Local<v8::Context> ctx);
SourceLocation(const std::string& fileName, const std::string& funcName, int line, v8::Local<v8::Context> ctx);

const std::string& GetFileName() const
{
return fileName;
}

const std::string& GetFuncName() const
{
return funcName;
}

int GetLineNumber() const
{
return line;
Expand All @@ -65,6 +71,7 @@ namespace V8Helpers

private:
CPersistent<v8::Context> context;
std::string funcName;
std::string fileName;
int line = 0;
};
Expand Down
21 changes: 6 additions & 15 deletions shared/V8ResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,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;
}
}

Expand Down Expand Up @@ -608,13 +601,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;
Expand Down

0 comments on commit a6f445b

Please sign in to comment.