Kagome
Polkadot Runtime Engine in C++17
io_extension.cpp
Go to the documentation of this file.
1 
7 
8 #include <boost/assert.hpp>
9 
10 #include "runtime/memory.hpp"
12 #include "runtime/ptr_size.hpp"
13 
14 namespace kagome::host_api {
16  std::shared_ptr<const runtime::MemoryProvider> memory_provider)
17  : memory_provider_(std::move(memory_provider)),
18  logger_{log::createLogger("IoExtension", "io_extension")} {
19  BOOST_ASSERT_MSG(memory_provider_ != nullptr, "memory provider is nullptr");
20  }
21 
23  runtime::WasmSpan target,
24  runtime::WasmSpan message) {
26 
27  auto read_str_from_position = [&](runtime::PtrSize location) {
28  return memory_provider_->getCurrentMemory()->get().loadStr(location.ptr,
29  location.size);
30  };
31 
32  const auto target_str = read_str_from_position(runtime::PtrSize(target));
33  const auto message_str = read_str_from_position(runtime::PtrSize(message));
34 
35  switch (static_cast<WasmLogLevel>(level)) {
37  logger_->error("target: {}, message: {}", target_str, message_str);
38  break;
39  case WasmLogLevel::Warn:
40  logger_->warn("target: {}, message: {}", target_str, message_str);
41  break;
42  case WasmLogLevel::Info:
43  logger_->info("target: {}, message: {}", target_str, message_str);
44  break;
45  case WasmLogLevel::Debug:
46  SL_DEBUG(logger_, "target: {}, message: {}", target_str, message_str);
47  break;
48  case WasmLogLevel::Trace:
49  SL_TRACE(logger_, "target: {}, message: {}", target_str, message_str);
50  break;
51  default: {
52  BOOST_UNREACHABLE_RETURN();
53  logger_->error(
54  "Message with incorrect log level. Target: {}, message: {}",
55  target_str,
56  message_str);
57  }
58  }
59  }
60 
63  using soralog::Level;
64 
65  switch (logger_->level()) {
66  case Level::OFF:
67  case Level::CRITICAL:
68  case Level::ERROR:
69  return static_cast<runtime::WasmEnum>(WasmLogLevel::Error);
70  case Level::WARN:
71  return static_cast<runtime::WasmEnum>(WasmLogLevel::Warn);
72  case Level::INFO:
73  case Level::VERBOSE:
74  return static_cast<runtime::WasmEnum>(WasmLogLevel::Info);
75  case Level::DEBUG:
76  return static_cast<runtime::WasmEnum>(WasmLogLevel::Debug);
77  case Level::TRACE:
78  return static_cast<runtime::WasmEnum>(WasmLogLevel::Trace);
79  default:
80  return static_cast<runtime::WasmEnum>(WasmLogLevel::Error);
81  }
82  }
83 
84 } // namespace kagome::host_api
void ext_logging_log_version_1(runtime::WasmEnum level, runtime::WasmSpan target, runtime::WasmSpan message)
STL namespace.
soralog::Level Level
Definition: logger.hpp:22
IOExtension(std::shared_ptr< const runtime::MemoryProvider > memory_provider)
WasmLogLevel
type of wasm log levels
Definition: types.hpp:16
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
uint32_t WasmEnum
Enum value is uint32_t.
Definition: types.hpp:39
runtime::WasmEnum ext_logging_max_level_version_1()
std::shared_ptr< const runtime::MemoryProvider > memory_provider_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112