Coverage Report

Created: 2026-06-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/plugin/wasi_logging/func.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
// BUILTIN-PLUGIN: Temporarily move the wasi-logging plugin sources here until
5
// the new plugin architecture is ready in 0.15.0.
6
7
#include "plugin/wasi_logging/func.h"
8
9
#include <string_view>
10
11
namespace WasmEdge {
12
namespace Host {
13
namespace WASILogging {
14
15
using namespace std::literals;
16
17
Expect<void> Log::body(const Runtime::CallingFrame &Frame, uint32_t Level,
18
                       uint32_t CxtPtr, uint32_t CxtLen, uint32_t MsgPtr,
19
0
                       uint32_t MsgLen) {
20
  // Check memory instance from module.
21
0
  auto *MemInst = Frame.getMemoryByIndex(0);
22
0
  if (MemInst == nullptr) {
23
0
    return Unexpect(ErrCode::Value::HostFuncError);
24
0
  }
25
26
  // Get Context and Message string_view with full-extent bounds check.
27
0
  auto CxtSV = MemInst->getStringView(CxtPtr, CxtLen);
28
0
  auto MsgSV = MemInst->getStringView(MsgPtr, MsgLen);
29
0
  if (CxtSV.data() == nullptr || MsgSV.data() == nullptr) {
30
0
    return Unexpect(ErrCode::Value::HostFuncError);
31
0
  }
32
33
  // Setup Logger for Stdout or Stderr
34
0
  std::shared_ptr<spdlog::logger> Logger;
35
0
  if (CxtSV == "stdout"sv || CxtSV == ""sv) {
36
0
    Logger = Env.StdoutLogger;
37
0
  } else if (CxtSV == "stderr"sv) {
38
0
    Logger = Env.StderrLogger;
39
0
  } else {
40
0
    if (CxtSV != Env.getLogFileName()) {
41
0
      try {
42
0
        spdlog::drop(Env.getLogRegName());
43
0
        Env.FileLogger =
44
0
            spdlog::basic_logger_mt(Env.getLogRegName(), std::string(CxtSV));
45
0
        Env.FileLogger->set_level(spdlog::level::trace);
46
0
        Env.FileLogger->set_pattern(Env.DefFormat);
47
0
        Env.setLogFileName(CxtSV);
48
0
      } catch (const spdlog::spdlog_ex &Ex) {
49
0
        spdlog::error("[WasiLogging] Cannot log into file: {}"sv, Ex.what());
50
0
        return Unexpect(ErrCode::Value::HostFuncError);
51
0
      }
52
0
    }
53
0
    Logger = Env.FileLogger;
54
0
  }
55
56
  // Print Message by Logging Level
57
0
  switch (static_cast<LogLevel>(Level)) {
58
0
  case LogLevel::Trace:
59
0
    Logger->trace(MsgSV);
60
0
    break;
61
0
  case LogLevel::Debug:
62
0
    Logger->debug(MsgSV);
63
0
    break;
64
0
  case LogLevel::Info:
65
0
    Logger->info(MsgSV);
66
0
    break;
67
0
  case LogLevel::Warn:
68
0
    Logger->warn(MsgSV);
69
0
    break;
70
0
  case LogLevel::Error:
71
0
    Logger->error(MsgSV);
72
0
    break;
73
0
  case LogLevel::Critical:
74
0
    Logger->critical(MsgSV);
75
0
    break;
76
0
  default:
77
0
    spdlog::error("[WasiLogging] Unrecognized Logging Level: {}"sv, Level);
78
0
    spdlog::error("[WasiLogging] Trace Level = {}"sv,
79
0
                  static_cast<uint32_t>(LogLevel::Trace));
80
0
    spdlog::error("[WasiLogging] Debug Level = {}"sv,
81
0
                  static_cast<uint32_t>(LogLevel::Debug));
82
0
    spdlog::error("[WasiLogging] Info Level = {}"sv,
83
0
                  static_cast<uint32_t>(LogLevel::Info));
84
0
    spdlog::error("[WasiLogging] Warn Level = {}"sv,
85
0
                  static_cast<uint32_t>(LogLevel::Warn));
86
0
    spdlog::error("[WasiLogging] Error Level = {}"sv,
87
0
                  static_cast<uint32_t>(LogLevel::Error));
88
0
    spdlog::error("[WasiLogging] Critical Level = {}"sv,
89
0
                  static_cast<uint32_t>(LogLevel::Critical));
90
0
    return Unexpect(ErrCode::Value::HostFuncError);
91
0
  }
92
0
  return {};
93
0
}
94
95
} // namespace WASILogging
96
} // namespace Host
97
} // namespace WasmEdge