Skip to content

Commit

Permalink
chore: static dispatch on dumpaction (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas authored Oct 11, 2022
1 parent 7d3fbb2 commit 2a6eaba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
32 changes: 16 additions & 16 deletions src/commands/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "v8.h"

namespace xprofiler {
using nlohmann::json;
using std::make_pair;
using std::string;
using std::to_string;
Expand Down Expand Up @@ -318,9 +319,9 @@ static string CreateFilepath(string prefix, string ext) {
func; \
if (err.Fail()) return result;

template <typename T>
static json DoDumpAction(json command, DumpAction action, string prefix,
string ext, T* data, bool profiling, XpfError& err) {
template <DumpAction action, bool profiling, typename T>
static json DoDumpAction(json command, string prefix, string ext, T* data,
XpfError& err) {
json result;

// get traceid
Expand Down Expand Up @@ -419,19 +420,18 @@ static json DoDumpAction(json command, DumpAction action, string prefix,
return result;
}

#define V(func, data_type, action, profiling, prefix, ext) \
COMMAND_CALLBACK(func) { \
/* TODO(legendecas): smart pointers */ \
data_type* data = new data_type; \
XpfError err; \
json result = DoDumpAction<data_type>(command, action, #prefix, #ext, \
data, profiling, err); \
if (err.Fail()) { \
error(format("%s", err.GetErrMessage())); \
delete data; \
return; \
} \
success(result); \
#define V(func, data_type, action, profiling, prefix, ext) \
COMMAND_CALLBACK(func) { \
data_type* data = new data_type; \
XpfError err; \
json result = DoDumpAction<action, profiling, data_type>(command, #prefix, \
#ext, data, err); \
if (err.Fail()) { \
error(format("%s", err.GetErrMessage())); \
delete data; \
return; \
} \
success(result); \
}

// cpu profiling
Expand Down
6 changes: 2 additions & 4 deletions src/library/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
#include "logger.h"

namespace xprofiler {
using nlohmann::json;

void Sleep(int seconds);

std::string FmtMessage(const char* format, ...);

std::string ConvertTime(std::string format);

template <typename T>
T GetJsonValue(json data, std::string key, XpfError& err) {
T GetJsonValue(nlohmann::json data, std::string key, XpfError& err) {
T result = T();
try {
result = data[key].get<T>();
} catch (json::exception& e) {
} catch (nlohmann::json::exception& e) {
// format error message
// ref: https://en.cppreference.com/w/cpp/error/exception/what
char error_message[256];
Expand Down

0 comments on commit 2a6eaba

Please sign in to comment.