Skip to content

Commit 2555724

Browse files
committed
Global ConfigLog.
1 parent c2d3bf0 commit 2555724

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"sync/atomic"
78

89
"github.com/tetratelabs/wazero/api"
910

@@ -48,6 +49,15 @@ func (c *Conn) Config(op DBConfig, arg ...bool) (bool, error) {
4849
return util.ReadBool(c.mod, argsPtr), c.error(rc)
4950
}
5051

52+
var defaultLogger atomic.Pointer[func(code ExtendedErrorCode, msg string)]
53+
54+
// ConfigLog sets up the default error logging callback for new connections.
55+
//
56+
// https://sqlite.org/errlog.html
57+
func ConfigLog(cb func(code ExtendedErrorCode, msg string)) {
58+
defaultLogger.Store(&cb)
59+
}
60+
5161
// ConfigLog sets up the error logging callback for the connection.
5262
//
5363
// https://sqlite.org/errlog.html

conn.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func newConn(ctx context.Context, filename string, flags OpenFlag) (ret *Conn, _
9292
}()
9393

9494
c.ctx = context.WithValue(c.ctx, connKey{}, c)
95+
if logger := defaultLogger.Load(); logger != nil {
96+
c.ConfigLog(*logger)
97+
}
9598
c.arena = c.newArena()
9699
c.handle, err = c.openDB(filename, flags)
97100
if err == nil {

tests/parallel/parallel_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tests
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"log"
@@ -24,15 +23,17 @@ import (
2423

2524
func TestMain(m *testing.M) {
2625
sqlite3.Initialize()
27-
sqlite3.AutoExtension(func(c *sqlite3.Conn) error {
28-
return c.ConfigLog(func(code sqlite3.ExtendedErrorCode, msg string) {
29-
// Having to do journal recovery is unexpected.
30-
if errors.Is(code, sqlite3.NOTICE) {
31-
log.Panicf("%v (%d): %s", code, code, msg)
32-
} else {
33-
log.Printf("%v (%d): %s", code, code, msg)
34-
}
35-
})
26+
sqlite3.ConfigLog(func(code sqlite3.ExtendedErrorCode, msg string) {
27+
switch code {
28+
case sqlite3.NOTICE_RECOVER_WAL:
29+
// Wal "recovery" is expected.
30+
break
31+
case sqlite3.NOTICE_RECOVER_ROLLBACK:
32+
// Rollback journal recovery is an error.
33+
log.Panicf("%v (%d): %s", code, code, msg)
34+
default:
35+
log.Printf("%v (%d): %s", code, code, msg)
36+
}
3637
})
3738
m.Run()
3839
}
@@ -68,7 +69,7 @@ func Test_wal(t *testing.T) {
6869
if testing.Short() {
6970
iter = 1000
7071
} else {
71-
iter = 2500
72+
iter = 5000
7273
}
7374

7475
name := "file:" +

0 commit comments

Comments
 (0)