Lightweight 0.20260921.0
Loading...
Searching...
No Matches
SqlLogger.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Api.hpp"
6#include "SqlDataBinder.hpp"
7#include "SqlError.hpp"
8
9#include <functional>
10#include <source_location>
11#include <string_view>
12
13namespace Lightweight
14{
15
16class SqlConnection;
17
18struct SqlVariant;
19
20/// @ingroup CoreApi
21/// Represents a logger for SQL operations.
23{
24 public:
25 /// Mandates the support for logging bind operations.
26 enum class SupportBindLogging : uint8_t
27 {
28 No,
29 Yes
30 };
31
32 LIGHTWEIGHT_API SqlLogger();
33 /// Default copy constructor.
34 LIGHTWEIGHT_API SqlLogger(SqlLogger const& /*other*/) = default;
35 /// Default move constructor.
36 LIGHTWEIGHT_API SqlLogger(SqlLogger&& /*other*/) = default;
37 /// Default copy assignment operator.
38 LIGHTWEIGHT_API SqlLogger& operator=(SqlLogger const& /*other*/) = default;
39 /// Default move assignment operator.
40 LIGHTWEIGHT_API SqlLogger& operator=(SqlLogger&& /*other*/) = default;
41 LIGHTWEIGHT_API virtual ~SqlLogger() = default;
42
43 /// Type definition for a function that writes messages.
44 using MessageWriter = std::function<void(std::string /*message*/)>;
45
46 /// Constructs a new logger.
47 ///
48 /// @param supportBindLogging Indicates if the logger should support bind logging.
49 /// @param writer Optional message writer function for log output.
50 LIGHTWEIGHT_API explicit SqlLogger(SupportBindLogging supportBindLogging, MessageWriter writer = {});
51
52 /// Sets the logging sink for the logger.
53 ///
54 /// @param writer A function that takes a message string and writes it to the desired output.
55 LIGHTWEIGHT_API void SetLoggingSink(MessageWriter writer = {});
56
57 /// Invoked on a warning.
58 virtual void OnWarning(std::string_view const& message) = 0;
59
60 /// Invoked on ODBC SQL error occurred.
61 virtual void OnError(SqlError errorCode, std::source_location sourceLocation = std::source_location::current()) = 0;
62
63 /// Invoked an ODBC SQL error occurred, with extended error information.
64 virtual void OnError(SqlErrorInfo const& errorInfo,
65 std::source_location sourceLocation = std::source_location::current()) = 0;
66
67 /// Invoked when a scoped code region needs to be timed and logged. The region starts with this call.
68 virtual void OnScopedTimerStart(std::string const& tag) = 0;
69
70 /// Invoked when a scoped code region needs to be timed and logged. The region ends with this call.
71 virtual void OnScopedTimerStop(std::string const& tag) = 0;
72
73 /// Invoked when a connection is opened.
74 virtual void OnConnectionOpened(SqlConnection const& connection) = 0;
75
76 /// Invoked when a connection is closed.
77 virtual void OnConnectionClosed(SqlConnection const& connection) = 0;
78
79 /// Invoked when a connection is idle.
80 virtual void OnConnectionIdle(SqlConnection const& connection) = 0;
81
82 /// Invoked when a connection is reused.
83 virtual void OnConnectionReuse(SqlConnection const& connection) = 0;
84
85 /// Invoked when a direct query is executed.
86 virtual void OnExecuteDirect(std::string_view const& query) = 0;
87
88 /// Invoked when a query is prepared.
89 virtual void OnPrepare(std::string_view const& query) = 0;
90
91 /// Invoked when an input parameter is bound.
92 template <typename T>
93 void OnBindInputParameter(std::string_view const& name, T&& value)
94 {
95 if (_supportsBindLogging)
96 {
97 using value_type = std::remove_cvref_t<T>;
98 if constexpr (SqlDataBinderSupportsInspect<value_type>)
99 {
100 OnBind(name, std::string(SqlDataBinder<value_type>::Inspect(std::forward<T>(value))));
101 }
102 }
103 }
104
105 /// Invoked when an input parameter is bound, by name.
106 virtual void OnBind(std::string_view const& name, std::string value) = 0;
107
108 /// Invoked when a prepared query is executed.
109 virtual void OnExecute(std::string_view const& query) = 0;
110
111 /// Invoked when a batch of queries is executed
112 virtual void OnExecuteBatch() = 0;
113
114 /// Invoked when a row is fetched.
115 virtual void OnFetchRow() = 0;
116
117 /// @brief Invoked once per block-prefetch round-trip (one @c SQLFetchScroll that materialized a
118 /// whole row block on the transparent prefetch path), with the number of rows the block yielded.
119 ///
120 /// Non-pure with an empty default so existing loggers need no change; override to observe how many
121 /// network round-trips a query actually cost (N rows at depth D collapse to @c ceil(N/D) blocks).
122 /// The argument is the number of rows materialized by this block fetch (0 at end of result set).
123 virtual void OnFetchBlock(std::size_t /*rowsInBlock*/) {}
124
125 /// Invoked when fetching is done.
126 virtual void OnFetchEnd() = 0;
127
128 class Null;
129
130 /// Retrieves a null logger that does nothing.
131 LIGHTWEIGHT_API static Null& NullLogger() noexcept;
132
133 /// Retrieves a logger that logs to standard output.
134 LIGHTWEIGHT_API static SqlLogger& StandardLogger();
135
136 /// Retrieves a logger that logs to the trace logger.
137 LIGHTWEIGHT_API static SqlLogger& TraceLogger();
138
139 /// Retrieves the currently configured logger.
140 LIGHTWEIGHT_API static SqlLogger& GetLogger();
141
142 /// Sets the current logger.
143 ///
144 /// The ownership of the logger is not transferred and remains with the caller.
145 LIGHTWEIGHT_API static void SetLogger(SqlLogger& logger);
146
147 protected:
148 /// The function used to write log messages.
149 MessageWriter _messageWriter; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
150
151 private:
152 bool _supportsBindLogging = false;
153};
154
155class SqlLogger::Null: public SqlLogger
156{
157 public:
158 void OnWarning(std::string_view const& /*message*/) override {}
159 void OnError(SqlError /*errorCode*/, std::source_location /*sourceLocation*/) override {}
160 void OnError(SqlErrorInfo const& /*errorInfo*/, std::source_location /*sourceLocation*/) override {}
161 void OnScopedTimerStart(std::string const& /*tag*/) override {}
162 void OnScopedTimerStop(std::string const& /*tag*/) override {}
163 void OnConnectionOpened(SqlConnection const& /*connection*/) override {}
164 void OnConnectionClosed(SqlConnection const& /*connection*/) override {}
165 void OnConnectionIdle(SqlConnection const& /*connection*/) override {}
166 void OnConnectionReuse(SqlConnection const& /*connection*/) override {}
167 void OnExecuteDirect(std::string_view const& /*query*/) override {}
168 void OnPrepare(std::string_view const& /*qurey*/) override {}
169 void OnBind(std::string_view const& /*name*/, std::string /*value*/) override {}
170 void OnExecute(std::string_view const& /*query*/) override {}
171 void OnExecuteBatch() override {}
172 void OnFetchRow() override {}
173 void OnFetchEnd() override {}
174};
175
176/// A scoped timer for logging.
177///
178/// This class is used to measure the time spent in a code region and log it.
179/// This is typically useful for performance analysis, to identify bottlenecks.
180///
181/// @see SqlLogger
183{
184 public:
185 /// Default copy constructor.
188 /// Default copy assignment operator.
190 SqlScopedTimeLogger& operator=(SqlScopedTimeLogger&&) = delete;
191 /// Constructs a scoped time logger with the given tag.
192 explicit SqlScopedTimeLogger(std::string tag):
193 _tag { std::move(tag) }
194 {
195 SqlLogger::GetLogger().OnScopedTimerStart(_tag);
196 }
197
199 {
200 SqlLogger::GetLogger().OnScopedTimerStop(_tag);
201 }
202
203 private:
204 std::string _tag;
205};
206
207} // namespace Lightweight
Represents a connection to a SQL database.
MessageWriter _messageWriter
The function used to write log messages.
virtual void OnExecuteDirect(std::string_view const &query)=0
Invoked when a direct query is executed.
virtual void OnExecute(std::string_view const &query)=0
Invoked when a prepared query is executed.
virtual void OnError(SqlError errorCode, std::source_location sourceLocation=std::source_location::current())=0
Invoked on ODBC SQL error occurred.
static LIGHTWEIGHT_API Null & NullLogger() noexcept
Retrieves a null logger that does nothing.
static LIGHTWEIGHT_API SqlLogger & GetLogger()
Retrieves the currently configured logger.
LIGHTWEIGHT_API SqlLogger(SqlLogger const &)=default
Default copy constructor.
virtual void OnFetchEnd()=0
Invoked when fetching is done.
LIGHTWEIGHT_API SqlLogger & operator=(SqlLogger &&)=default
Default move assignment operator.
static LIGHTWEIGHT_API SqlLogger & TraceLogger()
Retrieves a logger that logs to the trace logger.
static LIGHTWEIGHT_API void SetLogger(SqlLogger &logger)
virtual void OnError(SqlErrorInfo const &errorInfo, std::source_location sourceLocation=std::source_location::current())=0
Invoked an ODBC SQL error occurred, with extended error information.
LIGHTWEIGHT_API void SetLoggingSink(MessageWriter writer={})
virtual void OnBind(std::string_view const &name, std::string value)=0
Invoked when an input parameter is bound, by name.
virtual void OnConnectionReuse(SqlConnection const &connection)=0
Invoked when a connection is reused.
static LIGHTWEIGHT_API SqlLogger & StandardLogger()
Retrieves a logger that logs to standard output.
virtual void OnPrepare(std::string_view const &query)=0
Invoked when a query is prepared.
virtual void OnExecuteBatch()=0
Invoked when a batch of queries is executed.
void OnBindInputParameter(std::string_view const &name, T &&value)
Invoked when an input parameter is bound.
Definition SqlLogger.hpp:93
LIGHTWEIGHT_API SqlLogger(SqlLogger &&)=default
Default move constructor.
std::function< void(std::string)> MessageWriter
Type definition for a function that writes messages.
Definition SqlLogger.hpp:44
virtual void OnConnectionClosed(SqlConnection const &connection)=0
Invoked when a connection is closed.
virtual void OnFetchBlock(std::size_t)
Invoked once per block-prefetch round-trip (one SQLFetchScroll that materialized a whole row block on...
SupportBindLogging
Mandates the support for logging bind operations.
Definition SqlLogger.hpp:27
virtual void OnScopedTimerStart(std::string const &tag)=0
Invoked when a scoped code region needs to be timed and logged. The region starts with this call.
virtual void OnConnectionIdle(SqlConnection const &connection)=0
Invoked when a connection is idle.
virtual void OnScopedTimerStop(std::string const &tag)=0
Invoked when a scoped code region needs to be timed and logged. The region ends with this call.
virtual void OnWarning(std::string_view const &message)=0
Invoked on a warning.
LIGHTWEIGHT_API SqlLogger & operator=(SqlLogger const &)=default
Default copy assignment operator.
virtual void OnFetchRow()=0
Invoked when a row is fetched.
virtual void OnConnectionOpened(SqlConnection const &connection)=0
Invoked when a connection is opened.
LIGHTWEIGHT_API SqlLogger(SupportBindLogging supportBindLogging, MessageWriter writer={})
SqlScopedTimeLogger & operator=(SqlScopedTimeLogger const &)=default
Default copy assignment operator.
SqlScopedTimeLogger(std::string tag)
Constructs a scoped time logger with the given tag.
SqlScopedTimeLogger(SqlScopedTimeLogger const &)=default
Default copy constructor.
Represents an ODBC SQL error.
Definition SqlError.hpp:32