Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 5abf59a8bf180e50f92069a8bcfaf36c2a82f76a [file] [log] [blame]
danac455932012-11-26 19:50:411/*
2** 2012 November 26
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** OVERVIEW
14**
15** This file contains experimental code used to record data from live
drhb9830a12013-04-22 13:51:0916** SQLite applications that may be useful for offline analysis.
17** Specifically, this module can be used to capture the following
18** information:
danac455932012-11-26 19:50:4119**
20** 1) The initial contents of all database files opened by the
21** application, and
22**
23** 2) All SQL statements executed by the application.
24**
drhb9830a12013-04-22 13:51:0925** The captured information can then be used to run (for example)
26** performance analysis looking for slow queries or to look for
27** optimization opportunities in either the application or in SQLite
28** itself.
29**
danac455932012-11-26 19:50:4130** USAGE
31**
32** To use this module, SQLite must be compiled with the SQLITE_ENABLE_SQLLOG
drhb9830a12013-04-22 13:51:0933** pre-processor symbol defined and this file linked into the application.
34** One way to link this file into the application is to append the content
35** of this file onto the end of the "sqlite3.c" amalgamation and then
36** recompile the application as normal except with the addition of the
37** -DSQLITE_ENABLE_SQLLOG option.
danac455932012-11-26 19:50:4138**
39** At runtime, logging is enabled by setting environment variable
40** SQLITE_SQLLOG_DIR to the name of a directory in which to store logged
drhb9830a12013-04-22 13:51:0941** data. The logging directory must already exist.
danac455932012-11-26 19:50:4142**
43** Usually, if the application opens the same database file more than once
44** (either by attaching it or by using more than one database handle), only
mistachkin48864df2013-03-21 21:20:3245** a single copy is made. This behavior may be overridden (so that a
danac455932012-11-26 19:50:4146** separate copy is taken each time the database file is opened or attached)
47** by setting the environment variable SQLITE_SQLLOG_REUSE_FILES to 0.
48**
dand83f7ca2015-11-12 20:12:5149** If the environment variable SQLITE_SQLLOG_CONDITIONAL is defined, then
50** logging is only done for database connections if a file named
51** "<database>-sqllog" exists in the same directly as the main database
52** file when it is first opened ("<database>" is replaced by the actual
53** name of the main database file).
54**
danac455932012-11-26 19:50:4155** OUTPUT:
56**
57** The SQLITE_SQLLOG_DIR is populated with three types of files:
58**
59** sqllog_N.db - Copies of database files. N may be any integer.
60**
61** sqllog_N.sql - A list of SQL statements executed by a single
62** connection. N may be any integer.
63**
64** sqllog.idx - An index mapping from integer N to a database
65** file name - indicating the full path of the
66** database from which sqllog_N.db was copied.
dan71ba10d2012-11-27 10:56:3967**
68** ERROR HANDLING:
69**
70** This module attempts to make a best effort to continue logging if an
71** IO or other error is encountered. For example, if a log file cannot
72** be opened logs are not collected for that connection, but other
73** logging proceeds as expected. Errors are logged by calling sqlite3_log().
danac455932012-11-26 19:50:4174*/
75
drhb9830a12013-04-22 13:51:0976#ifndef _SQLITE3_H_
danac455932012-11-26 19:50:4177#include "sqlite3.h"
drhb9830a12013-04-22 13:51:0978#endif
79#include <stdio.h>
80#include <stdlib.h>
81#include <string.h>
82#include <assert.h>
danac455932012-11-26 19:50:4183
drhb9830a12013-04-22 13:51:0984#include <sys/types.h>
85#include <unistd.h>
dan71ba10d2012-11-27 10:56:3986static int getProcessId(void){
stephanb6503f12025-03-06 13:38:0787#ifdef _WIN32
drh7d1f0c72012-11-27 16:39:3188 return (int)_getpid();
89#else
dan71ba10d2012-11-27 10:56:3990 return (int)getpid();
drh7d1f0c72012-11-27 16:39:3191#endif
dan71ba10d2012-11-27 10:56:3992}
93
drhb9830a12013-04-22 13:51:0994/* Names of environment variables to be used */
danac455932012-11-26 19:50:4195#define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
96#define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
dand83f7ca2015-11-12 20:12:5197#define ENVIRONMENT_VARIABLE3_NAME "SQLITE_SQLLOG_CONDITIONAL"
danac455932012-11-26 19:50:4198
99/* Assume that all database and database file names are shorted than this. */
100#define SQLLOG_NAMESZ 512
101
102/* Maximum number of simultaneous database connections the process may
103** open (if any more are opened an error is logged using sqlite3_log()
104** and processing is halted).
105*/
106#define MAX_CONNECTIONS 256
107
drhb9830a12013-04-22 13:51:09108/* There is one instance of this object for each SQLite database connection
109** that is being logged.
110*/
danac455932012-11-26 19:50:41111struct SLConn {
112 int isErr; /* True if an error has occurred */
113 sqlite3 *db; /* Connection handle */
114 int iLog; /* First integer value used in file names */
115 FILE *fd; /* File descriptor for log file */
116};
117
drhb9830a12013-04-22 13:51:09118/* This object is a singleton that keeps track of all data loggers.
119*/
120static struct SLGlobal {
drh067b92b2020-06-19 15:24:12121 /* Protected by MUTEX_STATIC_MAIN */
danac455932012-11-26 19:50:41122 sqlite3_mutex *mutex; /* Recursive mutex */
123 int nConn; /* Size of aConn[] array */
124
125 /* Protected by SLGlobal.mutex */
dand83f7ca2015-11-12 20:12:51126 int bConditional; /* Only trace if *-sqllog file is present */
danac455932012-11-26 19:50:41127 int bReuse; /* True to avoid extra copies of db files */
dan71ba10d2012-11-27 10:56:39128 char zPrefix[SQLLOG_NAMESZ]; /* Prefix for all created files */
danac455932012-11-26 19:50:41129 char zIdx[SQLLOG_NAMESZ]; /* Full path to *.idx file */
130 int iNextLog; /* Used to allocate file names */
131 int iNextDb; /* Used to allocate database file names */
132 int bRec; /* True if testSqllog() is called rec. */
133 int iClock; /* Clock value */
134 struct SLConn aConn[MAX_CONNECTIONS];
135} sqllogglobal;
136
137/*
138** Return true if c is an ASCII whitespace character.
139*/
140static int sqllog_isspace(char c){
141 return (c==' ' || c=='\t' || c=='\n' || c=='\v' || c=='\f' || c=='\r');
142}
143
144/*
145** The first argument points to a nul-terminated string containing an SQL
146** command. Before returning, this function sets *pz to point to the start
147** of the first token in this command, and *pn to the number of bytes in
148** the token. This is used to check if the SQL command is an "ATTACH" or
149** not.
150*/
151static void sqllogTokenize(const char *z, const char **pz, int *pn){
152 const char *p = z;
153 int n;
154
155 /* Skip past any whitespace */
156 while( sqllog_isspace(*p) ){
157 p++;
158 }
159
160 /* Figure out how long the first token is */
161 *pz = p;
162 n = 0;
163 while( (p[n]>='a' && p[n]<='z') || (p[n]>='A' && p[n]<='Z') ) n++;
164 *pn = n;
165}
166
167/*
168** Check if the logs directory already contains a copy of database file
169** zFile. If so, return a pointer to the full path of the copy. Otherwise,
170** return NULL.
171**
172** If a non-NULL value is returned, then the caller must arrange to
173** eventually free it using sqlite3_free().
174*/
175static char *sqllogFindFile(const char *zFile){
176 char *zRet = 0;
177 FILE *fd = 0;
178
179 /* Open the index file for reading */
180 fd = fopen(sqllogglobal.zIdx, "r");
dan71ba10d2012-11-27 10:56:39181 if( fd==0 ){
182 sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error in fopen()");
183 return 0;
184 }
danac455932012-11-26 19:50:41185
dan71ba10d2012-11-27 10:56:39186 /* Loop through each entry in the index file. If zFile is not NULL and the
187 ** entry is a match, then set zRet to point to the filename of the existing
188 ** copy and break out of the loop. */
danac455932012-11-26 19:50:41189 while( feof(fd)==0 ){
danac455932012-11-26 19:50:41190 char zLine[SQLLOG_NAMESZ*2+5];
dan71ba10d2012-11-27 10:56:39191 if( fgets(zLine, sizeof(zLine), fd) ){
192 int n;
193 char *z;
danac455932012-11-26 19:50:41194
dan71ba10d2012-11-27 10:56:39195 zLine[sizeof(zLine)-1] = '\0';
danac455932012-11-26 19:50:41196 z = zLine;
dan71ba10d2012-11-27 10:56:39197 while( *z>='0' && *z<='9' ) z++;
198 while( *z==' ' ) z++;
199
200 n = strlen(z);
201 while( n>0 && sqllog_isspace(z[n-1]) ) n--;
202
203 if( n==strlen(zFile) && 0==memcmp(zFile, z, n) ){
204 char zBuf[16];
205 memset(zBuf, 0, sizeof(zBuf));
206 z = zLine;
207 while( *z>='0' && *z<='9' ){
208 zBuf[z-zLine] = *z;
209 z++;
210 }
211 zRet = sqlite3_mprintf("%s_%s.db", sqllogglobal.zPrefix, zBuf);
212 break;
danac455932012-11-26 19:50:41213 }
danac455932012-11-26 19:50:41214 }
215 }
216
dan71ba10d2012-11-27 10:56:39217 if( ferror(fd) ){
218 sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error reading index file");
219 }
220
danac455932012-11-26 19:50:41221 fclose(fd);
222 return zRet;
223}
224
dan71ba10d2012-11-27 10:56:39225static int sqllogFindAttached(
dand83f7ca2015-11-12 20:12:51226 sqlite3 *db, /* Database connection */
danac455932012-11-26 19:50:41227 const char *zSearch, /* Name to search for (or NULL) */
228 char *zName, /* OUT: Name of attached database */
229 char *zFile /* OUT: Name of attached file */
230){
231 sqlite3_stmt *pStmt;
232 int rc;
233
234 /* The "PRAGMA database_list" command returns a list of databases in the
235 ** order that they were attached. So a newly attached database is
236 ** described by the last row returned. */
237 assert( sqllogglobal.bRec==0 );
238 sqllogglobal.bRec = 1;
dand83f7ca2015-11-12 20:12:51239 rc = sqlite3_prepare_v2(db, "PRAGMA database_list", -1, &pStmt, 0);
danac455932012-11-26 19:50:41240 if( rc==SQLITE_OK ){
241 while( SQLITE_ROW==sqlite3_step(pStmt) ){
drh7d1f0c72012-11-27 16:39:31242 const char *zVal1; int nVal1;
243 const char *zVal2; int nVal2;
danac455932012-11-26 19:50:41244
drh7d1f0c72012-11-27 16:39:31245 zVal1 = (const char*)sqlite3_column_text(pStmt, 1);
danac455932012-11-26 19:50:41246 nVal1 = sqlite3_column_bytes(pStmt, 1);
dand83f7ca2015-11-12 20:12:51247 if( zName ){
248 memcpy(zName, zVal1, nVal1+1);
249 }
danac455932012-11-26 19:50:41250
drh7d1f0c72012-11-27 16:39:31251 zVal2 = (const char*)sqlite3_column_text(pStmt, 2);
danac455932012-11-26 19:50:41252 nVal2 = sqlite3_column_bytes(pStmt, 2);
253 memcpy(zFile, zVal2, nVal2+1);
254
255 if( zSearch && strlen(zSearch)==nVal1
256 && 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
257 ){
258 break;
259 }
260 }
261 rc = sqlite3_finalize(pStmt);
262 }
263 sqllogglobal.bRec = 0;
dan71ba10d2012-11-27 10:56:39264
265 if( rc!=SQLITE_OK ){
266 sqlite3_log(rc, "sqllogFindAttached(): error in \"PRAGMA database_list\"");
267 }
268 return rc;
danac455932012-11-26 19:50:41269}
270
271
272/*
273** Parameter zSearch is the name of a database attached to the database
274** connection associated with the first argument. This function creates
275** a backup of this database in the logs directory.
276**
277** The name used for the backup file is automatically generated. Call
278** it zFile.
279**
280** If the bLog parameter is true, then a statement of the following form
281** is written to the log file associated with *p:
282**
283** ATTACH 'zFile' AS 'zName';
284**
285** Otherwise, if bLog is false, a comment is added to the log file:
286**
287** -- Main database file is 'zFile'
288**
289** The SLGlobal.mutex mutex is always held when this function is called.
290*/
291static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
292 char zName[SQLLOG_NAMESZ]; /* Attached database name */
293 char zFile[SQLLOG_NAMESZ]; /* Database file name */
294 char *zFree;
295 char *zInit = 0;
dan71ba10d2012-11-27 10:56:39296 int rc;
danac455932012-11-26 19:50:41297
dand83f7ca2015-11-12 20:12:51298 rc = sqllogFindAttached(p->db, zSearch, zName, zFile);
dan71ba10d2012-11-27 10:56:39299 if( rc!=SQLITE_OK ) return;
300
danac455932012-11-26 19:50:41301 if( zFile[0]=='\0' ){
302 zInit = sqlite3_mprintf("");
303 }else{
304 if( sqllogglobal.bReuse ){
305 zInit = sqllogFindFile(zFile);
306 }else{
307 zInit = 0;
308 }
309 if( zInit==0 ){
310 int rc;
311 sqlite3 *copy = 0;
danac455932012-11-26 19:50:41312 int iDb;
313
314 /* Generate a file-name to use for the copy of this database */
315 iDb = sqllogglobal.iNextDb++;
drh87e1e562017-01-04 14:53:53316 zInit = sqlite3_mprintf("%s_%02d.db", sqllogglobal.zPrefix, iDb);
danac455932012-11-26 19:50:41317
318 /* Create the backup */
319 assert( sqllogglobal.bRec==0 );
320 sqllogglobal.bRec = 1;
321 rc = sqlite3_open(zInit, &copy);
322 if( rc==SQLITE_OK ){
323 sqlite3_backup *pBak;
324 sqlite3_exec(copy, "PRAGMA synchronous = 0", 0, 0, 0);
325 pBak = sqlite3_backup_init(copy, "main", p->db, zName);
326 if( pBak ){
327 sqlite3_backup_step(pBak, -1);
dan71ba10d2012-11-27 10:56:39328 rc = sqlite3_backup_finish(pBak);
329 }else{
330 rc = sqlite3_errcode(copy);
danac455932012-11-26 19:50:41331 }
332 sqlite3_close(copy);
333 }
334 sqllogglobal.bRec = 0;
335
dan71ba10d2012-11-27 10:56:39336 if( rc==SQLITE_OK ){
337 /* Write an entry into the database index file */
338 FILE *fd = fopen(sqllogglobal.zIdx, "a");
339 if( fd ){
340 fprintf(fd, "%d %s\n", iDb, zFile);
341 fclose(fd);
342 }
343 }else{
344 sqlite3_log(rc, "sqllogCopydb(): error backing up database");
345 }
danac455932012-11-26 19:50:41346 }
347 }
348
349 if( bLog ){
dan71ba10d2012-11-27 10:56:39350 zFree = sqlite3_mprintf("ATTACH '%q' AS '%q'; -- clock=%d\n",
danac455932012-11-26 19:50:41351 zInit, zName, sqllogglobal.iClock++
352 );
353 }else{
354 zFree = sqlite3_mprintf("-- Main database is '%q'\n", zInit);
355 }
356 fprintf(p->fd, "%s", zFree);
357 sqlite3_free(zFree);
358
359 sqlite3_free(zInit);
360}
361
362/*
363** If it is not already open, open the log file for connection *p.
364**
365** The SLGlobal.mutex mutex is always held when this function is called.
366*/
367static void sqllogOpenlog(struct SLConn *p){
368 /* If the log file has not yet been opened, open it now. */
369 if( p->fd==0 ){
370 char *zLog;
371
dan71ba10d2012-11-27 10:56:39372 /* If it is still NULL, have global.zPrefix point to a copy of
373 ** environment variable $ENVIRONMENT_VARIABLE1_NAME. */
374 if( sqllogglobal.zPrefix[0]==0 ){
danac455932012-11-26 19:50:41375 FILE *fd;
376 char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME);
dan71ba10d2012-11-27 10:56:39377 if( zVar==0 || strlen(zVar)+10>=(sizeof(sqllogglobal.zPrefix)) ) return;
drh65545b52015-01-19 00:35:53378 sqlite3_snprintf(sizeof(sqllogglobal.zPrefix), sqllogglobal.zPrefix,
drh87e1e562017-01-04 14:53:53379 "%s/sqllog_%05d", zVar, getProcessId());
drh65545b52015-01-19 00:35:53380 sqlite3_snprintf(sizeof(sqllogglobal.zIdx), sqllogglobal.zIdx,
381 "%s.idx", sqllogglobal.zPrefix);
danac455932012-11-26 19:50:41382 if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){
383 sqllogglobal.bReuse = atoi(getenv(ENVIRONMENT_VARIABLE2_NAME));
384 }
385 fd = fopen(sqllogglobal.zIdx, "w");
dan71ba10d2012-11-27 10:56:39386 if( fd ) fclose(fd);
danac455932012-11-26 19:50:41387 }
388
389 /* Open the log file */
drh87e1e562017-01-04 14:53:53390 zLog = sqlite3_mprintf("%s_%05d.sql", sqllogglobal.zPrefix, p->iLog);
danac455932012-11-26 19:50:41391 p->fd = fopen(zLog, "w");
danac455932012-11-26 19:50:41392 sqlite3_free(zLog);
dan71ba10d2012-11-27 10:56:39393 if( p->fd==0 ){
394 sqlite3_log(SQLITE_IOERR, "sqllogOpenlog(): Failed to open log file");
395 }
danac455932012-11-26 19:50:41396 }
397}
398
399/*
400** This function is called if the SQLLOG callback is invoked to report
401** execution of an SQL statement. Parameter p is the connection the statement
dan71ba10d2012-11-27 10:56:39402** was executed by and parameter zSql is the text of the statement itself.
danac455932012-11-26 19:50:41403*/
dan71ba10d2012-11-27 10:56:39404static void testSqllogStmt(struct SLConn *p, const char *zSql){
danac455932012-11-26 19:50:41405 const char *zFirst; /* Pointer to first token in zSql */
406 int nFirst; /* Size of token zFirst in bytes */
407
danac455932012-11-26 19:50:41408 sqllogTokenize(zSql, &zFirst, &nFirst);
409 if( nFirst!=6 || 0!=sqlite3_strnicmp("ATTACH", zFirst, 6) ){
410 /* Not an ATTACH statement. Write this directly to the log. */
dan71ba10d2012-11-27 10:56:39411 fprintf(p->fd, "%s; -- clock=%d\n", zSql, sqllogglobal.iClock++);
danac455932012-11-26 19:50:41412 }else{
413 /* This is an ATTACH statement. Copy the database. */
414 sqllogCopydb(p, 0, 1);
415 }
416}
417
418/*
dand83f7ca2015-11-12 20:12:51419** The database handle passed as the only argument has just been opened.
420** Return true if this module should log initial databases and SQL
421** statements for this connection, or false otherwise.
422**
423** If an error occurs, sqlite3_log() is invoked to report it to the user
424** and zero returned.
425*/
426static int sqllogTraceDb(sqlite3 *db){
427 int bRet = 1;
428 if( sqllogglobal.bConditional ){
429 char zFile[SQLLOG_NAMESZ]; /* Attached database name */
430 int rc = sqllogFindAttached(db, "main", 0, zFile);
431 if( rc==SQLITE_OK ){
432 int nFile = strlen(zFile);
433 if( (SQLLOG_NAMESZ-nFile)<8 ){
434 sqlite3_log(SQLITE_IOERR,
435 "sqllogTraceDb(): database name too long (%d bytes)", nFile
436 );
437 bRet = 0;
438 }else{
439 memcpy(&zFile[nFile], "-sqllog", 8);
440 bRet = !access(zFile, F_OK);
441 }
442 }
443 }
444 return bRet;
445}
446
447/*
danac455932012-11-26 19:50:41448** The SQLITE_CONFIG_SQLLOG callback registered by sqlite3_init_sqllog().
drhb9830a12013-04-22 13:51:09449**
450** The eType parameter has the following values:
451**
452** 0: Opening a new database connection. zSql is the name of the
453** file being opened. db is a pointer to the newly created database
454** connection.
455**
456** 1: An SQL statement has run to completion. zSql is the text of the
457** SQL statement with all parameters expanded to their actual values.
458**
459** 2: Closing a database connection. zSql is NULL. The db pointer to
460** the database connection being closed has already been shut down
461** and cannot be used for any further SQL.
462**
463** The pCtx parameter is a copy of the pointer that was originally passed
464** into the sqlite3_config(SQLITE_CONFIG_SQLLOG) statement. In this
465** particular implementation, pCtx is always a pointer to the
466** sqllogglobal global variable define above.
danac455932012-11-26 19:50:41467*/
dan71ba10d2012-11-27 10:56:39468static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){
drh696b33e2012-12-06 19:01:42469 struct SLConn *p = 0;
drh067b92b2020-06-19 15:24:12470 sqlite3_mutex *mainmtx = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN);
danac455932012-11-26 19:50:41471
dan71ba10d2012-11-27 10:56:39472 assert( eType==0 || eType==1 || eType==2 );
473 assert( (eType==2)==(zSql==0) );
danac455932012-11-26 19:50:41474
475 /* This is a database open command. */
dan71ba10d2012-11-27 10:56:39476 if( eType==0 ){
drh067b92b2020-06-19 15:24:12477 sqlite3_mutex_enter(mainmtx);
danac455932012-11-26 19:50:41478 if( sqllogglobal.mutex==0 ){
479 sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
480 }
drh067b92b2020-06-19 15:24:12481 sqlite3_mutex_leave(mainmtx);
danac455932012-11-26 19:50:41482
danac455932012-11-26 19:50:41483 sqlite3_mutex_enter(sqllogglobal.mutex);
dand83f7ca2015-11-12 20:12:51484 if( sqllogglobal.bRec==0 && sqllogTraceDb(db) ){
485
drh067b92b2020-06-19 15:24:12486 sqlite3_mutex_enter(mainmtx);
dand83f7ca2015-11-12 20:12:51487 p = &sqllogglobal.aConn[sqllogglobal.nConn++];
488 p->fd = 0;
489 p->db = db;
490 p->iLog = sqllogglobal.iNextLog++;
drh067b92b2020-06-19 15:24:12491 sqlite3_mutex_leave(mainmtx);
dand83f7ca2015-11-12 20:12:51492
493 /* Open the log and take a copy of the main database file */
danac455932012-11-26 19:50:41494 sqllogOpenlog(p);
dan71ba10d2012-11-27 10:56:39495 if( p->fd ) sqllogCopydb(p, "main", 0);
danac455932012-11-26 19:50:41496 }
497 sqlite3_mutex_leave(sqllogglobal.mutex);
498 }
499
500 else{
501
502 int i;
503 for(i=0; i<sqllogglobal.nConn; i++){
504 p = &sqllogglobal.aConn[i];
505 if( p->db==db ) break;
506 }
danac455932012-11-26 19:50:41507
508 /* A database handle close command */
dan71ba10d2012-11-27 10:56:39509 if( eType==2 ){
drh067b92b2020-06-19 15:24:12510 sqlite3_mutex_enter(mainmtx);
dand83f7ca2015-11-12 20:12:51511 if( i<sqllogglobal.nConn ){
512 if( p->fd ) fclose(p->fd);
513 p->db = 0;
514 p->fd = 0;
515 sqllogglobal.nConn--;
516 }
danac455932012-11-26 19:50:41517
danac455932012-11-26 19:50:41518 if( sqllogglobal.nConn==0 ){
519 sqlite3_mutex_free(sqllogglobal.mutex);
520 sqllogglobal.mutex = 0;
dand83f7ca2015-11-12 20:12:51521 }else if( i<sqllogglobal.nConn ){
danac455932012-11-26 19:50:41522 int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
523 if( nShift>0 ){
524 memmove(p, &p[1], nShift*sizeof(struct SLConn));
525 }
526 }
drh067b92b2020-06-19 15:24:12527 sqlite3_mutex_leave(mainmtx);
danac455932012-11-26 19:50:41528
529 /* An ordinary SQL command. */
dand83f7ca2015-11-12 20:12:51530 }else if( i<sqllogglobal.nConn && p->fd ){
danac455932012-11-26 19:50:41531 sqlite3_mutex_enter(sqllogglobal.mutex);
532 if( sqllogglobal.bRec==0 ){
dan71ba10d2012-11-27 10:56:39533 testSqllogStmt(p, zSql);
danac455932012-11-26 19:50:41534 }
535 sqlite3_mutex_leave(sqllogglobal.mutex);
536 }
537 }
538}
539
540/*
541** This function is called either before sqlite3_initialized() or by it.
542** It checks if the SQLITE_SQLLOG_DIR variable is defined, and if so
543** registers an SQLITE_CONFIG_SQLLOG callback to record the applications
544** database activity.
545*/
546void sqlite3_init_sqllog(void){
547 if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
548 if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
549 memset(&sqllogglobal, 0, sizeof(sqllogglobal));
550 sqllogglobal.bReuse = 1;
dand83f7ca2015-11-12 20:12:51551 if( getenv(ENVIRONMENT_VARIABLE3_NAME) ){
552 sqllogglobal.bConditional = 1;
553 }
danac455932012-11-26 19:50:41554 }
555 }
556}