Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Implement SQLite error logging (#6291)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedbunny authored Sep 8, 2016
1 parent 4c8d61a commit 4ef8f81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions platform/default/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <chrono>
#include <experimental/optional>

#include <mbgl/platform/log.hpp>

const static bool sqliteVersionCheck __attribute__((unused)) = []() {
if (sqlite3_libversion_number() / 1000000 != SQLITE_VERSION_NUMBER / 1000000) {
char message[96];
Expand All @@ -26,6 +28,7 @@ template <typename T>
using optional = std::experimental::optional<T>;

Database::Database(const std::string &filename, int flags) {
sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, nullptr);
const int err = sqlite3_open_v2(filename.c_str(), &db, flags, nullptr);
if (err != SQLITE_OK) {
const auto message = sqlite3_errmsg(db);
Expand Down Expand Up @@ -55,6 +58,16 @@ Database::operator bool() const {
return db != nullptr;
}

void Database::errorLogCallback(void *, const int err, const char *msg) {
if (err == SQLITE_ERROR) {
mbgl::Log::Error(mbgl::Event::Database, "%s (Code %i)", msg, err);
} else if (err == SQLITE_WARNING) {
mbgl::Log::Warning(mbgl::Event::Database, "%s (Code %i)", msg, err);
} else {
mbgl::Log::Info(mbgl::Event::Database, "%s (Code %i)", msg, err);
}
}

void Database::setBusyTimeout(std::chrono::milliseconds timeout) {
assert(db);
const int err = sqlite3_busy_timeout(db,
Expand Down
1 change: 1 addition & 0 deletions platform/default/sqlite3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Database {

private:
sqlite3 *db = nullptr;
static void errorLogCallback(void *arg, const int err, const char *msg);
};

class Statement {
Expand Down

0 comments on commit 4ef8f81

Please sign in to comment.