-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathLogSqliteDatabase.hpp
More file actions
345 lines (286 loc) · 13.6 KB
/
LogSqliteDatabase.hpp
File metadata and controls
345 lines (286 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef AREG_AREGEXTEND_DB_LOGSQLITEDATABASE_HPP
#define AREG_AREGEXTEND_DB_LOGSQLITEDATABASE_HPP
/************************************************************************
* This file is part of the AREG SDK core engine.
* AREG SDK is dual-licensed under Free open source (Apache version 2.0
* License) and Commercial (with various pricing models) licenses, depending
* on the nature of the project (commercial, research, academic or free).
* You should have received a copy of the AREG SDK license description in LICENSE.txt.
* If not, please contact to info[at]aregtech.com
*
* \copyright (c) 2017-2023 Aregtech UG. All rights reserved.
* \file aregextend/db/LogSqliteDatabase.hpp
* \author Artak Avetyan
* \ingroup AREG platform, extended library, SQLite Database log file.
************************************************************************/
/************************************************************************
* Include files.
************************************************************************/
#include "areg/base/GEGlobal.h"
#include "areg/logging/IELogDatabaseEngine.hpp"
#include "areg/base/String.hpp"
#if 0
#include <vector>
enum eLogItemType
{
ItemUnknown = 0 //!< Unknown log item type
, ItemInstance //!< Log instance item
, ItemThread //!< Log thread item
, ItemScope //!< Log scope item
, ItemPriority //!< Log priority item
};
struct sLogItem
{
ITEM_ID itemId { 0 }; //<! The ID of the log item
String itemName{ }; //!< The name of the log item
};
struct sLogItemList
{
eLogItemType itemType{ ItemUnknown }; //!< The type of the log item
std::vector<sLogItem> itemList{ }; //!< The list of log items
};
#endif
//////////////////////////////////////////////////////////////////////////
// LogSqliteDatabase class declaration
//////////////////////////////////////////////////////////////////////////
/**
* \brief The logging database engine, responsible to log messages in the database.
**/
class LogSqliteDatabase : public IELogDatabaseEngine
{
//////////////////////////////////////////////////////////////////////////
// Constructor / Destructor
//////////////////////////////////////////////////////////////////////////
public:
LogSqliteDatabase(void);
virtual ~LogSqliteDatabase(void);
//////////////////////////////////////////////////////////////////////////
// Attributes
//////////////////////////////////////////////////////////////////////////
public:
/**
* \brief Returns true if logging in the database is enabled.
* If logging is disabled, no database operation is performed.
**/
inline bool isDabataseLoggingEnabled(void) const;
/**
* \brief Enables or disables the database logging.
* This flag should be set before initializing database, where the
* database file and data tables are created and initialized.
* \param enable Flag, indicating whether the logging in the database is enabled or not.
**/
inline void setDatabaseLoggingEnabled(bool enable);
/**
* \brief Returns database file path.
**/
inline const String& getDatabasePath(void) const;
/**
* \brief Returns the initial database file path. The initial file path may contain mask like timestamp,
* so that each time database is disconnected and connected again with the empty file path,
* it creates new file.
**/
inline const String& getInitialDatabasePath(void) const;
//////////////////////////////////////////////////////////////////////////
// Overrides
//////////////////////////////////////////////////////////////////////////
public:
/************************************************************************/
// IEDatabaseEngine interface overrides.
/************************************************************************/
/**
* \brief Returns true if SqliteDatabase engine is opened and operable.
* Otherwise, returns false.
**/
virtual bool isOperable(void) const override;
/**
* \brief Connects to the specified SqliteDatabase.
* \param dbPath The path to the SqliteDatabase. If needed, the path may contain
* file path or URL, user name and password. It is up to
* SqliteDatabase engine to parse the path and initialize the connection.
* If the parameter is empty, it should take the data from the
* 'areg.init' configuration file.
* \return Returns true if succeeded to connect. Otherwise, returns false.
**/
virtual bool connect(const String & dbPath) override;
/**
* \brief Disconnects connected SqliteDatabase.
**/
virtual void disconnect(void) override;
/**
* \brief Execute the SQL script.
* \param sql The SQL script to execute.
* \return Returns true if succeeds to execute the SQL script.
**/
virtual bool execute(const String & sql) override;
/**
* \brief Call if need to make multiple operation. This call starts the transaction,
* that is required either commit or rollback call to complete the transaction.
**/
virtual bool begin(void) override;
/**
* \brief Commits or rolls back the SqliteDatabase changes and returns true if succeeded.
* \param doCommit If true, the SqliteDatabase engine should commit the changes.
* Otherwise, the SqliteDatabase engine should rollback the changes.
* \return Returns true if operation succeeded. Otherwise, returns false.
**/
virtual bool commit(bool doCommit) override;
/************************************************************************/
// IELogDatabaseEngine interface overrides.
/************************************************************************/
/**
* \brief Returns true if the database and the log tables are initialized,
* and ready to log messages.
**/
virtual bool tablesInitialized(void) const override;
/**
* \brief Called when logging message should be saved in the database.
* \param message The structure of the message to log.
* \param timestamp The timestamp to register when the message is logged.
* \return Returns true if succeeded to save the log in the database.
**/
virtual bool logMessage(const NELogging::sLogMessage & message, const DateTime & timestamp) override;
/**
* \brief Called when need to log information about log source instance.
* \param instance The structure of the logging message source to save in database.
* \param timestamp The timestamp to register when the instance is logged.
* \return Returns true if succeeded to save the log instance in the database.
**/
virtual bool logInstanceConnected(const NEService::sServiceConnectedInstance & instance, const DateTime & timestamp) override;
/**
* \brief Called when an instance of log source is disconnected.
* This call should as well automatically deactivate the log scopes.
* \param cookie The cookie ID of the instance to mark as disconnected.
* \param timestamp The deactivation timestamp to set.
* \return Returns true if operation succeeded.
**/
virtual bool logInstanceDisconnected(const ITEM_ID & cookie, const DateTime & timestamp) override;
/**
* \brief Called when need to log the information of the scope in the database.
* \param scope The log scope information to save in the database.
* \param cookie The cookie of the log scope owner instance.
* \param timestamp The timestamp to register when the scope is logged.
* \return Returns true if succeeded to save the log scope in the database.
**/
virtual bool logScopeActivate(const NELogging::sScopeInfo & scope, const ITEM_ID & cookie, const DateTime & timestamp) override;
/**
* \brief Called when need to log the information of the scope in the database.
* \param scopeName The name of the scope.
* \param scopeId The ID of the scope.
* \param scopePrio The log priority of the scope
* \param cookie The cookie of the log scope owner instance.
* \param timestamp The timestamp to register when the scope is logged.
* \return Returns true if succeeded to save the log scope in the database.
**/
virtual bool logScopeActivate(const String & scopeName, uint32_t scopeId, uint32_t scopePrio, const ITEM_ID & cookie, const DateTime & timestamp) override;
/**
* \brief Called when need to log the information of the list of scopes in the database.
* \param scopes The information of the list of the log scope to save in the database.
* \param cookie The cookie of the log scope owner instance.
* \param timestamp The timestamp to register when the scope is logged.
* \return Returns the number of scope entries saved in the database.
**/
virtual uint32_t logScopesActivate(const NELogging::ScopeNames& scopes, const ITEM_ID& cookie, const DateTime& timestamp) override;
/**
* \brief Call to deactivate all scopes related with the specified cookie ID.
* \param cookie The cookie ID, which scopes should be marked as deactivated.
* \param timestamp The deactivation timestamp to set.
* \return Returns true if operation succeeded.
**/
virtual bool logScopesDeactivate(const ITEM_ID & cookie, const DateTime & timestamp) override;
/**
* \brief Call to deactivate a single scope related with the specified cookie ID.
* \param cookie The cookie ID, which scope should be marked as deactivated.
* \param scopeId The scope ID to mark as deactivated.
* \param timestamp The deactivation timestamp to set.
* \return Returns true if operation succeeded.
**/
virtual bool logScopeDeactivate(const ITEM_ID & cookie, unsigned int scopeId, const DateTime & timestamp) override;
/**
* \brief Rolls back the database changes and returns true if succeeded.
**/
virtual bool rollback(void) override;
//////////////////////////////////////////////////////////////////////////
// Attributes and operations
//////////////////////////////////////////////////////////////////////////
#if 0
std::vector<String> getLogInstanceNames(void) const;
std::vector<ITEM_ID> getLogInstances(void) const;
std::vector<String> getLogThreadNames(void) const;
std::vector<ITEM_ID> getLogThreads(void) const;
std::vector<String> getLogScopeNames(void) const;
std::vector<ITEM_ID> getLogScopes(void) const;
std::vector<String> getPriorityNames(void) const;
std::vector<NELogging::sScopeInfo> getLogInstScopes(ITEM_ID instId) const;
std::vector<SharedBuffer> getLodMessages(void) const;
std::vector<SharedBuffer> getLodInstMessages(ITEM_ID instId) const;
std::vector<SharedBuffer> getLodScopeMessages(ITEM_ID instId, uint32_t scopeId) const;
#endif
//////////////////////////////////////////////////////////////////////////
// Hidden methods
//////////////////////////////////////////////////////////////////////////
private:
/**
* \brief Opens or creates the specified database file.
* The path can be relative or absolute, it may as contain the mask.
**/
inline bool _open(const String& dbPath);
/**
* \brief Closes previously opened database and releases resources.
**/
inline void _close(void);
/**
* \brief In the opened database file, creates the tables required to save logs.
**/
inline void _createTables(void);
/**
* \brief In the opened database file, creates the indexes required by optimize operations.
**/
inline void _createIndexes(void);
/**
* \brief Logs the initial information in the database like logging version and application name.
**/
inline void _initialize(void);
/**
* \brief Executes the SQL script. The database should be already opened and initialized.
**/
inline bool _execute(const char * sql);
//////////////////////////////////////////////////////////////////////////
// Member variables.
//////////////////////////////////////////////////////////////////////////
protected:
//!< The path to the SQLite database file.
String mDbPath;
//!< The initial path to the SQLIte database file. The path may contain mask like timestamp.
String mDbInitPath;
//!< The SQLite database object.
void * mDbObject;
//!< Flag, indicating whether the database and data tables are initialized or not.
bool mIsInitialized;
//!< Flag, indicating whether the database logging is enabled or not.
bool mDbLogEnabled;
//////////////////////////////////////////////////////////////////////////
// Forbidden calls.
//////////////////////////////////////////////////////////////////////////
private:
DECLARE_NOCOPY_NOMOVE(LogSqliteDatabase);
};
//////////////////////////////////////////////////////////////////////////
// LogSqliteDatabase class inline methods.
//////////////////////////////////////////////////////////////////////////
bool LogSqliteDatabase::isDabataseLoggingEnabled(void) const
{
return mDbLogEnabled;
}
inline void LogSqliteDatabase::setDatabaseLoggingEnabled(bool enable)
{
mDbLogEnabled = enable;
}
inline const String& LogSqliteDatabase::getDatabasePath(void) const
{
return mDbPath;
}
inline const String& LogSqliteDatabase::getInitialDatabasePath(void) const
{
return mDbInitPath;
}
#endif // AREG_AREGEXTEND_DB_LOGSQLITEDATABASE_HPP