ld_logging.c

Go to the documentation of this file.
00001 #include "ld_logging.h"
00002 
00003 #include "application_config.h"
00004 #include "er_errors.h"
00005 #include "ld_create.h"
00006 #include "ld_private.h"
00007 
00008 #include "common/platform_config.h"
00009 #include "common/threading.h"
00010 
00011 #include <glib/gprintf.h>
00012 
00013 #include <time.h>
00014 #include <stdarg.h> // va_list
00015 #include <errno.h>
00016 
00017 #if defined(__SYMBIAN32__)
00018 
00019 #if __BTPROX_ENABLED__
00020 
00021 #define SET_BTPROX_SQL_ERROR { rval = FALSE; if (error) *error = gx_error_new(domain_cl2app, code_database_command, "failed to log btprox event: %s (%d)", sqlite3_errmsg(self->db), sqlite3_errcode(self->db)); }
00022 
00023 gboolean log_db_log_btprox(LogDb* self, 
00024          GPtrArray* items,
00025          GError** error)
00026 {
00027   //logt("log_db_log_btprox");
00028 
00029   time_t now = time(NULL);
00030   if (now == -1) {
00031     if (error)
00032       *error = gx_error_new(domain_cl2app, code_time_query, "failed to get current time: %s (%d)", strerror(errno), errno);
00033     return FALSE;
00034   }
00035 
00036   mutex_lock(&self->mutex);
00037 
00038   gboolean rval = TRUE;
00039   gboolean inTran = FALSE;
00040 
00041   // Remember that after an sqlite3_step returns SQLITE_DONE, a
00042   // prepared statement may not be used again before resetting it.
00043   assert(self->stmts.transactionBeginStmt);
00044   if (sqlite3_step(self->stmts.transactionBeginStmt) != SQLITE_DONE)
00045     goto sql_fail;
00046   inTran = TRUE;
00047   if (sqlite3_reset(self->stmts.transactionBeginStmt))
00048     goto sql_fail;
00049   //logt("transaction began");
00050 
00051   assert(self->stmts.btproxScanStmt);
00052   if (sqlite3_bind_int(self->stmts.btproxScanStmt, 1, now))
00053     goto sql_fail;
00054   if (sqlite3_step(self->stmts.btproxScanStmt) != SQLITE_DONE)
00055     goto sql_fail;
00056   if (sqlite3_reset(self->stmts.btproxScanStmt))
00057     goto sql_fail;
00058   //logt("btprox scan entry insert done");
00059 
00060   assert(self->db);
00061   sqlite3_int64 scanId = sqlite3_last_insert_rowid(self->db);
00062   assert((scanId != 0) && "just did an insert so there should be a row ID");
00063   //logt("got row id");
00064 
00065   // It is quite possible for us to have found 0 items. These will not
00066   // be visible if one takes a product over the scan and item tables.
00067   assert(items);
00068   //logst;
00069   int count;
00070   //logg("numItems = %d", items->len);
00071   for (count = 0; count < items->len; count++) {
00072     btprox_item* item = (btprox_item*)g_ptr_array_index(items, count);
00073     //logg("count = %d", count);
00074     assert(item);
00075     //logg("addr '%s'", item->address);
00076     assert(item->address);
00077     assert(item->name);
00078     //logg("name '%s'", item->name);
00079     //logg("bt device '%s' '%s'", item->address, item->name);
00080 
00081     if (sqlite3_bind_int64(self->stmts.btproxItemStmt, 1, scanId))
00082       goto sql_fail;
00083     if (sqlite3_bind_text(self->stmts.btproxItemStmt, 2, item->address, strlen(item->address), SQLITE_TRANSIENT))
00084       goto sql_fail;
00085     if (sqlite3_bind_text(self->stmts.btproxItemStmt, 3, item->name, strlen(item->name), SQLITE_TRANSIENT))
00086       goto sql_fail;
00087     if (sqlite3_step(self->stmts.btproxItemStmt) != SQLITE_DONE)
00088       goto sql_fail;
00089     if (sqlite3_reset(self->stmts.btproxItemStmt))
00090       goto sql_fail;
00091   }
00092 
00093   goto done;
00094 
00095  sql_fail:
00096   SET_BTPROX_SQL_ERROR;
00097     
00098  done:
00099   if (inTran) {
00100     if (rval) {
00101       // Commit.
00102       if (sqlite3_step(self->stmts.transactionCommitStmt) == SQLITE_DONE) {
00103   if (sqlite3_reset(self->stmts.transactionCommitStmt))
00104     SET_BTPROX_SQL_ERROR;
00105       } else {
00106   SET_BTPROX_SQL_ERROR;
00107       }
00108     } else {
00109       // Rollback. Best effort.
00110       if (sqlite3_step(self->stmts.transactionRollbackStmt) == SQLITE_DONE)
00111   // Best effort.
00112   sqlite3_reset(self->stmts.transactionRollbackStmt);
00113     }
00114   }
00115   mutex_unlock(&self->mutex);
00116   return rval;
00117 }
00118 
00119 #endif // __BTPROX_ENABLED__
00120 
00121 #endif // defined(__SYMBIAN32__)
00122 
00123 /**
00124 
00125 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00126 and the authors. All rights reserved.
00127 
00128 Authors: Tero Hasu <tero.hasu@hut.fi>
00129 
00130 Permission is hereby granted, free of charge, to any person
00131 obtaining a copy of this software and associated documentation files
00132 (the "Software"), to deal in the Software without restriction,
00133 including without limitation the rights to use, copy, modify, merge,
00134 publish, distribute, sublicense, and/or sell copies of the Software,
00135 and to permit persons to whom the Software is furnished to do so,
00136 subject to the following conditions:
00137 
00138 The above copyright notice and this permission notice shall be
00139 included in all copies or substantial portions of the Software.
00140 
00141 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00142 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00143 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00144 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00145 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00146 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00147 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00148 SOFTWARE.
00149 
00150  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1