VLink  2.0.0
A high-performance communication middleware
logger_plugin_interface.h
浏览该文件的文档.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file logger_plugin_interface.h
26  * @brief Abstract contract for shared-library logger backends loaded via the VLink @c Plugin system.
27  *
28  * @details
29  * A logger plugin is a shared library that exports a factory through @c VLINK_PLUGIN_DECLARE and
30  * embeds @c VLINK_PLUGIN_REGISTER inside its concrete subclass. At runtime the host application
31  * uses @c vlink::Plugin::load<LoggerPluginInterface> to construct an instance and forwards every
32  * log record to its @c log method.
33  *
34  * @par Plugin contract
35  *
36  * | Hook | Direction | Contract |
37  * | ------------------------ | ---------------- | ------------------------------------------------ |
38  * | @c VLINK_PLUGIN_REGISTER | inside the class | Injects @c get_plugin_id() (ID = demangled name) |
39  * | @c VLINK_PLUGIN_DECLARE | translation unit | Exposes the factory and the version metadata |
40  * | @c init | host -> plugin | Called once after construction with the app name |
41  * | @c log | host -> plugin | Called per record after passing level filters |
42  * | Destructor | host -> plugin | Called when the host releases the plugin handle |
43  *
44  * @par Lifecycle
45  *
46  * @verbatim
47  * host plugin
48  * ---- ------
49  * Plugin::load<...> ----------> factory creates instance
50  * init(app_name) ----------> initialise backend
51  * log(level, msg) ----------> forward to backend (repeated)
52  * Plugin destroy ----------> destroy instance
53  * @endverbatim
54  *
55  * @par Loading example
56  * @code
57  * vlink::Plugin plugin;
58  * auto backend = plugin.load<vlink::LoggerPluginInterface>("my_logger_plugin", 1, 0);
59  *
60  * if (backend) {
61  * backend->init("my_app");
62  * backend->log(vlink::Logger::kInfo, "Hello from plugin!");
63  * }
64  * @endcode
65  *
66  * @par Implementation example
67  * @code
68  * class MyLogger : public vlink::LoggerPluginInterface {
69  * public:
70  * bool init(std::string_view app_name) override { return true; }
71  * bool log(int level, std::string_view str) override { return write_to_backend(level, str); }
72  * };
73  * VLINK_PLUGIN_DECLARE(MyLogger, 1, 0)
74  * @endcode
75  *
76  * @note @p level mirrors @c vlink::Logger::Level values. Both methods must avoid throwing
77  * across the plugin boundary; thrown exceptions there cause undefined behaviour.
78  *
79  * @see Plugin, Logger
80  */
81 
82 #pragma once
83 
84 #include <string_view>
85 
86 #include "./plugin.h"
87 
88 namespace vlink {
89 
90 /**
91  * @class LoggerPluginInterface
92  * @brief Pure-virtual interface that every shared-library logger backend implements.
93  *
94  * @details
95  * Concrete subclasses use @c VLINK_PLUGIN_REGISTER internally to expose their factory and
96  * destructor functions to the @c Plugin loader. Instances are owned by the host application
97  * for the duration of the plugin handle.
98  */
101 
102  protected:
104 
105  virtual ~LoggerPluginInterface() = default;
106 
107  public:
108  /**
109  * @brief Initialises the backend immediately after the plugin instance is constructed.
110  *
111  * @param app_name Calling application name; may inform log labels or file paths.
112  * @return @c true on success; @c false to indicate the plugin must not be used further.
113  */
114  virtual bool init(std::string_view app_name) = 0;
115 
116  /**
117  * @brief Writes a single record to the backend.
118  *
119  * @details
120  * Implementations should be non-blocking; @p str remains valid only for the duration of the
121  * call.
122  *
123  * @param level Severity using @c vlink::Logger::Level values.
124  * @param str Fully formatted record.
125  * @return @c true on success; @c false to indicate a write error.
126  */
127  virtual bool log(int level, std::string_view str) = 0;
128 
129  private:
131 };
132 
133 } // namespace vlink
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Strongly-typed shared-library plugin loader with ID and version verification.
#define VLINK_PLUGIN_REGISTER(InterfaceType)
Declares a plugin's identity from the demangled name of its abstract interface.
Definition: plugin.h:345