VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
logger_plugin_interface.h
Go to the documentation of this file.
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 interface for pluggable logger backends loaded via the Plugin system.
27 *
28 * @details
29 * @c LoggerPluginInterface defines the contract that every custom logger plugin must implement.
30 * A plugin is a shared library (.so / .dll) that exports a factory function registered via
31 * the @c VLINK_PLUGIN_REGISTER macro and compiled with @c VLINK_PLUGIN_DECLARE.
32 *
33 * @par Plugin registration
34 * Each implementing class must embed @c VLINK_PLUGIN_REGISTER(LoggerPluginInterface) and
35 * provide a corresponding @c VLINK_PLUGIN_DECLARE in its translation unit so the @c Plugin
36 * loader can create and destroy instances dynamically.
37 *
38 * @par Loading a logger plugin
39 * @code
40 * vlink::Plugin plugin;
41 * auto logger_backend = plugin.load<vlink::LoggerPluginInterface>("my_logger_plugin.so");
42 *
43 * if (logger_backend) {
44 * logger_backend->init("my_app");
45 * logger_backend->log(vlink::Logger::kInfo, "Hello from plugin!");
46 * }
47 * @endcode
48 *
49 * @par Implementing a logger plugin
50 * @code
51 * class MyLogger : public vlink::LoggerPluginInterface {
52 * public:
53 * bool init(std::string_view app_name) override {
54 * // initialise your logging backend
55 * return true;
56 * }
57 * bool log(int level, std::string_view str) override {
58 * // forward to your backend
59 * return true;
60 * }
61 * };
62 * VLINK_PLUGIN_DECLARE(MyLogger)
63 * @endcode
64 *
65 * @note
66 * - The @p level parameter passed to @c log() corresponds to @c vlink::Logger::Level values.
67 * - Both methods should be implemented to be non-throwing; exceptions escaping a plugin
68 * boundary may cause undefined behaviour.
69 *
70 * @see Plugin, Logger
71 */
72
73#pragma once
74
75#include <string_view>
76
77#include "./plugin.h"
78
79namespace vlink {
80
81/**
82 * @class LoggerPluginInterface
83 * @brief Pure-virtual interface for a custom logger backend loaded as a dynamic plugin.
84 *
85 * @details
86 * Concrete implementations are loaded at runtime by @c Plugin::load<LoggerPluginInterface>().
87 * The @c VLINK_PLUGIN_REGISTER macro inside the class body wires up the factory/destroy
88 * functions that @c Plugin uses to manage the lifetime of the plugin instance.
89 */
92
93 protected:
95
96 virtual ~LoggerPluginInterface() = default;
97
98 public:
99 /**
100 * @brief Initialises the logger backend for the given application name.
101 *
102 * @details
103 * Called once by the host application after the plugin is loaded. The @p app_name
104 * string may be used to label log entries or configure a log file path.
105 *
106 * @param app_name Name of the calling application.
107 * @return @c true on success; @c false if initialisation failed and the plugin should
108 * not be used.
109 */
110 virtual bool init(std::string_view app_name) = 0;
111
112 /**
113 * @brief Writes a single log entry to the backend.
114 *
115 * @details
116 * Called from @c vlink::Logger internals whenever a log message passes the current
117 * log level filter. Implementations should be non-blocking to avoid stalling the
118 * caller thread.
119 *
120 * @param level Log severity level. Corresponds to @c vlink::Logger::Level values
121 * (e.g., @c kDebug, @c kInfo, @c kWarn, @c kError).
122 * @param str The fully-formatted log message string.
123 * @return @c true if the message was written successfully; @c false on error.
124 */
125 virtual bool log(int level, std::string_view str) = 0;
126
127 private:
129};
130
131} // namespace vlink
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184
Type-safe dynamic plugin loader with version checking and lifecycle management.
#define VLINK_PLUGIN_REGISTER(InterfaceType)
Macro to register a plugin, automatically deriving its ID from the interface type name.
Definition plugin.h:343