Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/lib/plugin/wasi_logging/func.cpp
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
// BUILTIN-PLUGIN: Temporary move the wasi-logging plugin sources here until
5
// the new plugin architecture 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 Buffer Pointer.
27
0
  char *CxtBuf = MemInst->getPointer<char *>(CxtPtr);
28
0
  char *MsgBuf = MemInst->getPointer<char *>(MsgPtr);
29
0
  if (CxtBuf == nullptr || MsgBuf == nullptr) {
30
0
    return Unexpect(ErrCode::Value::HostFuncError);
31
0
  }
32
33
  // Get Context and Message string_view
34
0
  std::string_view CxtSV(CxtBuf, CxtLen);
35
0
  std::string_view MsgSV(MsgBuf, MsgLen);
36
37
  // Setup Logger for Stdout or Stderr
38
0
  std::shared_ptr<spdlog::logger> Logger;
39
0
  if (CxtSV == "stdout"sv || CxtSV == ""sv) {
40
0
    Logger = Env.StdoutLogger;
41
0
  } else if (CxtSV == "stderr"sv) {
42
0
    Logger = Env.StderrLogger;
43
0
  } else {
44
0
    if (CxtSV != Env.getLogFileName()) {
45
0
      try {
46
0
        spdlog::drop(Env.getLogRegName());
47
0
        Env.FileLogger =
48
0
            spdlog::basic_logger_mt(Env.getLogRegName(), std::string(CxtSV));
49
0
        Env.FileLogger->set_level(spdlog::level::trace);
50
0
        Env.FileLogger->set_pattern(Env.DefFormat);
51
0
        Env.setLogFileName(CxtSV);
52
0
      } catch (const spdlog::spdlog_ex &Ex) {
53
0
        spdlog::error("[WasiLogging] Cannot log into file: {}"sv, Ex.what());
54
0
        return Unexpect(ErrCode::Value::HostFuncError);
55
0
      }
56
0
    }
57
0
    Logger = Env.FileLogger;
58
0
  }
59
60
  // Print Message by Logging Level
61
0
  switch (static_cast<LogLevel>(Level)) {
62
0
  case LogLevel::Trace:
63
0
    Logger->trace(MsgSV);
64
0
    break;
65
0
  case LogLevel::Debug:
66
0
    Logger->debug(MsgSV);
67
0
    break;
68
0
  case LogLevel::Info:
69
0
    Logger->info(MsgSV);
70
0
    break;
71
0
  case LogLevel::Warn:
72
0
    Logger->warn(MsgSV);
73
0
    break;
74
0
  case LogLevel::Error:
75
0
    Logger->error(MsgSV);
76
0
    break;
77
0
  case LogLevel::Critical:
78
0
    Logger->critical(MsgSV);
79
0
    break;
80
0
  default:
81
0
    spdlog::error("[WasiLogging] Unrecognized Logging Level: {}"sv, Level);
82
0
    spdlog::error("[WasiLogging] Trace Level = {}"sv,
83
0
                  static_cast<uint32_t>(LogLevel::Trace));
84
0
    spdlog::error("[WasiLogging] Debug Level = {}"sv,
85
0
                  static_cast<uint32_t>(LogLevel::Debug));
86
0
    spdlog::error("[WasiLogging] Info Level = {}"sv,
87
0
                  static_cast<uint32_t>(LogLevel::Info));
88
0
    spdlog::error("[WasiLogging] Warn Level = {}"sv,
89
0
                  static_cast<uint32_t>(LogLevel::Warn));
90
0
    spdlog::error("[WasiLogging] Error Level = {}"sv,
91
0
                  static_cast<uint32_t>(LogLevel::Error));
92
0
    spdlog::error("[WasiLogging] Critical Level = {}"sv,
93
0
                  static_cast<uint32_t>(LogLevel::Critical));
94
0
    return Unexpect(ErrCode::Value::HostFuncError);
95
0
  }
96
0
  return {};
97
0
}
98
99
} // namespace WASILogging
100
} // namespace Host
101
} // namespace WasmEdge