debug.c File Reference

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "pi-debug.h"
#include "pi-threadsafe.h"

Include dependency graph for debug.c:

Go to the source code of this file.

Functions

static PI_MUTEX_DEFINE (logfile_mutex)
int pi_debug_get_types (void)
void pi_debug_set_types (int types)
int pi_debug_get_level (void)
void pi_debug_set_level (int level)
void pi_debug_set_file (const char *path)
void pi_log (int type, int level, const char *format,...)
void pi_dumpline (const char *buf, size_t len, unsigned int addr)
void dumpline (const char *buf, size_t len, unsigned int addr)
void pi_dumpdata (const char *buf, size_t len)
void dumpdata (const char *buf, size_t len)

Variables

static int debug_types = PI_DBG_NONE
static int debug_level = PI_DBG_LVL_NONE
static FILE * debug_file = NULL


Function Documentation

void dumpdata ( const char *  buf,
size_t  len 
)

Definition at line 231 of file debug.c.

References pi_dumpdata().

00232 {
00233         /* this function will be removed in 0.13. Use pi_dumpdata() instead */
00234         pi_dumpdata(buf, len);
00235 }

Here is the call graph for this function:

void dumpline ( const char *  buf,
size_t  len,
unsigned int  addr 
)

Definition at line 215 of file debug.c.

References pi_dumpline().

00216 {
00217         /* this function will be removed in 0.13. Use pi_dumpline() instead. */
00218         pi_dumpline(buf, len, addr);
00219 }

Here is the call graph for this function:

int pi_debug_get_level ( void   ) 

Definition at line 84 of file debug.c.

References debug_level.

00085 {
00086         return debug_level;
00087 }

int pi_debug_get_types ( void   ) 

Definition at line 48 of file debug.c.

References debug_types.

00049 {
00050         return debug_types;
00051 }

void pi_debug_set_file ( const char *  path  ) 

Definition at line 121 of file debug.c.

References debug_file, pi_mutex_lock(), and pi_mutex_unlock().

Referenced by env_dbgcheck().

00122 {
00123         pi_mutex_lock(&logfile_mutex);
00124 
00125         if (debug_file != NULL && debug_file != stderr)
00126                 fclose (debug_file);
00127 
00128         debug_file = fopen (path, "a");
00129         if (debug_file == NULL)
00130                 debug_file = stderr;
00131 
00132         pi_mutex_unlock(&logfile_mutex);
00133 }

Here is the call graph for this function:

void pi_debug_set_level ( int  level  ) 

Definition at line 102 of file debug.c.

References debug_level, pi_mutex_lock(), and pi_mutex_unlock().

Referenced by env_dbgcheck().

00103 {
00104         pi_mutex_lock(&logfile_mutex);
00105         debug_level = level;
00106         pi_mutex_unlock(&logfile_mutex);
00107 }

Here is the call graph for this function:

void pi_debug_set_types ( int  types  ) 

Definition at line 66 of file debug.c.

References debug_types.

Referenced by env_dbgcheck().

00067 {
00068         debug_types = types;
00069 }

void pi_dumpdata ( const char *  buf,
size_t  len 
)

Definition at line 222 of file debug.c.

References pi_dumpline().

00223 {
00224         unsigned int i;
00225 
00226         for (i = 0; i < len; i += 16)
00227                 pi_dumpline(buf + i, ((len - i) > 16) ? 16 : len - i, i);
00228 }

Here is the call graph for this function:

void pi_dumpline ( const char *  buf,
size_t  len,
unsigned int  addr 
)

Definition at line 176 of file debug.c.

References line, LOG, PI_DBG_ALL, and PI_DBG_LVL_NONE.

00177 {
00178         unsigned int i;
00179         int offset;
00180         char line[256];
00181 
00182         offset = sprintf(line, "  %.4x  ", addr);
00183 
00184         for (i = 0; i < 16; i++) {
00185                 if (i < len)
00186                         offset += sprintf(line+offset, "%.2x ",
00187                                0xff & (unsigned int) buf[i]);
00188                 else {
00189                         strcpy(line+offset, "   ");
00190                         offset += 3;
00191                 }
00192         }
00193 
00194         strcpy(line+offset, "  ");
00195         offset += 2;
00196 
00197         for (i = 0; i < len; i++) {
00198                 if (buf[i] == '%') {
00199                         /* since we're going through pi_log, we need to
00200                          * properly escape % characters
00201                          */
00202                         line[offset++] = '%';
00203                         line[offset++] = '%';
00204                 } else if (isprint(buf[i]) && buf[i] >= 32 && buf[i] <= 126)
00205                         line[offset++] = buf[i];
00206                 else
00207                         line[offset++] = '.';
00208         }
00209 
00210         strcpy(line+offset,"\n");
00211         LOG((PI_DBG_ALL, PI_DBG_LVL_NONE, line));
00212 }

void pi_log ( int  type,
int  level,
const char *  format,
  ... 
)

Definition at line 148 of file debug.c.

References debug_file, debug_level, debug_types, PI_DBG_ALL, pi_mutex_lock(), pi_mutex_unlock(), and pi_thread_id().

00149 {
00150         va_list ap;
00151 
00152         if (!(debug_types & type) && type != PI_DBG_ALL)
00153                 return;
00154         
00155         if (debug_level < level)
00156                 return;
00157 
00158         pi_mutex_lock(&logfile_mutex);
00159 
00160         if (debug_file == NULL)
00161                 debug_file = stderr;
00162 
00163 #if HAVE_PTHREAD
00164         fprintf(debug_file, "[thread 0x%08lx] ", pi_thread_id());
00165 #endif
00166         va_start(ap, format);
00167         vfprintf(debug_file, format, ap);
00168         va_end(ap);
00169 
00170         fflush(debug_file);
00171 
00172         pi_mutex_unlock(&logfile_mutex);
00173 }

Here is the call graph for this function:

static PI_MUTEX_DEFINE ( logfile_mutex   )  [static]


Variable Documentation

FILE* debug_file = NULL [static]

Definition at line 33 of file debug.c.

Referenced by pi_debug_set_file(), and pi_log().

int debug_level = PI_DBG_LVL_NONE [static]

Definition at line 32 of file debug.c.

Referenced by pi_debug_get_level(), pi_debug_set_level(), and pi_log().

int debug_types = PI_DBG_NONE [static]

Definition at line 31 of file debug.c.

Referenced by pi_debug_get_types(), pi_debug_set_types(), and pi_log().


© 1996-2007 by pilot-link.org. All rights reserved.