Skip to Content.
Sympa Menu

freetds - [freetds] Log, patch and format

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: "ZIGLIO, Frediano, VF-IT" <Frediano.Ziglio AT vodafone.com>
  • To: "FreeTDS Development Group" <freetds AT lists.ibiblio.org>
  • Subject: [freetds] Log, patch and format
  • Date: Thu, 9 Dec 2004 11:28:45 +0100

I'm trying to change log format to include source line informations (as
suggested)

Here is a patch (0.64)




Index: include/tds.h
===================================================================
RCS file: /cvsroot/freetds/freetds/include/tds.h,v
retrieving revision 1.200
diff -u -1 -0 -r1.200 tds.h
--- include/tds.h 7 Dec 2004 22:39:21 -0000 1.200
+++ include/tds.h 9 Dec 2004 10:16:29 -0000
@@ -896,27 +896,27 @@
/* values for tds->state */
typedef enum _TDS_STATE
{
TDS_QUERYING,
TDS_PENDING,
TDS_IDLE,
TDS_CANCELED,
TDS_DEAD
} TDS_STATE;

-#define TDS_DBG_FUNC 7
-#define TDS_DBG_INFO2 6
-#define TDS_DBG_INFO1 5
-#define TDS_DBG_NETWORK 4
-#define TDS_DBG_WARN 3
-#define TDS_DBG_ERROR 2
-#define TDS_DBG_SEVERE 1
+#define TDS_DBG_FUNC __FILE__, ((__LINE__ << 4) | 7)
+#define TDS_DBG_INFO2 __FILE__, ((__LINE__ << 4) | 6)
+#define TDS_DBG_INFO1 __FILE__, ((__LINE__ << 4) | 5)
+#define TDS_DBG_NETWORK __FILE__, ((__LINE__ << 4) | 4)
+#define TDS_DBG_WARN __FILE__, ((__LINE__ << 4) | 3)
+#define TDS_DBG_ERROR __FILE__, ((__LINE__ << 4) | 2)
+#define TDS_DBG_SEVERE __FILE__, ((__LINE__ << 4) | 1)

/**
* An attempt at better logging.
* Using these bitmapped values, various logging features can be turned
on and off.
* It can be especially helpful to turn packet data on/off for security
reasons.
*/
enum TDS_DBG_LOG_STATE
{
TDS_DBG_LOGIN = 1 /* for diagnosing login
problems;
otherwise the
username/password information is suppressed. */
@@ -1282,24 +1282,24 @@
void *tds_get_parent(TDSSOCKET * tds);
void tds_ctx_set_parent(TDSCONTEXT * ctx, void *the_parent);
void *tds_ctx_get_parent(TDSCONTEXT * ctx);
int tds_swap_bytes(unsigned char *buf, int bytes);
int tds_version(TDSSOCKET * tds_socket, char *pversion_string);
void tdsdump_off(void);
void tdsdump_on(void);
int tdsdump_open(const char *filename);
int tdsdump_append(void);
void tdsdump_close(void);
-void tdsdump_dump_buf(int debug_lvl, const char *msg, const void *buf,
int length);
-void tdsdump_log(int dbg_lvl, const char *fmt, ...)
+void tdsdump_dump_buf(const char* file, unsigned int level_line, const
char *msg, const void *buf, int length);
+void tdsdump_log(const char* file, unsigned int level_line, const char
*fmt, ...)
#if defined(__GNUC__) && __GNUC__ >= 2
- __attribute__ ((__format__ (__printf__, 2, 3)))
+ __attribute__ ((__format__ (__printf__, 3, 4)))
#endif
;

/* net.c */
int tds_open_socket(TDSSOCKET * tds, const char *ip_addr, unsigned int
port, int timeout);
int tds_close_socket(TDSSOCKET * tds);
int tds_read_packet(TDSSOCKET * tds);
int tds_write_packet(TDSSOCKET * tds, unsigned char final);

/* vstrbuild.c */
Index: src/tds/util.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/tds/util.c,v
retrieving revision 1.51
diff -u -1 -0 -r1.51 util.c
--- src/tds/util.c 7 Dec 2004 22:39:21 -0000 1.51
+++ src/tds/util.c 9 Dec 2004 10:16:30 -0000
@@ -243,21 +243,21 @@
if (result == 1) {
char today[64];
struct tm *tm;
time_t t;

time(&t);
tm = localtime(&t);

tdsdump_on();
strftime(today, sizeof(today), "%Y-%m-%d %H:%M:%S", tm);
- tdsdump_log(tds_g_debug_lvl, "Starting log file for
FreeTDS %s\n"
+ tdsdump_log(NULL, tds_g_debug_lvl, "Starting log file
for FreeTDS %s\n"
"\ton %s with debug level %d.\n", VERSION,
today, tds_g_debug_lvl);
}
return result;
} /* tdsdump_open() */

int
tdsdump_append(void)
{
int result;

@@ -289,56 +289,68 @@
tdsdump_off();
if (dumpfile != NULL && dumpfile != stdout && dumpfile !=
stderr) {
fclose(dumpfile);
}
dumpfile = NULL;
if (g_dump_filename)
TDS_ZERO_FREE(g_dump_filename);
} /* tdsdump_close() */

static void
-tdsdump_start(FILE *file)
+tdsdump_start(FILE *file, const char *fname, int line)
{
char buf[128];

- if (tds_g_append_mode)
- fprintf(dumpfile, "pid: %d:", (int) getpid());
-
/* write always time before log */
fputs(tds_timestamp_str(buf, 127), dumpfile);
fputc(' ', dumpfile);
+
+ if (fname && line) {
+ const char *p;
+ p = strrchr(fname, '/');
+ if (p)
+ fname = p + 1;
+ p = strrchr(fname, '\\');
+ if (p)
+ fname = p + 1;
+ fprintf(dumpfile, "%d (%s:%d):", (int) getpid(), fname,
line);
+ } else {
+ fprintf(dumpfile, "%d:", (int) getpid());
+ }
}

/**
* Dump the contents of data into the log file in a human readable
format.
* \param msg message to print before dump
* \param buf buffer to dump
* \param length number of bytes in the buffer
*/
void
-tdsdump_dump_buf(int debug_lvl, const char *msg, const void *buf, int
length)
+tdsdump_dump_buf(const char* file, unsigned int level_line, const char
*msg, const void *buf, int length)
{
int i;
int j;
const int bytesPerLine = 16;
const unsigned char *data = (const unsigned char *) buf;
+ const int debug_lvl = level_line & 15;
+ const int line = level_line >> 4;

if (debug_lvl > tds_g_debug_lvl || !write_dump)
return;

if (tds_g_append_mode && !tdsdump_append())
return;

if (dumpfile == NULL)
return;

- tdsdump_start(dumpfile);
+ tdsdump_start(dumpfile, file, line);

fprintf(dumpfile, "%s\n", msg);

for (i = 0; i < length; i += bytesPerLine) {
/*
* print the offset as a 4 digit hex number
*/
fprintf(dumpfile, "%04x", i);

/*
@@ -379,34 +391,36 @@
}
} /* tdsdump_dump_buf() */


/**
* This function write a message to the debug log.
* \param debug_lvl level of debugging
* \param fmt printf-like format string
*/
void
-tdsdump_log(int debug_lvl, const char *fmt, ...)
+tdsdump_log(const char* file, unsigned int level_line, const char *fmt,
...)
{
+ const int debug_lvl = level_line & 15;
+ const int line = level_line >> 4;
va_list ap;

if (debug_lvl > tds_g_debug_lvl || !write_dump)
return;

if (tds_g_append_mode && !tdsdump_append())
return;

if (dumpfile == NULL)
return;

- tdsdump_start(dumpfile);
+ tdsdump_start(dumpfile, file, line);

va_start(ap, fmt);

vfprintf(dumpfile, fmt, ap);
va_end(ap);

fflush(dumpfile);

if (tds_g_append_mode) {
if (dumpfile && dumpfile != stdout && dumpfile !=
stderr)




As you can see a used a small trick defining log levels like

#define TDS_DBG_FUNC __FILE__, ((__LINE__ << 4) | 7)

and adding extra parameter to log function.

It's working fine but log format it's not so fine. Just an extract (just
3 lines):

11:20:41.230206 31045 (token.c:320):looking for login token, got
e3(ENVCHANGE)
11:20:41.230238 31045 (token.c:105):tds_process_default_tokens() marker
is e3(ENVCHANGE)
11:20:41.230259 31045 (read.c:178):tds_get_string: reading 8 from wire
to give 4 to client.

As anyone can see line it's quite long. The reason it's that it contains
all possible informations:
- time
- pid
- source file and line
- message
I know that James don't like that much time at every line but it's very
useful to discover problems related to speed.
Perhaps it would be better to define some flags to enable/disable every
component (time, pid, source and perhaps thread id where pid1 == pid2 do
not means thread1 == thread2)

freddy77




Archive powered by MHonArc 2.6.24.

Top of Page