#include <stdarg.h>
 #include <dlfcn.h>
 #include <pthread.h>
-#ifdef HAVE_SYSLOG_H
-#include <syslog.h>
-#endif
 
 #ifdef WIN32
 #include <windows.h>
 #include <errmsg.h>
 
 #include "glite/lbu/trio.h"
+#include "glite/lbu/log.h"
 #include "db.h"
 #include "db-int.h"
 
        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);
 
        (*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");
 
 #include <libpq-fe.h>
 
 #include "glite/lbu/trio.h"
+#include "glite/lbu/log.h"
 #include "db.h"
 #include "db-int.h"
 
 
 #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)
 
        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;
        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:
        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);
        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;
        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));
 
                        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");
                        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;
                }
        }
        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) {
        }
        pthread_mutex_unlock(&psql_module.lock);
 }
+
 
 #include <unistd.h>
 
 #include <glite/lbu/trio.h>
+#include "glite/lbu/log.h"
 
 #include "db.h"
 #include "db-int.h"
                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;
 }
 
 }
 
 
-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;
 }
 
        
        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)