Changeset: e736c0aa82e0 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e736c0aa82e0
Modified Files:
        clients/ChangeLog.Dec2011
        clients/odbc/driver/ODBCGlobal.h
        clients/odbc/driver/ODBCUtil.c
        clients/odbc/driver/SQLBrowseConnect.c
        clients/odbc/driver/SQLDriverConnect.c
Branch: Dec2011
Log Message:

ODBC: Implemented logfile attribute in connection string.
The value should be the absolute path name of a file to which an
interaction log is written.  The environment variable ODBCDEBUG is
used for the same thing and overrides the attribute.


diffs (185 lines):

diff --git a/clients/ChangeLog.Dec2011 b/clients/ChangeLog.Dec2011
--- a/clients/ChangeLog.Dec2011
+++ b/clients/ChangeLog.Dec2011
@@ -1,6 +1,14 @@
 # ChangeLog file for clients
 # This file is updated with Maddlog
 
+* Mon Oct 24 2011 Sjoerd Mullender <[email protected]>
+- ODBC: Implemented an easier way to create a log file of interactions
+  with the ODBC driver.  You can now add a connection attribute
+  "LOGFILE=filename" to the connection string parameter of
+  SQLBrowseConnect and SQLDriverConnect, and to the relevant part of
+  the Windows registry or odbc.ini file.  This value is only used if
+  there is no environment variable ODBCDEBUG.
+
 * Wed Oct 12 2011 Fabian Groffen <[email protected]>
 - Quoting of object names for mclient's \d command is now more flexible
   and consistent with standard SQL quoting rules, bug #2846.
diff --git a/clients/odbc/driver/ODBCGlobal.h b/clients/odbc/driver/ODBCGlobal.h
--- a/clients/odbc/driver/ODBCGlobal.h
+++ b/clients/odbc/driver/ODBCGlobal.h
@@ -116,18 +116,27 @@ SQLRETURN SQLFreeHandle_(SQLSMALLINT han
 SQLRETURN SQLGetDiagRec_(SQLSMALLINT handleType, SQLHANDLE handle, SQLSMALLINT 
recNumber, SQLCHAR *sqlState, SQLINTEGER *nativeErrorPtr, SQLCHAR *messageText, 
SQLSMALLINT bufferLength, SQLSMALLINT *textLengthPtr);
 
 #ifdef ODBCDEBUG
+extern const char *ODBCdebug;
+
 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
-#define ODBCLOG(...)   do {                                            \
-                               char *_s = getenv("ODBCDEBUG");         \
-                               if (_s && *_s) {                        \
-                                       FILE *_f;                       \
-                                       _f = fopen(_s, "a");            \
-                                       if (_f) {                       \
-                                               fprintf(_f, __VA_ARGS__); \
-                                               fclose(_f);             \
-                                       }                               \
-                               }                                       \
-                       } while (0)
+#define ODBCLOG(...)                                                   \
+       do {                                                            \
+               if (ODBCdebug == NULL) {                                \
+                       if ((ODBCdebug = getenv("ODBCDEBUG")) == NULL)  \
+                               ODBCdebug = strdup("");                 \
+                       else                                            \
+                               ODBCdebug = strdup(ODBCdebug);          \
+               }                                                       \
+               if (ODBCdebug != NULL && *ODBCdebug != 0) {             \
+                       FILE *_f;                                       \
+                       _f = fopen(ODBCdebug, "a");                     \
+                       if (_f == NULL)                                 \
+                               _f = stderr;                            \
+                       fprintf(_f, __VA_ARGS__);                       \
+                       if (_f != stderr)                               \
+                               fclose(_f);                             \
+               }                                                       \
+       } while (0)
 #else
 extern void ODBCLOG(_In_z_ _Printf_format_string_ const char *fmt, ...)
        __attribute__((__format__(__printf__, 1, 2)));
diff --git a/clients/odbc/driver/ODBCUtil.c b/clients/odbc/driver/ODBCUtil.c
--- a/clients/odbc/driver/ODBCUtil.c
+++ b/clients/odbc/driver/ODBCUtil.c
@@ -452,6 +452,9 @@ struct sql_types ODBC_c_types[] = {
 };
 
 #ifdef ODBCDEBUG
+
+const char *ODBCdebug;
+
 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
 #include <stdarg.h>
 
@@ -459,13 +462,18 @@ void
 ODBCLOG(const char *fmt, ...)
 {
        va_list ap;
-       char *s = getenv("ODBCDEBUG");
 
        va_start(ap, fmt);
-       if (s && *s) {
+       if (ODBCdebug == NULL) {
+               if ((ODBCdebug = getenv("ODBCDEBUG")) == NULL)
+                       ODBCdebug = strdup("");
+               else
+                       ODBCdebug = strdup(ODBCdebug);
+       }
+       if (ODBCdebug != NULL && *ODBCdebug != 0) {
                FILE *f;
 
-               f = fopen(s, "a");
+               f = fopen(ODBCdebug, "a");
                if (f) {
                        vfprintf(f, fmt, ap);
                        fclose(f);
diff --git a/clients/odbc/driver/SQLBrowseConnect.c 
b/clients/odbc/driver/SQLBrowseConnect.c
--- a/clients/odbc/driver/SQLBrowseConnect.c
+++ b/clients/odbc/driver/SQLBrowseConnect.c
@@ -108,6 +108,15 @@ SQLBrowseConnect_(ODBCDbc *dbc,
                } else if (strcasecmp(key, "database") == 0 && dbname == NULL) {
                        dbname = attr;
                        allocated |= 16;
+#ifdef ODBCDEBUG
+               } else if (strcasecmp(key, "logfile") == 0 &&
+                          getenv("ODBCDEBUG") == NULL) {
+                       /* environment trumps everything */
+                       if (ODBCdebug)
+                               free((void *) ODBCdebug); /* discard const */
+                       ODBCdebug = attr;
+                       allocated |= 32;
+#endif
                } else
                        free(attr);
                free(key);
@@ -148,6 +157,15 @@ SQLBrowseConnect_(ODBCDbc *dbc,
                                allocated |= 16;
                        }
                }
+#ifdef ODBCDEBUG
+               if ((allocated & 32) == 0 && getenv("ODBCDEBUG") == NULL) {
+                       /* if not set from InConnectionString argument
+                        * or environment, look in profile */
+                       n = SQLGetPrivateProfileString(dsn, "logfile", "", buf, 
sizeof(buf), "odbc.ini");
+                       if (n > 0 && buf[0])
+                               ODBCdebug = strdup(buf);
+               }
+#endif
        }
 
        if (uid != NULL && pwd != NULL) {
@@ -188,6 +206,15 @@ SQLBrowseConnect_(ODBCDbc *dbc,
                        OutConnectionString += 21;
                        BufferLength -= 21;
                }
+#ifdef ODBCDEBUG
+               if (ODBCdebug == NULL) {
+                       if (BufferLength > 0)
+                               strncpy((char *) OutConnectionString, 
"*LOGFILE:Debug log file=?;", BufferLength);
+                       len += 26;
+                       OutConnectionString += 26;
+                       BufferLength -= 26;
+               }
+#endif
 
                if (StringLength2Ptr)
                        *StringLength2Ptr = len;
diff --git a/clients/odbc/driver/SQLDriverConnect.c 
b/clients/odbc/driver/SQLDriverConnect.c
--- a/clients/odbc/driver/SQLDriverConnect.c
+++ b/clients/odbc/driver/SQLDriverConnect.c
@@ -163,6 +163,13 @@ SQLDriverConnect_(ODBCDbc *dbc,
                else if (strcasecmp(key, "port") == 0 && port == 0) {
                        port = atoi(attr);
                        free(attr);
+#ifdef ODBCDEBUG
+               } else if (strcasecmp(key, "logfile") == 0 &&
+                          getenv("ODBCDEBUG") == NULL) {
+                       if (ODBCdebug)
+                               free((void *) ODBCdebug); /* discard const */
+                       ODBCdebug = strdup(attr);
+#endif
                } else
                        free(attr);
                free(key);
@@ -261,6 +268,21 @@ SQLDriverConnect_(ODBCDbc *dbc,
                                BufferLength = -1;
                        }
                }
+#ifdef ODBCDEBUG
+               if (ODBCdebug) {
+                       if (BufferLength > 0) {
+                               n = snprintf((char *) OutConnectionString,
+                                            BufferLength,
+                                            "LOGFILE=%s;", ODBCdebug);
+                               if (n < 0)
+                                       n = BufferLength + 1;
+                               BufferLength -= n;
+                               OutConnectionString += n;
+                       } else {
+                               BufferLength = -1;
+                       }
+               }
+#endif
 
                /* calculate how much space was needed */
                if (StringLength2Ptr)
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to