-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/issue 869 backport console log (#870)
* add 'log' function to JavaScript UDA/UDF for debugging. close #4912 (#4997) * minor refinement for javascript log console (#5018) * change LOG_DEBUG to LOG INFO and add try-catch (#5631) * change LOG_DEBUG to LOG INFO and add try-catch * clang-format * add map,set transform * remove useless include * fix some comments * move comments * format code * fix last comments * fix catch * fix compile error --------- Co-authored-by: sunset3000 <tian.fan@timeplus.io> Co-authored-by: Ken Chen <zlchen.ken@gmail.com> Co-authored-by: chhtimeplus <162944718+chhtimeplus@users.noreply.github.com>
- Loading branch information
1 parent
0cc5119
commit dccb20f
Showing
6 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <V8/ConvertDataTypes.h> | ||
#include <V8/Modules/Console.h> | ||
|
||
#include <Common/ProtonCommon.h> | ||
#include <Common/logger_useful.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
namespace V8 | ||
{ | ||
|
||
void log(const v8::FunctionCallbackInfo<v8::Value> & args) | ||
{ | ||
v8::Isolate * isolate = args.GetIsolate(); | ||
assert(args.Length() >= 0); | ||
std::vector<String> result(args.Length()); | ||
try | ||
{ | ||
auto context = isolate->GetCurrentContext(); | ||
for (int i = 0; i < args.Length(); i++) | ||
{ | ||
if (args[i]->IsString() || args[i]->IsNumber()) | ||
{ | ||
result[i] = from_v8<String>(isolate, args[i]); | ||
} | ||
else if (args[i]->IsObject()) | ||
{ | ||
/// trans Object to String | ||
bool is_key_value = false; | ||
auto obj = args[i].As<v8::Object>(); | ||
/// trans Map,Set to String | ||
auto array = obj->PreviewEntries(&is_key_value); | ||
if (!array.IsEmpty()) | ||
result[i] = from_v8<String>(isolate, array.ToLocalChecked()->ToString(context).ToLocalChecked()); | ||
else if (obj->IsRegExp()) | ||
result[i] = from_v8<String>(isolate, obj->ToString(context).ToLocalChecked()); | ||
else | ||
result[i] = from_v8<String>(isolate, v8::JSON::Stringify(context, obj).ToLocalChecked()); | ||
} | ||
else | ||
{ | ||
/// default trans to String | ||
result[i] = from_v8<String>(isolate, args[i]->ToString(context).ToLocalChecked()); | ||
} | ||
} | ||
LOG_INFO(&Poco::Logger::get(from_v8<String>(isolate, args.Data())), "{}", fmt::join(result, " ")); | ||
} | ||
catch (DB::Exception & e) | ||
{ | ||
LOG_ERROR(&Poco::Logger::get(from_v8<String>(isolate, args.Data())), "Hit an udf/uda error : {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
tryLogCurrentException(__PRETTY_FUNCTION__); | ||
} | ||
} | ||
|
||
v8::Local<v8::Object> WrapObject(v8::Isolate * isolate, const std::string & func_name) | ||
{ | ||
v8::EscapableHandleScope handle_scope(isolate); | ||
|
||
v8::Local<v8::ObjectTemplate> module = v8::ObjectTemplate::New(isolate); | ||
|
||
module->SetInternalFieldCount(1); | ||
|
||
/// add 'log' function | ||
v8::Local<v8::Value> logger_name = to_v8(isolate, fmt::format("{}({})", DB::ProtonConsts::PROTON_JAVASCRIPT_UDF_LOGGER_PREFIX, func_name)); | ||
module->Set(isolate, "log", v8::FunctionTemplate::New(isolate, log, logger_name)); | ||
|
||
/// create instance | ||
v8::Local<v8::Object> result = module->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); | ||
|
||
return handle_scope.Escape(result); | ||
} | ||
|
||
void installConsole(v8::Isolate * isolate, v8::Local<v8::Context> & ctx, const std::string & func_name) | ||
{ | ||
v8::Local<v8::Object> console = WrapObject(isolate, func_name); | ||
ctx->Global()->Set(ctx, to_v8(isolate, "console"), console).Check(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <v8.h> | ||
|
||
#include <string> | ||
|
||
namespace DB | ||
{ | ||
|
||
namespace V8 | ||
{ | ||
/// Install the module console to the specified v8 context, | ||
/// \param func_name the function name of Javascript UDF or UDA, which is used to get the corresponding function logger | ||
void installConsole(v8::Isolate * isolate, v8::Local<v8::Context> & ctx, const std::string & func_name); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters