comlog in db module
authorJiří Filipovič <fila@ics.muni.cz>
Thu, 18 Mar 2010 13:05:35 +0000 (13:05 +0000)
committerJiří Filipovič <fila@ics.muni.cz>
Thu, 18 Mar 2010 13:05:35 +0000 (13:05 +0000)
org.glite.lbjp-common.db/Makefile
org.glite.lbjp-common.db/interface/db.h
org.glite.lbjp-common.db/src/db-mysql.c
org.glite.lbjp-common.db/src/db-pg.c
org.glite.lbjp-common.db/src/db.c

index 97ff011..e9f6699 100644 (file)
@@ -68,7 +68,7 @@ COMPILE:=libtool --mode=compile ${CC} ${CFLAGS}
 LINK:=libtool --mode=link ${CC} -rpath ${stagedir}/${libdir} ${LDFLAGS} 
 INSTALL:=libtool --mode=install install
 
-EXT_LIBS:=-lglite_lbu_trio -lpthread ${DL_LIBS}
+EXT_LIBS:=-lglite_lbu_trio -lglite_lbu_log -lpthread ${DL_LIBS}
 TESTOBJS:=${OBJS} dbtest.o
 OBJS:=${OBJS} db.o
 HDRS:=db.h
index de1b8a9..4de56ca 100644 (file)
@@ -155,7 +155,7 @@ int glite_lbu_DBClearError(glite_lbu_DBContext ctx);
  * \param[in]  backend  required database backend
  * \return     error code
  */
-int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx, int backend);
+int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx, int backend, char *log_category);
 
 
 /**
@@ -417,6 +417,7 @@ long int glite_lbu_Lastid(glite_lbu_Statement stmt);
  * @} database group
  */
 
+static char *set_log_category;
 
 #ifdef __cplusplus
 }
index d3ed647..74891bb 100644 (file)
@@ -29,9 +29,6 @@ limitations under the License.
 #include <stdarg.h>
 #include <dlfcn.h>
 #include <pthread.h>
-#ifdef HAVE_SYSLOG_H
-#include <syslog.h>
-#endif
 
 #ifdef WIN32
 #include <windows.h>
@@ -48,6 +45,7 @@ limitations under the License.
 #include <errmsg.h>
 
 #include "glite/lbu/trio.h"
+#include "glite/lbu/log.h"
 #include "db.h"
 #include "db-int.h"
 
@@ -449,6 +447,7 @@ int glite_lbu_QueryIndicesMysql(glite_lbu_DBContext ctx_gen, const char *table,
        Key_name = Seq_in_index = Column_name = Sub_part = -1;
 
        asprintf(&sql, "show index from %s", table);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, sql);
        if (glite_lbu_ExecSQLMysql(ctx_gen,sql,&stmt)<0) {
                free(sql);
                return STATUS(ctx);
@@ -957,10 +956,12 @@ static int transaction_test(glite_lbu_DBContext ctx, int *caps) {
 
        (*caps) &= ~GLITE_LBU_DB_CAP_TRANSACTIONS;
 
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, "SHOW TABLES");
        if ((retval = glite_lbu_ExecSQLMysql(ctx, "SHOW TABLES", &stmt)) <= 0 || glite_lbu_FetchRowMysql(stmt, 1, NULL, table) < 0) goto quit;
        glite_lbu_FreeStmtMysql(&stmt);
 
        trio_asprintf(&cmd, "SHOW CREATE TABLE %|Ss", table[0]);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, cmd);
        if (glite_lbu_ExecSQLMysql(ctx, cmd, &stmt) <= 0 || (retval = glite_lbu_FetchRowMysql(stmt, 2, NULL, res)) < 0 ) goto quit;
        if (retval != 2 || strcmp(res[0], table[0])) {
                ERR(ctx, EIO, "unexpected show create result");
index 940015d..d8af7c8 100644 (file)
@@ -37,6 +37,7 @@ limitations under the License.
 #include <libpq-fe.h>
 
 #include "glite/lbu/trio.h"
+#include "glite/lbu/log.h"
 #include "db.h"
 #include "db-int.h"
 
@@ -48,11 +49,11 @@ limitations under the License.
 
 #define DB_CONNECT_TIMEOUT "20"
 
-#ifdef LOG
+/*#ifdef LOG
   #define lprintf(FMT...) fprintf(stdout, "[db-pg] %s: ", __FUNCTION__); fprintf(stdout, ##FMT);
 #else
   #define lprintf(FMT...)
-#endif
+#endif*/
 
 #define set_error(CTX, CODE, DESC...) glite_lbu_DBSetError((glite_lbu_DBContext)(CTX), (CODE), __FUNCTION__, __LINE__, ##DESC)
 
@@ -245,7 +246,8 @@ int glite_lbu_DBConnectPsql(glite_lbu_DBContext ctx_gen, const char *cs) {
        else pgcs = pgcsbuf;
        free(buf);
 
-       lprintf("connection string = %s\n", pgcs);
+        glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, 
+               "connection string = %s\n", pgcs);
        ctx->conn = psql_module.PQconnectdb(pgcs);
        free(pgcsbuf);
        if (!ctx->conn) return ENOMEM;
@@ -282,6 +284,7 @@ int glite_lbu_DBQueryCapsPsql(glite_lbu_DBContext ctx_gen) {
        int has_prepared = 0;
        char *res = NULL;
 
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, "SHOW server_version");
        if (glite_lbu_ExecSQLPsql(ctx_gen, "SHOW server_version", &stmt) == -1) return -1;
        switch (glite_lbu_FetchRowPsql(stmt, 1, NULL, &res)) {
        case 1:
@@ -365,6 +368,7 @@ void glite_lbu_FreeStmtPsql(glite_lbu_Statement *stmt_gen) {
        if (stmt->res) psql_module.PQclear(stmt->res);
        if (stmt->name) {
                asprintf(&sql, "DEALLOCATE %s", stmt->name);
+               glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, sql);
                stmt->res = psql_module.PQexec(ctx->conn, sql);
                free(sql);
                psql_module.PQclear(stmt->res);
@@ -383,7 +387,8 @@ int glite_lbu_ExecSQLPsql(glite_lbu_DBContext ctx_gen, const char *cmd, glite_lb
        char *nstr, *errmsg, *pos;
        PGresult *res;
 
-       lprintf("command = %s\n", cmd);
+       //lprintf("command = %s\n", cmd);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, "command = %s\n", cmd);
        if (stmt_out) *stmt_out = NULL;
        if ((res = psql_module.PQexec(ctx->conn, cmd)) == NULL) {
                ctx->generic.err.code = ENOMEM;
@@ -457,7 +462,7 @@ int glite_lbu_PrepareStmtPsql(glite_lbu_DBContext ctx_gen, const char *sql, glit
        asprintf(&stmt->name, "%s%d", prepared_names[i], ++ctx->prepared_counts[i]);
 
        asprintf(&sqlPrep, "PREPARE %s AS %s", stmt->name, stmt->sql);
-       lprintf("prepare = %s\n", sqlPrep);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, sqlPrep);
        res = psql_module.PQexec(ctx->conn, sqlPrep);
        if (psql_module.PQresultStatus(res) != PGRES_COMMAND_OK) {
                asprintf(&s, "error preparing command: %s", psql_module.PQerrorMessage(ctx->conn));
@@ -528,12 +533,13 @@ int glite_lbu_ExecPreparedStmtPsql_v(glite_lbu_Statement stmt_gen, int n, va_lis
 
                        s = va_arg(ap, char *);
                        binary_len = va_arg(ap, unsigned long);
-                       lprintf("blob, len = %lu, ptr = %p\n", binary_len, s);
+                       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, 
+                               "blob, len = %lu, ptr = %p\n", binary_len, s);
                        if (s) {
                                tmp = malloc(2*binary_len + 1);
                                psql_module.PQescapeStringConn(ctx->conn, tmp, s, binary_len, NULL);
                                asprintf(&tmpdata[i], "'%s'", tmp);
-                               lprintf("escaped: '%s'\n", tmpdata[i]);
+                               glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, "escaped: '%s'\n", tmpdata[i]);
                                free(tmp);
                        } else
                                tmpdata[i] = strdup("NULL");
@@ -567,7 +573,8 @@ int glite_lbu_ExecPreparedStmtPsql_v(glite_lbu_Statement stmt_gen, int n, va_lis
                        break;
 
                default:
-                       lprintf("unknown type %d\n", type);
+                       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG,
+                               "unknown type %d\n", type);
                        set_error(ctx, EINVAL, "unimplemented type");
                        goto quit;
                }
@@ -585,7 +592,7 @@ int glite_lbu_ExecPreparedStmtPsql_v(glite_lbu_Statement stmt_gen, int n, va_lis
        }
        if (n) strcat(sql, ")");
 
-       lprintf("exec prepared: n = %d, sql = '%s'\n", n, sql);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, sql);
        stmt->res = psql_module.PQexec(ctx->conn, sql);
        status = psql_module.PQresultStatus(stmt->res);
        if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
@@ -618,3 +625,4 @@ static void glite_lbu_DBCleanup(void) {
        }
        pthread_mutex_unlock(&psql_module.lock);
 }
+
index 525a07a..429f727 100644 (file)
@@ -26,6 +26,7 @@ limitations under the License.
 #include <unistd.h>
 
 #include <glite/lbu/trio.h>
+#include "glite/lbu/log.h"
 
 #include "db.h"
 #include "db-int.h"
@@ -97,7 +98,8 @@ int glite_lbu_DBSetError(glite_lbu_DBContext ctx, int code, const char *func, in
                va_end(ap);
        } else
                ctx->err.desc = NULL;
-       dprintf(ctx, "[db %d] %s:%d %s\n", getpid(), func, line, ctx->err.desc);
+       glite_common_log(set_log_category, LOG_PRIORITY_WARN, 
+               "[db %d] %s:%d %s\n", getpid(), func, line, ctx->err.desc);
        return code;
 }
 
@@ -176,13 +178,14 @@ int glite_lbu_DBError(glite_lbu_DBContext ctx, char **text, char **desc) {
 }
 
 
-int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx, int backend) {
+int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx, int backend, char *log_category) {
        int ret;
 
        if (!VALID(backend)) return EINVAL;
        if (backends[backend]->backend != backend) return ENOTSUP;
        ret = backends[backend]->initContext(ctx);
        if (ctx && *ctx) (*ctx)->backend = backend;
+       set_log_category = log_category;
        return ret;
 }
 
@@ -360,6 +363,7 @@ static int flush_bufferd_insert(glite_lbu_bufInsert bi)
        
        trio_asprintf(&stmt, "insert into %|Ss(%|Ss) values %s;",
                bi->table_name, bi->columns, vals);
+       glite_common_log(set_log_category, LOG_PRIORITY_DEBUG, stmt);
 
        if (glite_lbu_ExecSQL(bi->ctx,stmt,NULL) < 0) {
                 if (STATUS(bi->ctx) == EEXIST)