-
Couldn't load subscription status.
- Fork 7
.,cgosqlite: add log handler callback #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+113
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package cgosqlite | ||
|
|
||
| // #include <sqlite3.h> | ||
| // | ||
| // void logCallbackGo(void* userData, int errCode, char* msgC); | ||
| // | ||
| // static void log_callback_into_go(void *userData, int errCode, const char *msg) { | ||
| // logCallbackGo(userData, errCode, (char*)msg); | ||
| // } | ||
| // | ||
| // static int ts_sqlite3_config_log(void) { | ||
| // // TODO(raggi): if the library gains new uses of sqlite3_config they need to | ||
| // // share a mutex. | ||
| // return sqlite3_config(SQLITE_CONFIG_LOG, log_callback_into_go, NULL); | ||
| // } | ||
| import "C" | ||
| import ( | ||
| "sync" | ||
| "unsafe" | ||
|
|
||
| "github.com/tailscale/sqlite/sqliteh" | ||
| ) | ||
|
|
||
| // LogCallback receives SQLite log messages. | ||
| type LogCallback func(code sqliteh.Code, msg string) | ||
|
|
||
| var ( | ||
| logCallbackMu sync.Mutex | ||
| logCallback LogCallback | ||
| ) | ||
|
|
||
| //export logCallbackGo | ||
| func logCallbackGo(userData unsafe.Pointer, errCode C.int, msgC *C.char) { | ||
| logCallbackMu.Lock() | ||
| cb := logCallback | ||
| logCallbackMu.Unlock() | ||
|
|
||
| if cb == nil { | ||
| return | ||
| } | ||
|
|
||
| msg := C.GoString(msgC) | ||
| cb(sqliteh.Code(errCode), msg) | ||
| } | ||
|
|
||
| // SetLogCallback sets the global SQLite log callback. | ||
| // If callback is nil, logs are discarded. | ||
| func SetLogCallback(callback LogCallback) error { | ||
| logCallbackMu.Lock() | ||
| logCallback = callback | ||
| logCallbackMu.Unlock() | ||
|
|
||
| res := C.ts_sqlite3_config_log() | ||
| return errCode(res) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //go:build cgo | ||
| // +build cgo | ||
|
|
||
| package sqlite | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/tailscale/sqlite/cgosqlite" | ||
| "github.com/tailscale/sqlite/sqliteh" | ||
| ) | ||
|
|
||
| // ensure LogCallback is convertible to cgosqlite.LogCallback | ||
| var _ cgosqlite.LogCallback = cgosqlite.LogCallback(LogCallback(func(code sqliteh.Code, msg string) {})) | ||
|
|
||
| func TestSetLogCallback(t *testing.T) { | ||
| var mu sync.Mutex | ||
| var logs []string | ||
|
|
||
| err := SetLogCallback(func(code sqliteh.Code, msg string) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| logs = append(logs, msg) | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer SetLogCallback(nil) | ||
|
|
||
| db := openTestDB(t) | ||
|
|
||
| _, err = db.Exec("SELECT * FROM nonexistent_table") | ||
| if err == nil { | ||
| t.Fatal("expected error from invalid SQL") | ||
| } | ||
|
|
||
| mu.Lock() | ||
| gotLogs := len(logs) > 0 | ||
| mu.Unlock() | ||
|
|
||
| if !gotLogs { | ||
| t.Fatal("expected to receive log messages") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're not using the userData? This is global and not per-DB?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured you'd do like 695e777 did for the WALHook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's global yeah, it's a sqlite3_config option, thankfully one of the few that can be set after sqlite3_initialize.