diff options
author | Vasu Nori <vnori@google.com> | 2010-03-01 14:22:08 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2010-03-01 14:22:08 -0800 |
commit | e0dab5bf6744812e7059ae63b31ac835479cb321 (patch) | |
tree | 07450227df5c2210575ae3cdfae6f4c3e6483da4 | |
parent | 37acc3122c87ab82fa6ea2f988681747c0fb826e (diff) | |
parent | 54dd0f25acad86f7c4901e6f7d8fdd5628e42970 (diff) | |
download | frameworks_base-e0dab5bf6744812e7059ae63b31ac835479cb321.zip frameworks_base-e0dab5bf6744812e7059ae63b31ac835479cb321.tar.gz frameworks_base-e0dab5bf6744812e7059ae63b31ac835479cb321.tar.bz2 |
Merge "register a logging func with sqlite, to get extra info on errors"
-rw-r--r-- | core/jni/android_database_SQLiteDatabase.cpp | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/core/jni/android_database_SQLiteDatabase.cpp b/core/jni/android_database_SQLiteDatabase.cpp index 26d0e86..b93311b 100644 --- a/core/jni/android_database_SQLiteDatabase.cpp +++ b/core/jni/android_database_SQLiteDatabase.cpp @@ -63,6 +63,26 @@ enum { static jfieldID offset_db_handle; +static void sqlLogger(void *databaseName, int iErrCode, const char *zMsg) { + LOGI("sqlite returned: error code = %d, msg = %s\n", iErrCode, zMsg); +} + +// register the logging func on sqlite. needs to be done BEFORE any sqlite3 func is called. +static void registerLoggingFunc() { + static bool loggingFuncSet = false; + if (loggingFuncSet) { + return; + } + + LOGV("Registering sqlite logging func \n"); + int err = sqlite3_config(SQLITE_CONFIG_LOG, &sqlLogger, 0); + if (err != SQLITE_OK) { + LOGE("sqlite_config failed error_code = %d. THIS SHOULD NEVER occur.\n", err); + return; + } + loggingFuncSet = true; +} + /* public native void dbopen(String path, int flags, String locale); */ static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags) { @@ -72,6 +92,9 @@ static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags) char const * path8 = env->GetStringUTFChars(pathString, NULL); int sqliteFlags; + // register the logging func on sqlite. needs to be done BEFORE any sqlite3 func is called. + registerLoggingFunc(); + // convert our flags into the sqlite flags if (flags & CREATE_IF_NECESSARY) { sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; @@ -143,7 +166,21 @@ done: if (handle != NULL) sqlite3_close(handle); } -void sqlTrace(void *databaseName, const char *sql) { +static char *getDatabaseName(JNIEnv* env, sqlite3 * handle, jstring databaseName) { + char const *path = env->GetStringUTFChars(databaseName, NULL); + if (path == NULL) { + LOGE("Failure in getDatabaseName(). VM ran out of memory?\n"); + return NULL; // VM would have thrown OutOfMemoryError + } + int len = strlen(path); + char *dbNameStr = (char *)malloc(len + 1); + strncpy(dbNameStr, path, len); + dbNameStr[len-1] = NULL; + env->ReleaseStringUTFChars(databaseName, path); + return dbNameStr; +} + +static void sqlTrace(void *databaseName, const char *sql) { LOGI("sql_statement|%s|%s\n", (char *)databaseName, sql); } @@ -151,21 +188,10 @@ void sqlTrace(void *databaseName, const char *sql) { static void enableSqlTracing(JNIEnv* env, jobject object, jstring databaseName) { sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle); - char const *path = env->GetStringUTFChars(databaseName, NULL); - if (path == NULL) { - LOGE("Failure in enableSqlTracing(). VM ran out of memory?\n"); - return; // VM would have thrown OutOfMemoryError - } - int len = strlen(path); - char *traceFuncArg = (char *)malloc(len + 1); - strncpy(traceFuncArg, path, len); - traceFuncArg[len-1] = NULL; - env->ReleaseStringUTFChars(databaseName, path); - sqlite3_trace(handle, &sqlTrace, (void *)traceFuncArg); - LOGI("will be printing all sql statements executed on database = %s\n", traceFuncArg); + sqlite3_trace(handle, &sqlTrace, (void *)getDatabaseName(env, handle, databaseName)); } -void sqlProfile(void *databaseName, const char *sql, sqlite3_uint64 tm) { +static void sqlProfile(void *databaseName, const char *sql, sqlite3_uint64 tm) { double d = tm/1000000.0; LOGI("elapsedTime4Sql|%s|%.3f ms|%s\n", (char *)databaseName, d, sql); } @@ -174,21 +200,10 @@ void sqlProfile(void *databaseName, const char *sql, sqlite3_uint64 tm) { static void enableSqlProfiling(JNIEnv* env, jobject object, jstring databaseName) { sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle); - char const *path = env->GetStringUTFChars(databaseName, NULL); - if (path == NULL) { - LOGE("Failure in enableSqlProfiling(). VM ran out of memory?\n"); - return; // VM would have thrown OutOfMemoryError - } - int len = strlen(path); - char *traceFuncArg = (char *)malloc(len + 1); - strncpy(traceFuncArg, path, len); - traceFuncArg[len-1] = NULL; - env->ReleaseStringUTFChars(databaseName, path); - sqlite3_profile(handle, &sqlProfile, (void *)traceFuncArg); - LOGI("will be printing execution time of all sql statements executed on database = %s\n", - traceFuncArg); + sqlite3_profile(handle, &sqlProfile, (void *)getDatabaseName(env, handle, databaseName)); } + /* public native void close(); */ static void dbclose(JNIEnv* env, jobject object) { |