Skip to content

Commit

Permalink
refactor: log with tid if possible
Browse files Browse the repository at this point in the history
PR-URL: #176
  • Loading branch information
hyj1991 authored May 8, 2022
1 parent eb97ced commit 1d81ae2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/commands/cpuprofiler/cpu_profile.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cpu_profile.h"

#include "cpu_profile_node.h"
#include "environment_data.h"
#include "library/writer.h"
#include "logger.h"
#include "xpf_v8.h"
Expand All @@ -15,11 +16,13 @@ void CpuProfile::DeleteCpuProfile(const v8::CpuProfile* profile) {

void CpuProfile::Serialize(v8::Isolate* isolate, CpuProfilePtr node,
std::string filename) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
HandleScope scope(isolate);
ofstream outfile;
outfile.open(filename, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
Error("cpu_profile", "open file %s failed.", filename.c_str());
ErrorT("cpu_profile", env_data->thread_id(), "open file %s failed.",
filename.c_str());
return;
}

Expand Down
38 changes: 22 additions & 16 deletions src/commands/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ void TransactionDone(string thread_name, string unique_key, XpfError& err) {

template <typename T>
T* GetProfilingData(void* data, string notify_type, string unique_key) {
Isolate* isolate = Isolate::GetCurrent();
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
T* dump_data = static_cast<T*>(data);
Debug(module_type, "<%s> %s action start.", notify_type.c_str(),
unique_key.c_str());
DebugT(module_type, env_data->thread_id(), "<%s> %s action start.",
notify_type.c_str(), unique_key.c_str());
return dump_data;
}

Expand All @@ -146,46 +148,49 @@ T* GetDumpData(void* data) {
}

void AfterDumpFile(string& filepath, string notify_type, string unique_key) {
Debug(module_type, "<%s> %s dump file: %s.", notify_type.c_str(),
unique_key.c_str(), filepath.c_str());
Isolate* isolate = Isolate::GetCurrent();
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
DebugT(module_type, env_data->thread_id(), "<%s> %s dump file: %s.",
notify_type.c_str(), unique_key.c_str(), filepath.c_str());
filepath = "";
}

} // namespace

#define CHECK_ERR(func) \
func; \
if (err.Fail()) { \
Debug(module_type, "<%s> %s error: %s", notify_type.c_str(), \
unique_key.c_str(), err.GetErrMessage()); \
return; \
#define CHECK_ERR(func) \
func; \
if (err.Fail()) { \
DebugT(module_type, env_data->thread_id(), "<%s> %s error: %s", \
notify_type.c_str(), unique_key.c_str(), err.GetErrMessage()); \
return; \
}

void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
BaseDumpData* dump_data = static_cast<BaseDumpData*>(data);
string traceid = dump_data->traceid;
DumpAction action = dump_data->action;
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);

// check transaction has been done
XpfError err;
string unique_key = traceid + "::" + Action2String(action);
TransactionDone(notify_type, unique_key, err);
if (err.Fail()) {
Debug(module_type, "%s", err.GetErrMessage());
DebugT(module_type, env_data->thread_id(), "%s", err.GetErrMessage());
request_map.erase(unique_key);
// clear dump_data
if (dump_data->run_once) {
Debug(module_type, "<%s> %s dump_data cleared.", notify_type.c_str(),
unique_key.c_str());
DebugT(module_type, env_data->thread_id(), "<%s> %s dump_data cleared.",
notify_type.c_str(), unique_key.c_str());
delete dump_data;
}
return;
}

// set action executing flag
request_map.insert(make_pair(unique_key, true));
Debug(module_type, "<%s> %s handled.", notify_type.c_str(),
unique_key.c_str());
DebugT(module_type, env_data->thread_id(), "<%s> %s handled.",
notify_type.c_str(), unique_key.c_str());

// check conflict action running
CHECK_ERR(ConflictActionRunning(action, err))
Expand Down Expand Up @@ -251,7 +256,8 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
break;
}
default:
Error(module_type, "not support dump action: %d", action);
ErrorT(module_type, env_data->thread_id(), "not support dump action: %d",
action);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/gcprofiler/gc_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void GcProfiler::StartGCProfiling(v8::Isolate* isolate, std::string filename) {
std::unique_ptr<GcProfiler> gc_profiler =
std::unique_ptr<GcProfiler>(new GcProfiler(isolate, filename));
if (!gc_profiler->is_open()) {
Error("gc_profiler", "open file %s failed.", filename.c_str());
ErrorT("gc_profiler", env_data->thread_id(), "open file %s failed.",
filename.c_str());
return;
}
env_data->gc_profiler = std::move(gc_profiler);
Expand Down
6 changes: 5 additions & 1 deletion src/commands/heapdump/heap_snapshot.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "heap_snapshot.h"

#include "environment_data.h"
#include "library/writer.h"
#include "logger.h"

Expand Down Expand Up @@ -45,9 +46,12 @@ class FileOutputStream final : public OutputStream {

void HeapSnapshot::Serialize(HeapSnapshotPointer profile,
std::string filename) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
FileOutputStream stream(filename);
if (!stream.is_open()) {
Error("heapdump", "open file %s failed.", filename.c_str());
ErrorT("heapdump", env_data->thread_id(), "open file %s failed.",
filename.c_str());
return;
}
profile->Serialize(&stream, v8::HeapSnapshot::kJSON);
Expand Down
5 changes: 4 additions & 1 deletion src/commands/heapprofiler/sampling_heap_profiler.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "sampling_heap_profiler.h"

#include "environment_data.h"
#include "library/writer.h"
#include "logger.h"
#include "xpf_v8.h"
Expand Down Expand Up @@ -47,9 +48,11 @@ void SamplingHeapProfiler::StartSamplingHeapProfiling(v8::Isolate* isolate) {

void SamplingHeapProfiler::StopSamplingHeapProfiling(v8::Isolate* isolate,
std::string filename) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
ofstream outfile(filename, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
Error("sampling_heap_profiler", "open file %s failed.", filename.c_str());
ErrorT("sampling_heap_profiler", env_data->thread_id(),
"open file %s failed.", filename.c_str());
return;
}
HandleScope scope(isolate);
Expand Down
5 changes: 4 additions & 1 deletion src/commands/listener.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "commands/dump.h"
#include "commands/parser.h"
#include "environment_data.h"
#include "logger.h"
#include "nan.h"
#include "platform/platform.h"
Expand Down Expand Up @@ -38,7 +39,9 @@ void RunCommandsListener(const FunctionCallbackInfo<Value>& info) {
info.GetReturnValue().Set(False());
return;
}
Info("init", "commands listener: listener thread created.");
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
InfoT("init", env_data->thread_id(),
"commands listener: listener thread created.");

info.GetReturnValue().Set(True());
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/report/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ void NodeReport::WriteNodeReport(JSONWriter* writer, std::string location,
void NodeReport::GetNodeReport(v8::Isolate* isolate, std::string filepath,
std::string location, std::string message,
bool fatal_error) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
NodeReport report(isolate);
ofstream outfile;
outfile.open(filepath, ios::out | ios::binary);
if (!outfile.is_open()) {
Error("node_report", "open file %s failed.", filepath.c_str());
ErrorT("node_report", env_data->thread_id(), "open file %s failed.",
filepath.c_str());
outfile.close();
return;
}
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/fatal_error.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "commands/coredumper/coredumper.h"
#include "commands/report/node_report.h"
#include "configure-inl.h"
#include "environment_data.h"
#include "library/utils.h"
#include "logger.h"
#include "nan.h"
Expand All @@ -24,26 +25,28 @@ constexpr char module_type[] = "fatal_error";
fflush(stderr);

Isolate* isolate = TryGetCurrentIsolate();
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
ThreadId thread_id = env_data->thread_id();

// generate report before abort
if (GetEnableFatalErrorReport()) {
string filepath = GetLogDir() + GetSep() + "x-fatal-error-" +
to_string(GetPid()) + "-" + ConvertTime("%Y%m%d") + "-" +
to_string(GetNextDiagFileId()) + ".diag";

Info(module_type, "dump report to %s.", filepath.c_str());
InfoT(module_type, thread_id, "dump report to %s.", filepath.c_str());
NodeReport::GetNodeReport(isolate, filepath, location, message, true);
Info(module_type, "report dumped.");
InfoT(module_type, thread_id, "report dumped.");
}

// generator core file before abort
if (GetEnableFatalErrorCoredump()) {
string filepath = GetLogDir() + GetSep() + "x-fatal-error-" +
to_string(GetPid()) + "-" + ConvertTime("%Y%m%d") + "-" +
to_string(GetNextDiagFileId()) + ".core";
Info(module_type, "dump core to %s.", filepath.c_str());
InfoT(module_type, thread_id, "dump core to %s.", filepath.c_str());
Coredumper::WriteCoredump(filepath);
Info(module_type, "core dumped.");
InfoT(module_type, thread_id, "core dumped.");
}

Abort();
Expand Down
4 changes: 2 additions & 2 deletions src/logbypass/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void AddSentRequest(const FunctionCallbackInfo<Value>& info) {
HttpStatistics* http_statistics = env_data->http_statistics();

if (!info[0]->IsNumber()) {
Error(module_type, "request cost must be number!");
ErrorT(module_type, env_data->thread_id(), "request cost must be number!");
return;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ void AddHttpStatusCode(const FunctionCallbackInfo<Value>& info) {
HttpStatistics* http_statistics = env_data->http_statistics();

if (!info[0]->IsNumber()) {
Error(module_type, "request cost must be number!");
ErrorT(module_type, env_data->thread_id(), "request cost must be number!");
return;
}

Expand Down
5 changes: 3 additions & 2 deletions src/logbypass/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ void LogByPass::Write(EnvironmentData* env_data, bool log_format_alinode) {

void RunLogBypass(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
ThreadId thread_id = env_data->thread_id();
// init gc hooks
InitGcStatusHooks(env_data);
Info("init", "logbypass: gc hooks setted.");
InfoT("init", thread_id, "logbypass: gc hooks setted.");

// init log thread
ProcessData* data = ProcessData::Get();
Mutex::ScopedLock lock(data->log_by_pass_mutex);
if (data->log_by_pass == nullptr) {
data->log_by_pass = std::unique_ptr<LogByPass>(new LogByPass());
data->log_by_pass->StartIfNeeded();
Info("init", "logbypass: log thread created.");
InfoT("init", thread_id, "logbypass: log thread created.");
}

info.GetReturnValue().Set(True());
Expand Down

0 comments on commit 1d81ae2

Please sign in to comment.