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
|
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
|
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
+
-
+
-
+
+
-
+
+
-
+
|
<title>The Error And Warning Log</title>
<tcl>hd_keywords {errlog} {error log}</tcl>
<h1 align="center">The Error And Warning Log</h1>
<p>SQLite can be configured to invoke a callback function containing
an error code and a terse error message whenever anomalies occur.
The mechanism is very helpful in tracking obscure problems that
This mechanism is very helpful in tracking obscure problems that
occur rarely and in the field. Application developers are encouraged
to take advantage of the error logging facility of SQLite in their
products, as it presents hardly any overhead at all, but can be a
products, as it is very low CPU and memory cost but can be a
huge aid for debugging.</p>
<h2>Setting Up The Error Logging Callback</h2>
<p>There can only be a single error logging callback per process.
The error logging callback is registered at start-time using C-code
similar to the following:
<blockquote><pre>
[sqlite3_config]([SQLITE_CONFIG_LOG], errLogCallback, pData);
</pre></blockquote>
<p>The error logger callback function might look something like this:</p>
<blockquote><pre>
void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
}
</pre></blockquote>
<p>The example above illustrates the signature of the error logger callback,
at least. In an embedded application, one usually does not print
<p>The example above illustrates the signature of the error logger callback.
However, in an embedded application, one usually does not print
messages on stderr. Instead, one might store the messages in a
preallocated circular buffer where they can be accessed when diagnostic
information is needed during debugging. Or perhaps the messages can be
sent to [http://en.wikipedia.org/wiki/Syslog | Syslog]. Somehow, the
messages need to be stored where they are accessible to developers debugging
issues, not displayed to end users.</p>
messages need to be stored where they are accessible to developers,
not displayed to end users.</p>
<p>Do not misunderstand: There is nothing technically wrong with displaying
the error logger messages to end users. The messages do not contain
sensitive or private information that must be protected from unauthorized
viewing. Rather the messages are technical in nature and are not useful
or meaningful to the typical end user. These messages are intended for
database geeks. Display them accordingly.</p>
or meaningful to the typical end user. The messages coming from the
error logger are intended for database geeks. Display them accordingly.</p>
<h2>Interface Details</h2>
<p>The third argument to the [sqlite3_config]([SQLITE_CONFIG_LOG],...) call
(the "pData" argument in the example above) is a pointer to arbitrary
<p>The third argument to the [sqlite3_config]([SQLITE_CONFIG_LOG],...)
interface (the "pData" argument in the example above) is a pointer to arbitrary
data. SQLite passes this pointer through to the first argument of the
error logger callback. The pointer can be used to pass application-specific
setup or state information, if desired. Or it can simply be a NULL
pointer which is ignored by the callback.</p>
<p>The second argument to the error logger callback is an integer
[extended error code]. The third argument to the error logger is the
text of the error message. The error message text is stored in a fixed-length
stack buffer in the calling function and so will only be valid for the
duration of the error logger callback function. The error logger should
make a copy of this message into persistent storage if retention of the
message is needed.</p>
<p>The error logger callback should be treated like a signal handler.
The application should do something with the error, quickly, then return
The application should save off or otherwise process the error, then return
as soon as possible. No other SQLite APIs should be invoked, directly or
indirectly, from the error logger. SQLite is <u>not</u> reentrant through
indirectly, from the error logger. In particular, the error logger callback
the error logger callback. In particular, the error logger callback
is invoked when a memory allocation fails, so it is generally a bad idea
to try to allocate memory inside the error logger.</p>
to try to allocate memory inside the error logger. Do not even think
about trying to store the error message in another SQLite database.</p>
<p>Applications can use the [sqlite3_log(E,F,..)] API to send new messages
to the log, if desired.</p>
to the log, if desired, but this is discouraged. The [sqlite3_log()]
interface is intended for use by extensions only, not by applications.</p>
<h2>Variety of Error Messages</h2>
<p>The error messages that might be sent to the error logger and their
exact format is subject to changes from one release to the next. So
applications should not depend on any particular error message text or
applications should not depend on any particular error message text formats or
error codes. Things do not change capriciously, but they do sometimes
changes.</p>
<p>The following is a partial list of the kinds of messages that might
appear in the error logger callback.</p>
<ul>
|
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
|
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
|
-
+
-
+
+
+
+
-
+
-
+
-
-
+
|
SQLITE_WARNING messages are logged when database files are renamed or
aliased in ways that can lead to database corruption.
(See [unlink corruption | 1] and [database filename aliasing | 2] for
additional information.)
</p>
<li><p>
Out of memory (OOM) errors conditions generate error logging events
Out of memory (OOM) error conditions generate error logging events
with the SQLITE_NOMEM error code and a message that says how many bytes
of memory were requested by the failed allocation.
</p>
<li><p>I/O errors in the OS-interface generate error logging events.
The message to these events gives the line number in the source code where
the error originated and the filename associated with the event when
there is a corresponding file. </p>
<li><p>When database corruption is detected, an SQLITE_CORRUPT error
logger callback is invoked. As with I/O errors, the error message text
contains the line number in the original source code where the error
was first detected.</p>
<li><p>
An error logger callbacked is invoked on SQLITE_MISUSE errors.
An error logger callback is invoked on SQLITE_MISUSE errors.
This is useful in detecting application design issues when return codes
are not consistently checked in the application code.
</ul>
<p>SQLite strives to keep error logger traffic low and only send messages
to the error logger when there really is something wrong. Applications
might further cull the error message traffic
might take this further and deliberately ignore certain classes of error
by deliberately ignore certain classes of error
messages that they do not care about. For example, an application that
makes frequent database schema changes might want to ignore all
SQLITE_SCHEMA errors.</p>
<h2>Summary</h2>
<p>The use of the error logger callback is highly recommended.
The debugging information that the error logger provides has proven
very useful in tracking down obscure problems that occurs with applications
after they get into the field. The error logger callback has also
proven useful in catching errors occasional errors that the application
misses because of inconsistent checking of API return codes.
Developers are encouraged to implement an error logger callback early
in the development cycle in order to spot unexpected behavior quickly,
and to leave the error logger callback turned on through deployment.
If the error logger never finds a problem, then no harm is done.
But failure to set up an appropriate error logger might leave the
But failure to set up an appropriate error logger might compromise
developres wishing they had after mysterious and unreproducible
bug reports start coming back from the field.</p>
diagnostic capabilities later on.</p>
|