Coverage Report

Created: 2026-06-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/common/spdlog.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "common/spdlog.h"
5
6
#if defined(__clang_major__) && __clang_major__ >= 10
7
#pragma clang diagnostic push
8
// Suppression can be removed after spdlog with the fix is released.
9
// https://github.com/gabime/spdlog/pull/3198
10
#pragma clang diagnostic ignored "-Wextra-semi"
11
#endif
12
#if !defined(__has_include) || __has_include(<spdlog/sinks/callback_sink.h>)
13
#include <spdlog/sinks/callback_sink.h>
14
#else
15
// Fallback for spdlog without callback_sink.h (e.g. RHEL 9 / EPEL 9
16
// spdlog 1.10).
17
#include <functional>
18
#include <mutex>
19
#include <spdlog/sinks/base_sink.h>
20
#include <utility>
21
namespace spdlog::sinks {
22
template <typename Mutex> class callback_sink final : public base_sink<Mutex> {
23
public:
24
  explicit callback_sink(std::function<void(const details::log_msg &)> Callback)
25
      : Cb(std::move(Callback)) {}
26
27
protected:
28
  void sink_it_(const details::log_msg &Msg) override { Cb(Msg); }
29
  void flush_() override {}
30
31
private:
32
  std::function<void(const details::log_msg &)> Cb;
33
};
34
using callback_sink_mt = callback_sink<std::mutex>;
35
} // namespace spdlog::sinks
36
#endif
37
#if defined(__clang_major__) && __clang_major__ >= 10
38
#pragma clang diagnostic pop
39
#endif
40
#ifdef _WIN32
41
#include <spdlog/sinks/wincolor_sink.h>
42
using color_sink_t = spdlog::sinks::wincolor_stdout_sink_mt;
43
#else
44
#include <spdlog/sinks/ansicolor_sink.h>
45
using color_sink_t = spdlog::sinks::ansicolor_stdout_sink_mt;
46
#endif
47
48
using namespace std::literals;
49
50
namespace WasmEdge {
51
namespace Log {
52
53
0
void setLogOff() { spdlog::set_level(spdlog::level::off); }
54
55
0
void setTraceLoggingLevel() { spdlog::set_level(spdlog::level::trace); }
56
57
0
void setDebugLoggingLevel() { spdlog::set_level(spdlog::level::debug); }
58
59
0
void setInfoLoggingLevel() { spdlog::set_level(spdlog::level::info); }
60
61
0
void setWarnLoggingLevel() { spdlog::set_level(spdlog::level::warn); }
62
63
0
void setErrorLoggingLevel() { spdlog::set_level(spdlog::level::err); }
64
65
0
void setCriticalLoggingLevel() { spdlog::set_level(spdlog::level::critical); }
66
67
0
bool setLoggingLevelFromString(std::string_view Level) {
68
0
  if (Level == "off"sv) {
69
0
    setLogOff();
70
0
    return true;
71
0
  }
72
0
  if (Level == "trace"sv) {
73
0
    setTraceLoggingLevel();
74
0
    return true;
75
0
  }
76
0
  if (Level == "debug"sv) {
77
0
    setDebugLoggingLevel();
78
0
    return true;
79
0
  }
80
0
  if (Level == "info"sv) {
81
0
    setInfoLoggingLevel();
82
0
    return true;
83
0
  }
84
0
  if (Level == "warning"sv || Level == "warn"sv) {
85
0
    setWarnLoggingLevel();
86
0
    return true;
87
0
  }
88
0
  if (Level == "error"sv) {
89
0
    setErrorLoggingLevel();
90
0
    return true;
91
0
  }
92
0
  if (Level == "fatal"sv || Level == "critical"sv) {
93
0
    setCriticalLoggingLevel();
94
0
    return true;
95
0
  }
96
0
  return false;
97
0
}
98
99
void setLoggingCallback(
100
0
    std::function<void(const spdlog::details::log_msg &)> Callback) {
101
0
  if (Callback) {
102
0
    auto Callback_sink =
103
0
        std::make_shared<spdlog::sinks::callback_sink_mt>(Callback);
104
0
    spdlog::set_default_logger(
105
0
        std::make_shared<spdlog::logger>("WasmEdge"s, Callback_sink));
106
0
  } else {
107
0
    spdlog::set_default_logger(std::make_shared<spdlog::logger>(
108
0
        "WasmEdge"s, std::make_shared<color_sink_t>()));
109
0
  }
110
0
}
111
112
} // namespace Log
113
} // namespace WasmEdge