-
Notifications
You must be signed in to change notification settings - Fork 52
add set_log_hook/1 #266
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
add set_log_hook/1 #266
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ static ErlNifResourceType* connection_type = NULL; | |
static ErlNifResourceType* statement_type = NULL; | ||
static sqlite3_mem_methods default_alloc_methods = {0}; | ||
|
||
ErlNifPid* log_hook_pid = NULL; | ||
ErlNifMutex* log_hook_mutex = NULL; | ||
|
||
typedef struct connection | ||
{ | ||
sqlite3* db; | ||
|
@@ -956,6 +959,11 @@ on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info) | |
return -1; | ||
} | ||
|
||
log_hook_mutex = enif_mutex_create("exqlite:log_hook"); | ||
if (!log_hook_mutex) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
@@ -965,6 +973,7 @@ on_unload(ErlNifEnv* caller_env, void* priv_data) | |
assert(caller_env); | ||
|
||
sqlite3_config(SQLITE_CONFIG_MALLOC, &default_alloc_methods); | ||
enif_mutex_destroy(log_hook_mutex); | ||
} | ||
|
||
// | ||
|
@@ -1065,6 +1074,61 @@ exqlite_set_update_hook(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) | |
return make_atom(env, "ok"); | ||
} | ||
|
||
// | ||
// Log Notifications | ||
// | ||
|
||
void | ||
log_callback(void* arg, int iErrCode, const char* zMsg) | ||
{ | ||
if (log_hook_pid == NULL) { | ||
return; | ||
} | ||
|
||
ErlNifEnv* msg_env = enif_alloc_env(); | ||
ERL_NIF_TERM error = make_binary(msg_env, zMsg, strlen(zMsg)); | ||
ERL_NIF_TERM msg = enif_make_tuple3(msg_env, make_atom(msg_env, "log"), enif_make_int(msg_env, iErrCode), error); | ||
|
||
if (!enif_send(NULL, log_hook_pid, msg_env, msg)) { | ||
enif_mutex_lock(log_hook_mutex); | ||
sqlite3_config(SQLITE_CONFIG_LOG, NULL, NULL); | ||
enif_free(log_hook_pid); | ||
log_hook_pid = NULL; | ||
enif_mutex_unlock(log_hook_mutex); | ||
} | ||
|
||
enif_free_env(msg_env); | ||
} | ||
|
||
static ERL_NIF_TERM | ||
exqlite_set_log_hook(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) | ||
{ | ||
assert(env); | ||
|
||
if (argc != 1) { | ||
return enif_make_badarg(env); | ||
} | ||
|
||
ErlNifPid* pid = (ErlNifPid*)enif_alloc(sizeof(ErlNifPid)); | ||
if (!enif_get_local_pid(env, argv[0], pid)) { | ||
enif_free(pid); | ||
return make_error_tuple(env, "invalid_pid"); | ||
} | ||
|
||
enif_mutex_lock(log_hook_mutex); | ||
|
||
if (log_hook_pid) { | ||
enif_free(log_hook_pid); | ||
} | ||
|
||
log_hook_pid = pid; | ||
sqlite3_config(SQLITE_CONFIG_LOG, log_callback, NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a shame this log hook is global like this. I wish it were on a per sqlite database basis, but alas it is not. But I supposed you would only utilize this in a dev or test environment to track down a bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason it's not per database is -- I guess -- to also be able to cover errors coming from opening invalid databases and some other operations that happen prior to getting a reference to a database. The docs actually suggest using it in production: https://www.sqlite.org/errlog.html
|
||
|
||
enif_mutex_unlock(log_hook_mutex); | ||
|
||
return make_atom(env, "ok"); | ||
} | ||
|
||
// | ||
// Most of our nif functions are going to be IO bounded | ||
// | ||
|
@@ -1086,6 +1150,7 @@ static ErlNifFunc nif_funcs[] = { | |
{"release", 2, exqlite_release, ERL_NIF_DIRTY_JOB_IO_BOUND}, | ||
{"enable_load_extension", 2, exqlite_enable_load_extension, ERL_NIF_DIRTY_JOB_IO_BOUND}, | ||
{"set_update_hook", 2, exqlite_set_update_hook, ERL_NIF_DIRTY_JOB_IO_BOUND}, | ||
{"set_log_hook", 1, exqlite_set_log_hook, ERL_NIF_DIRTY_JOB_IO_BOUND}, | ||
}; | ||
|
||
ERL_NIF_INIT(Elixir.Exqlite.Sqlite3NIF, nif_funcs, on_load, NULL, NULL, on_unload) |
Uh oh!
There was an error while loading. Please reload this page.