Improve SQLite logging.

GitOrigin-RevId: 5a779f25594f518508a0a9df7f6c64cfb9cbc9e4
This commit is contained in:
levlam 2020-03-29 00:12:51 +03:00
parent 9679cef0bb
commit d5ebf00b72
2 changed files with 12 additions and 3 deletions

View File

@ -93,9 +93,13 @@ void SqliteDb::trace(bool flag) {
Status SqliteDb::exec(CSlice cmd) {
CHECK(!empty());
char *msg;
VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
if (enable_logging_) {
VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
}
auto rc = sqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db());
if (enable_logging_) {
VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db());
}
if (rc != SQLITE_OK) {
CHECK(msg != nullptr);
return Status::Error(PSLICE() << tag("query", cmd) << " to database \"" << raw_->path() << "\" failed: " << msg);
@ -151,7 +155,11 @@ Status SqliteDb::commit_transaction() {
}
Status SqliteDb::check_encryption() {
return exec("SELECT count(*) FROM sqlite_master");
auto status = exec("SELECT count(*) FROM sqlite_master");
if (status.is_ok()) {
enable_logging_ = true;
}
return status;
}
Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key) {

View File

@ -80,6 +80,7 @@ class SqliteDb {
explicit SqliteDb(std::shared_ptr<detail::RawSqliteDb> raw) : raw_(std::move(raw)) {
}
std::shared_ptr<detail::RawSqliteDb> raw_;
bool enable_logging_ = false;
Status check_encryption();
};