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