/src/WasmEdge/include/plugin/wasi_logging/env.h
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 | | #pragma once |
8 | | |
9 | | #include "common/hexstr.h" |
10 | | #include "common/spdlog.h" |
11 | | |
12 | | #include <spdlog/sinks/basic_file_sink.h> |
13 | | #include <spdlog/sinks/stdout_color_sinks.h> |
14 | | #include <spdlog/spdlog.h> |
15 | | |
16 | | #include <limits> |
17 | | #include <memory> |
18 | | #include <mutex> |
19 | | #include <random> |
20 | | #include <string> |
21 | | #include <string_view> |
22 | | #include <unordered_set> |
23 | | |
24 | | namespace WasmEdge { |
25 | | namespace Host { |
26 | | namespace WASILogging { |
27 | | |
28 | | class LogEnv { |
29 | | public: |
30 | 0 | LogEnv() noexcept { |
31 | 0 | using namespace std::literals; |
32 | | // Get the stdout and stderr logger. |
33 | 0 | StdoutLogger = spdlog::get("wasi_logging_stdout"s); |
34 | 0 | if (!StdoutLogger) { |
35 | 0 | StdoutLogger = spdlog::stdout_color_mt("wasi_logging_stdout"s); |
36 | 0 | StdoutLogger->set_level(spdlog::level::trace); |
37 | 0 | StdoutLogger->set_pattern(DefFormat); |
38 | 0 | } |
39 | 0 | StderrLogger = spdlog::get("wasi_logging_stderr"s); |
40 | 0 | if (!StderrLogger) { |
41 | 0 | StderrLogger = spdlog::stderr_color_mt("wasi_logging_stderr"s); |
42 | 0 | StderrLogger->set_level(spdlog::level::trace); |
43 | 0 | StderrLogger->set_pattern(DefFormat); |
44 | 0 | } |
45 | |
|
46 | 0 | std::random_device RandDev; |
47 | 0 | std::mt19937 RandGen(RandDev()); |
48 | 0 | std::uniform_int_distribution<uint64_t> RandDist( |
49 | 0 | 0, std::numeric_limits<uint64_t>::max()); |
50 | |
|
51 | 0 | std::unique_lock Lock(Mutex); |
52 | 0 | do { |
53 | 0 | InstanceID = RandDist(RandGen); |
54 | 0 | } while (RegisteredID.find(InstanceID) != RegisteredID.cend()); |
55 | 0 | LogRegName = "wasi_logging_file_" + convertUIntToHexStr(InstanceID); |
56 | 0 | RegisteredID.insert(InstanceID); |
57 | 0 | } |
58 | | |
59 | 0 | ~LogEnv() noexcept { |
60 | 0 | std::unique_lock Lock(Mutex); |
61 | 0 | spdlog::drop(LogFileName); |
62 | 0 | RegisteredID.erase(InstanceID); |
63 | 0 | } |
64 | | |
65 | 0 | std::string_view getLogFileName() const noexcept { return LogFileName; } |
66 | 0 | void setLogFileName(std::string_view Name) noexcept { |
67 | 0 | LogFileName = std::string(Name); |
68 | 0 | } |
69 | | |
70 | 0 | const std::string &getLogRegName() const noexcept { return LogRegName; } |
71 | | |
72 | 0 | uint64_t getInstanceID() const noexcept { return InstanceID; } |
73 | | |
74 | | static std::mutex Mutex; |
75 | | static std::unordered_set<uint64_t> RegisteredID; |
76 | | static const std::string DefFormat; |
77 | | std::shared_ptr<spdlog::logger> StdoutLogger; |
78 | | std::shared_ptr<spdlog::logger> StderrLogger; |
79 | | std::shared_ptr<spdlog::logger> FileLogger; |
80 | | |
81 | | private: |
82 | | std::string LogFileName; |
83 | | std::string LogRegName; |
84 | | uint64_t InstanceID; |
85 | | }; |
86 | | |
87 | | } // namespace WASILogging |
88 | | } // namespace Host |
89 | | } // namespace WasmEdge |