Lightweight 0.20250904.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/// Represents a logger for SQL operations.
22{
23 public:
24 /// Mandates the support for logging bind operations.
25 enum class SupportBindLogging : uint8_t
26 {
27 No,
28 Yes
29 };
30
31 LIGHTWEIGHT_API SqlLogger();
32 LIGHTWEIGHT_API SqlLogger(SqlLogger const& /*other*/) = default;
33 LIGHTWEIGHT_API SqlLogger(SqlLogger&& /*other*/) = default;
34 LIGHTWEIGHT_API SqlLogger& operator=(SqlLogger const& /*other*/) = default;
35 LIGHTWEIGHT_API SqlLogger& operator=(SqlLogger&& /*other*/) = default;
36 LIGHTWEIGHT_API virtual ~SqlLogger() = default;
37
38 /// Type definition for a function that writes messages.
39 using MessageWriter = std::function<void(std::string /*message*/)>;
40
41 /// Constructs a new logger.
42 ///
43 /// @param supportBindLogging Indicates if the logger should support bind logging.
44 LIGHTWEIGHT_API explicit SqlLogger(SupportBindLogging supportBindLogging, MessageWriter writer = {});
45
46 /// Sets the logging sink for the logger.
47 ///
48 /// @param writer A function that takes a message string and writes it to the desired output.
49 LIGHTWEIGHT_API void SetLoggingSink(MessageWriter writer = {});
50
51 /// Invoked on a warning.
52 virtual void OnWarning(std::string_view const& message) = 0;
53
54 /// Invoked on ODBC SQL error occurred.
55 virtual void OnError(SqlError errorCode, std::source_location sourceLocation = std::source_location::current()) = 0;
56
57 /// Invoked an ODBC SQL error occurred, with extended error information.
58 virtual void OnError(SqlErrorInfo const& errorInfo,
59 std::source_location sourceLocation = std::source_location::current()) = 0;
60
61 /// Invoked when a scoped code region needs to be timed and logged. The region starts with this call.
62 virtual void OnScopedTimerStart(std::string const& tag) = 0;
63
64 /// Invoked when a scoped code region needs to be timed and logged. The region ends with this call.
65 virtual void OnScopedTimerStop(std::string const& tag) = 0;
66
67 /// Invoked when a connection is opened.
68 virtual void OnConnectionOpened(SqlConnection const& connection) = 0;
69
70 /// Invoked when a connection is closed.
71 virtual void OnConnectionClosed(SqlConnection const& connection) = 0;
72
73 /// Invoked when a connection is idle.
74 virtual void OnConnectionIdle(SqlConnection const& connection) = 0;
75
76 /// Invoked when a connection is reused.
77 virtual void OnConnectionReuse(SqlConnection const& connection) = 0;
78
79 /// Invoked when a direct query is executed.
80 virtual void OnExecuteDirect(std::string_view const& query) = 0;
81
82 /// Invoked when a query is prepared.
83 virtual void OnPrepare(std::string_view const& query) = 0;
84
85 /// Invoked when an input parameter is bound.
86 template <typename T>
87 void OnBindInputParameter(std::string_view const& name, T&& value)
88 {
89 if (_supportsBindLogging)
90 {
91 using value_type = std::remove_cvref_t<T>;
92 if constexpr (SqlDataBinderSupportsInspect<value_type>)
93 {
94 OnBind(name, std::string(SqlDataBinder<value_type>::Inspect(std::forward<T>(value))));
95 }
96 }
97 }
98
99 /// Invoked when an input parameter is bound, by name.
100 virtual void OnBind(std::string_view const& name, std::string value) = 0;
101
102 /// Invoked when a prepared query is executed.
103 virtual void OnExecute(std::string_view const& query) = 0;
104
105 /// Invoked when a batch of queries is executed
106 virtual void OnExecuteBatch() = 0;
107
108 /// Invoked when a row is fetched.
109 virtual void OnFetchRow() = 0;
110
111 /// Invoked when fetching is done.
112 virtual void OnFetchEnd() = 0;
113
114 class Null;
115
116 /// Retrieves a null logger that does nothing.
117 LIGHTWEIGHT_API static Null& NullLogger() noexcept;
118
119 /// Retrieves a logger that logs to standard output.
120 LIGHTWEIGHT_API static SqlLogger& StandardLogger();
121
122 /// Retrieves a logger that logs to the trace logger.
123 LIGHTWEIGHT_API static SqlLogger& TraceLogger();
124
125 /// Retrieves the currently configured logger.
126 LIGHTWEIGHT_API static SqlLogger& GetLogger();
127
128 /// Sets the current logger.
129 ///
130 /// The ownership of the logger is not transferred and remains with the caller.
131 LIGHTWEIGHT_API static void SetLogger(SqlLogger& logger);
132
133 protected:
134 MessageWriter _messageWriter; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
135
136 private:
137 bool _supportsBindLogging = false;
138};
139
140class SqlLogger::Null: public SqlLogger
141{
142 public:
143 void OnWarning(std::string_view const& /*message*/) override {}
144 void OnError(SqlError /*errorCode*/, std::source_location /*sourceLocation*/) override {}
145 void OnError(SqlErrorInfo const& /*errorInfo*/, std::source_location /*sourceLocation*/) override {}
146 void OnScopedTimerStart(std::string const& /*tag*/) override {}
147 void OnScopedTimerStop(std::string const& /*tag*/) override {}
148 void OnConnectionOpened(SqlConnection const& /*connection*/) override {}
149 void OnConnectionClosed(SqlConnection const& /*connection*/) override {}
150 void OnConnectionIdle(SqlConnection const& /*connection*/) override {}
151 void OnConnectionReuse(SqlConnection const& /*connection*/) override {}
152 void OnExecuteDirect(std::string_view const& /*query*/) override {}
153 void OnPrepare(std::string_view const& /*qurey*/) override {}
154 void OnBind(std::string_view const& /*name*/, std::string /*value*/) override {}
155 void OnExecute(std::string_view const& /*query*/) override {}
156 void OnExecuteBatch() override {}
157 void OnFetchRow() override {}
158 void OnFetchEnd() override {}
159};
160
161/// A scoped timer for logging.
162///
163/// This class is used to measure the time spent in a code region and log it.
164/// This is typically useful for performance analysis, to identify bottlenecks.
165///
166/// @see SqlLogger
168{
169 public:
172 SqlScopedTimeLogger& operator=(SqlScopedTimeLogger const&) = default;
173 SqlScopedTimeLogger& operator=(SqlScopedTimeLogger&&) = delete;
174 explicit SqlScopedTimeLogger(std::string tag):
175 _tag { std::move(tag) }
176 {
177 SqlLogger::GetLogger().OnScopedTimerStart(_tag);
178 }
179
181 {
182 SqlLogger::GetLogger().OnScopedTimerStop(_tag);
183 }
184
185 private:
186 std::string _tag;
187};
188
189} // namespace Lightweight
Represents a connection to a SQL database.
Represents a logger for SQL operations.
Definition SqlLogger.hpp:22
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.
virtual void OnFetchEnd()=0
Invoked when fetching is done.
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:87
std::function< void(std::string)> MessageWriter
Type definition for a function that writes messages.
Definition SqlLogger.hpp:39
virtual void OnConnectionClosed(SqlConnection const &connection)=0
Invoked when a connection is closed.
SupportBindLogging
Mandates the support for logging bind operations.
Definition SqlLogger.hpp:26
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.
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={})
Represents an ODBC SQL error.
Definition SqlError.hpp:33