Skip to content

Commit 51c47c4

Browse files
author
Le Yao
committed
Avoid unnecessary log level checks
INFO: Analyzed 9 targets (5 packages loaded, 930 targets configured). INFO: Found 1 target and 8 test targets... INFO: Elapsed time: 5.281s, Critical Path: 0.02s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action //test:context_test (cached) PASSED in 0.1s //test:exports_test (cached) PASSED in 5.7s //test:null_vm_test (cached) PASSED in 0.1s //test:runtime_test (cached) PASSED in 2.0s //test:shared_data (cached) PASSED in 0.1s //test:shared_queue (cached) PASSED in 0.1s //test:vm_id_handle (cached) PASSED in 0.1s //test/common:bytecode_util_test (cached) PASSED in 0.1s Executed 0 out of 8 tests: 8 tests pass. INFO: Build completed successfully, 1 total action Signed-off-by: Le Yao <[email protected]>
1 parent 13048e4 commit 51c47c4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/wamr/wamr.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,16 @@ void Wamr::registerHostFunctionImpl(std::string_view module_name, std::string_vi
468468
store_.get(), type.get(),
469469
[](void *data, const wasm_val_t params[], wasm_val_t results[]) -> wasm_trap_t * {
470470
auto func_data = reinterpret_cast<HostFuncData *>(data);
471-
if (func_data->vm_->cmpLogLevel(LogLevel::trace)) {
471+
const bool log = func_data->vm_->cmpLogLevel(LogLevel::trace);
472+
if (log) {
472473
func_data->vm_->integration()->trace("[vm->host] " + func_data->name_ + "(" +
473474
printValues(params, sizeof...(Args)) + ")");
474475
}
475476
auto args_tuple = convertValTypesToArgsTuple<std::tuple<Args...>>(params);
476477
auto args = std::tuple_cat(std::make_tuple(current_context_), args_tuple);
477478
auto fn = reinterpret_cast<void (*)(void *, Args...)>(func_data->raw_func_);
478479
std::apply(fn, args);
479-
if (func_data->vm_->cmpLogLevel(LogLevel::trace)) {
480+
if (log) {
480481
func_data->vm_->integration()->trace("[vm<-host] " + func_data->name_ + " return: void");
481482
}
482483
return nullptr;
@@ -500,7 +501,8 @@ void Wamr::registerHostFunctionImpl(std::string_view module_name, std::string_vi
500501
store_.get(), type.get(),
501502
[](void *data, const wasm_val_t params[], wasm_val_t results[]) -> wasm_trap_t * {
502503
auto func_data = reinterpret_cast<HostFuncData *>(data);
503-
if (func_data->vm_->cmpLogLevel(LogLevel::trace)) {
504+
const bool log = func_data->vm_->cmpLogLevel(LogLevel::trace);
505+
if (log) {
504506
func_data->vm_->integration()->trace("[vm->host] " + func_data->name_ + "(" +
505507
printValues(params, sizeof...(Args)) + ")");
506508
}
@@ -509,7 +511,7 @@ void Wamr::registerHostFunctionImpl(std::string_view module_name, std::string_vi
509511
auto fn = reinterpret_cast<R (*)(void *, Args...)>(func_data->raw_func_);
510512
R res = std::apply(fn, args);
511513
assignVal<R>(res, results[0]);
512-
if (func_data->vm_->cmpLogLevel(LogLevel::trace)) {
514+
if (log) {
513515
func_data->vm_->integration()->trace("[vm<-host] " + func_data->name_ +
514516
" return: " + std::to_string(res));
515517
}
@@ -552,11 +554,12 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
552554

553555
*function = [func, function_name, this](ContextBase *context, Args... args) -> void {
554556
wasm_val_t params[] = {makeVal(args)...};
555-
SaveRestoreContext saved_context(context);
556-
if (cmpLogLevel(LogLevel::trace)) {
557+
const bool log = cmpLogLevel(LogLevel::trace);
558+
if (log) {
557559
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
558560
printValues(params, sizeof...(Args)) + ")");
559561
}
562+
SaveRestoreContext saved_context(context);
560563
WasmTrapPtr trap{wasm_func_call(func, params, nullptr)};
561564
if (trap) {
562565
WasmByteVec error_message;
@@ -566,7 +569,7 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
566569
std::string(error_message.get()->data, error_message.get()->size));
567570
return;
568571
}
569-
if (cmpLogLevel(LogLevel::trace)) {
572+
if (log) {
570573
integration()->trace("[host<-vm] " + std::string(function_name) + " return: void");
571574
}
572575
};
@@ -598,11 +601,12 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
598601
*function = [func, function_name, this](ContextBase *context, Args... args) -> R {
599602
wasm_val_t params[] = {makeVal(args)...};
600603
wasm_val_t results[1];
601-
SaveRestoreContext saved_context(context);
602-
if (cmpLogLevel(LogLevel::trace)) {
604+
const bool log = cmpLogLevel(LogLevel::trace);
605+
if (log) {
603606
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
604607
printValues(params, sizeof...(Args)) + ")");
605608
}
609+
SaveRestoreContext saved_context(context);
606610
WasmTrapPtr trap{wasm_func_call(func, params, results)};
607611
if (trap) {
608612
WasmByteVec error_message;
@@ -613,7 +617,7 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
613617
return R{};
614618
}
615619
R ret = convertValueTypeToArg<R>(results[0]);
616-
if (cmpLogLevel(LogLevel::trace)) {
620+
if (log) {
617621
integration()->trace("[host<-vm] " + std::string(function_name) +
618622
" return: " + std::to_string(ret));
619623
}

0 commit comments

Comments
 (0)