This commit is contained in:
Marcelo Roberto Jimenez 2010-12-13 09:33:49 -02:00
parent 3dd133a03c
commit e8106e4f05
2 changed files with 84 additions and 144 deletions

View File

@ -31,7 +31,7 @@
******************************************************************************/ ******************************************************************************/
#ifndef UPNP_DEBUG_H #ifndef UPNP_DEBUG_H
#define UPNP_DEBUG_H #define UPNP_DEBUG_H
/*! /*!
* \file * \file
@ -39,7 +39,7 @@
#include "ThreadPool.h" #include "ThreadPool.h"
#include "upnpconfig.h" #include "upnpconfig.h"
#include "UpnpGlobal.h" /* for UPNP_INLINE */ #include "UpnpGlobal.h" /* for UPNP_INLINE */
#include <stdio.h> #include <stdio.h>
@ -107,7 +107,6 @@ static UPNP_INLINE int UpnpInitLog(void)
return UPNP_E_SUCCESS; return UPNP_E_SUCCESS;
} }
#endif #endif
/*! /*!
* \brief Set the log level (see \c Upnp_LogLevel). * \brief Set the log level (see \c Upnp_LogLevel).
*/ */
@ -129,7 +128,9 @@ static UPNP_INLINE void UpnpSetLogLevel(Upnp_LogLevel log_level)
#ifdef DEBUG #ifdef DEBUG
void UpnpCloseLog(void); void UpnpCloseLog(void);
#else #else
static UPNP_INLINE void UpnpCloseLog(void) {} static UPNP_INLINE void UpnpCloseLog(void)
{
}
#endif #endif
/*! /*!
@ -142,8 +143,7 @@ void UpnpSetLogFileNames(
/*! [in] Name of the information file. */ /*! [in] Name of the information file. */
const char *InfoFileName); const char *InfoFileName);
#else #else
static UPNP_INLINE void UpnpSetLogFileNames( static UPNP_INLINE void UpnpSetLogFileNames(const char *ErrFileName,
const char *ErrFileName,
const char *InfoFileName) const char *InfoFileName)
{ {
return; return;
@ -175,7 +175,6 @@ static UPNP_INLINE FILE *UpnpGetDebugFile(Upnp_LogLevel level, Dbg_Module module
} }
#endif #endif
/*! /*!
* \brief Returns true if debug output should be done in this module. * \brief Returns true if debug output should be done in this module.
* *
@ -189,9 +188,7 @@ int DebugAtThisLevel(
/*! [in] Debug will go in the name of this module. */ /*! [in] Debug will go in the name of this module. */
Dbg_Module Module); Dbg_Module Module);
#else #else
static UPNP_INLINE int DebugAtThisLevel( static UPNP_INLINE int DebugAtThisLevel(Upnp_LogLevel DLevel, Dbg_Module Module)
Upnp_LogLevel DLevel,
Dbg_Module Module)
{ {
return 0; return 0;
DLevel = DLevel; DLevel = DLevel;
@ -199,11 +196,10 @@ static UPNP_INLINE int DebugAtThisLevel(
} }
#endif #endif
/*! /*!
* \brief Prints the debug statement either on the standard output or log file * \brief Prints the debug statement either on the standard output or log file
* along with the information from where this debug statement is coming. * along with the information from where this debug statement is coming.
*/ */
#ifdef DEBUG #ifdef DEBUG
void UpnpPrintf( void UpnpPrintf(
/*! [in] The level of the debug logging. It will decide whether debug /*! [in] The level of the debug logging. It will decide whether debug
@ -212,27 +208,22 @@ void UpnpPrintf(
/*! [in] debug will go in the name of this module. */ /*! [in] debug will go in the name of this module. */
Dbg_Module Module, Dbg_Module Module,
/*! [in] Name of the file from where debug statement is coming. */ /*! [in] Name of the file from where debug statement is coming. */
const char* DbgFileName, const char *DbgFileName,
/*! [in] Line number of the file from where debug statement is coming. */ /*! [in] Line number of the file from where debug statement is coming. */
int DbgLineNo, int DbgLineNo,
/*! [in] Printf like format specification. */ /*! [in] Printf like format specification. */
const char* FmtStr, const char *FmtStr,
/*! [in] Printf like Variable number of arguments that will go in the debug /*! [in] Printf like Variable number of arguments that will go in the
* statement. */ * debug statement. */
...) ...)
#if (__GNUC__ >= 3) #if (__GNUC__ >= 3)
/* This enables printf like format checking by the compiler */ /* This enables printf like format checking by the compiler. */
__attribute__((format (__printf__, 5, 6))) __attribute__ ((format(__printf__, 5, 6)))
#endif #endif
; ;
#else /* DEBUG */ #else /* DEBUG */
static UPNP_INLINE void UpnpPrintf( static UPNP_INLINE void UpnpPrintf(Upnp_LogLevel DLevel, Dbg_Module Module,
Upnp_LogLevel DLevel, const char *DbgFileName, int DbgLineNo, const char *FmtStr, ...)
Dbg_Module Module,
const char* DbgFileName,
int DbgLineNo,
const char* FmtStr,
...)
{ {
return; return;
DLevel = DLevel; DLevel = DLevel;
@ -243,24 +234,22 @@ static UPNP_INLINE void UpnpPrintf(
} }
#endif /* DEBUG */ #endif /* DEBUG */
/*! /*!
* \brief Writes the file name and file number from where debug statement is * \brief Writes the file name and file number from where debug statement is
* coming to the log file. * coming to the log file.
*/ */
#ifdef DEBUG #ifdef DEBUG
void UpnpDisplayFileAndLine( void UpnpDisplayFileAndLine(
/*! [in] File descriptor where line number and file name will be written. */ /*! [in] File descriptor where line number and file name will be
FILE *fd, * written. */
FILE * fd,
/*! [in] Name of the file. */ /*! [in] Name of the file. */
const char *DbgFileName, const char *DbgFileName,
/*! [in] Line number of the file. */ /*! [in] Line number of the file. */
int DbgLineNo); int DbgLineNo);
#else #else
static UPNP_INLINE void UpnpDisplayFileAndLine( static UPNP_INLINE void UpnpDisplayFileAndLine(FILE *fd,
FILE *fd, const char *DbgFileName, int DbgLineNo)
const char *DbgFileName,
int DbgLineNo)
{ {
return; return;
fd = fd; fd = fd;
@ -269,14 +258,13 @@ static UPNP_INLINE void UpnpDisplayFileAndLine(
} }
#endif #endif
/*! /*!
* \brief Writes the buffer in the file as per the requested banner * \brief Writes the buffer in the file as per the requested banner
*/ */
#ifdef DEBUG #ifdef DEBUG
void UpnpDisplayBanner( void UpnpDisplayBanner(
/*! [in] file descriptor where the banner will be written. */ /*! [in] file descriptor where the banner will be written. */
FILE *fd, FILE * fd,
/*! [in] The buffer that will be written. */ /*! [in] The buffer that will be written. */
const char **lines, const char **lines,
/*! [in] Size of the buffer. */ /*! [in] Size of the buffer. */
@ -284,11 +272,8 @@ void UpnpDisplayBanner(
/*! [in] This parameter provides the width of the banner. */ /*! [in] This parameter provides the width of the banner. */
size_t starlength); size_t starlength);
#else #else
static UPNP_INLINE void UpnpDisplayBanner( static UPNP_INLINE void UpnpDisplayBanner(FILE *fd, const char **lines,
FILE *fd, size_t size, int starlength)
const char **lines,
size_t size,
int starlength)
{ {
return; return;
fd = fd; fd = fd;
@ -298,26 +283,24 @@ static UPNP_INLINE void UpnpDisplayBanner(
} }
#endif #endif
/*! /*!
* \brief Prints thread pool statistics. * \brief Prints thread pool statistics.
*/ */
#ifdef DEBUG #ifdef DEBUG
void PrintThreadPoolStats( void PrintThreadPoolStats(
/*! [in] The thread pool. */ /*! [in] The thread pool. */
ThreadPool *tp, ThreadPool * tp,
/*! [in] The file name that called this function, use the macro __FILE__. */ /*! [in] The file name that called this function, use the macro
* __FILE__. */
const char *DbgFileName, const char *DbgFileName,
/*! [in] The line number that the function was called, use the macro __LINE__. */ /*! [in] The line number that the function was called, use the macro
* __LINE__. */
int DbgLineNo, int DbgLineNo,
/*! [in] The message. */ /*! [in] The message. */
const char *msg); const char *msg);
#else #else
static UPNP_INLINE void PrintThreadPoolStats( static UPNP_INLINE void PrintThreadPoolStats(ThreadPool *tp,
ThreadPool *tp, const char *DbgFileName, int DbgLineNo, const char *msg)
const char *DbgFileName,
int DbgLineNo,
const char *msg)
{ {
return; return;
tp = tp; tp = tp;
@ -334,4 +317,3 @@ static UPNP_INLINE void PrintThreadPoolStats(
#endif #endif
#endif /* UPNP_DEBUG_H */ #endif /* UPNP_DEBUG_H */

View File

@ -29,22 +29,22 @@
* *
******************************************************************************/ ******************************************************************************/
#include "config.h" /*!
* \file
*/
#include "config.h"
#include "ithread.h" #include "ithread.h"
#include "ixml.h" #include "ixml.h"
#include "upnp.h" #include "upnp.h"
#include "upnpdebug.h" #include "upnpdebug.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
/*! Mutex to synchronize all the log file opeartions in the debug mode */ /*! Mutex to synchronize all the log file opeartions in the debug mode */
static ithread_mutex_t GlobalDebugMutex; static ithread_mutex_t GlobalDebugMutex;
@ -63,31 +63,27 @@ static const char *errFileName = "IUpnpErrFile.txt";
/*! Name of the info file */ /*! Name of the info file */
static const char *infoFileName = "IUpnpInfoFile.txt"; static const char *infoFileName = "IUpnpInfoFile.txt";
#ifdef DEBUG #ifdef DEBUG
int UpnpInitLog(void) int UpnpInitLog(void)
{ {
ithread_mutex_init(&GlobalDebugMutex, NULL); ithread_mutex_init(&GlobalDebugMutex, NULL);
if(DEBUG_TARGET == 1) { if (DEBUG_TARGET == 1) {
if((ErrFileHnd = fopen( errFileName, "a")) == NULL) { if ((ErrFileHnd = fopen(errFileName, "a")) == NULL) {
return -1; return -1;
} }
if((InfoFileHnd = fopen( infoFileName, "a")) == NULL) { if ((InfoFileHnd = fopen(infoFileName, "a")) == NULL) {
return -1; return -1;
} }
} }
return UPNP_E_SUCCESS; return UPNP_E_SUCCESS;
} }
void UpnpSetLogLevel(Upnp_LogLevel log_level) void UpnpSetLogLevel(Upnp_LogLevel log_level)
{ {
g_log_level = log_level; g_log_level = log_level;
} }
void UpnpCloseLog(void) void UpnpCloseLog(void)
{ {
if (DEBUG_TARGET == 1) { if (DEBUG_TARGET == 1) {
@ -99,71 +95,56 @@ void UpnpCloseLog(void)
ithread_mutex_destroy(&GlobalDebugMutex); ithread_mutex_destroy(&GlobalDebugMutex);
} }
void UpnpSetLogFileNames(const char *ErrFileName, const char *InfoFileName)
void UpnpSetLogFileNames(
const char *ErrFileName,
const char *InfoFileName)
{ {
if (ErrFileName) { if (ErrFileName) {
errFileName = ErrFileName; errFileName = ErrFileName;
} }
if (InfoFileName) { if (InfoFileName) {
infoFileName = InfoFileName; infoFileName = InfoFileName;
} }
} }
int DebugAtThisLevel(Upnp_LogLevel DLevel, Dbg_Module Module)
int DebugAtThisLevel(
Upnp_LogLevel DLevel,
Dbg_Module Module)
{ {
int ret = DLevel <= g_log_level; int ret = DLevel <= g_log_level;
ret &= ret &=
DEBUG_ALL || DEBUG_ALL ||
(Module == SSDP && DEBUG_SSDP ) || (Module == SSDP && DEBUG_SSDP) ||
(Module == SOAP && DEBUG_SOAP ) || (Module == SOAP && DEBUG_SOAP) ||
(Module == GENA && DEBUG_GENA ) || (Module == GENA && DEBUG_GENA) ||
(Module == TPOOL && DEBUG_TPOOL) || (Module == TPOOL && DEBUG_TPOOL) ||
(Module == MSERV && DEBUG_MSERV) || (Module == MSERV && DEBUG_MSERV) ||
(Module == DOM && DEBUG_DOM ) || (Module == DOM && DEBUG_DOM) || (Module == HTTP && DEBUG_HTTP);
(Module == HTTP && DEBUG_HTTP );
return ret; return ret;
} }
void UpnpPrintf(Upnp_LogLevel DLevel,
void UpnpPrintf( Dbg_Module Module,
Upnp_LogLevel DLevel, const char *DbgFileName, int DbgLineNo, const char *FmtStr, ...)
Dbg_Module Module,
const char *DbgFileName,
int DbgLineNo,
const char *FmtStr,
...)
{ {
va_list ArgList; va_list ArgList;
if (!DebugAtThisLevel(DLevel, Module)) { if (!DebugAtThisLevel(DLevel, Module))
return; return;
}
ithread_mutex_lock(&GlobalDebugMutex); ithread_mutex_lock(&GlobalDebugMutex);
va_start(ArgList, FmtStr); va_start(ArgList, FmtStr);
if (!DEBUG_TARGET) { if (!DEBUG_TARGET) {
if (DbgFileName) { if (DbgFileName)
UpnpDisplayFileAndLine(stdout, DbgFileName, DbgLineNo); UpnpDisplayFileAndLine(stdout, DbgFileName, DbgLineNo);
}
vfprintf(stdout, FmtStr, ArgList); vfprintf(stdout, FmtStr, ArgList);
fflush(stdout); fflush(stdout);
} else if (DLevel == 0) { } else if (DLevel == 0) {
if (DbgFileName) { if (DbgFileName)
UpnpDisplayFileAndLine(ErrFileHnd, DbgFileName, DbgLineNo); UpnpDisplayFileAndLine(ErrFileHnd, DbgFileName,
} DbgLineNo);
vfprintf(ErrFileHnd, FmtStr, ArgList); vfprintf(ErrFileHnd, FmtStr, ArgList);
fflush(ErrFileHnd); fflush(ErrFileHnd);
} else { } else {
if (DbgFileName) { if (DbgFileName)
UpnpDisplayFileAndLine(InfoFileHnd, DbgFileName, DbgLineNo); UpnpDisplayFileAndLine(InfoFileHnd, DbgFileName,
} DbgLineNo);
vfprintf(InfoFileHnd, FmtStr, ArgList); vfprintf(InfoFileHnd, FmtStr, ArgList);
fflush(InfoFileHnd); fflush(InfoFileHnd);
} }
@ -171,31 +152,23 @@ void UpnpPrintf(
ithread_mutex_unlock(&GlobalDebugMutex); ithread_mutex_unlock(&GlobalDebugMutex);
} }
FILE *GetDebugFile(Upnp_LogLevel DLevel, Dbg_Module Module) FILE *GetDebugFile(Upnp_LogLevel DLevel, Dbg_Module Module)
{ {
FILE *ret; FILE *ret;
if (!DebugAtThisLevel(DLevel, Module)) { if (!DebugAtThisLevel(DLevel, Module))
ret = NULL; ret = NULL;
} if (!DEBUG_TARGET)
if (!DEBUG_TARGET) {
ret = stdout; ret = stdout;
} else if (DLevel == 0) { else if (DLevel == 0)
ret = ErrFileHnd; ret = ErrFileHnd;
} else { else
ret = InfoFileHnd; ret = InfoFileHnd;
}
return ret; return ret;
} }
void UpnpDisplayFileAndLine(FILE *fd, const char *DbgFileName, int DbgLineNo)
void UpnpDisplayFileAndLine(
FILE *fd,
const char *DbgFileName,
int DbgLineNo)
{ {
#define NLINES 2 #define NLINES 2
#define MAX_LINE_SIZE 512 #define MAX_LINE_SIZE 512
@ -205,10 +178,8 @@ void UpnpDisplayFileAndLine(
int i; int i;
/* Initialize the pointer array */ /* Initialize the pointer array */
for (i = 0; i < NLINES; i++) { for (i = 0; i < NLINES; i++)
lines[i] = buf[i]; lines[i] = buf[i];
}
/* Put the debug lines in the buffer */ /* Put the debug lines in the buffer */
sprintf(buf[0], "DEBUG - THREAD ID: 0x%lX", sprintf(buf[0], "DEBUG - THREAD ID: 0x%lX",
#ifdef WIN32 #ifdef WIN32
@ -216,24 +187,16 @@ void UpnpDisplayFileAndLine(
#else #else
(unsigned long int)ithread_self() (unsigned long int)ithread_self()
#endif #endif
); );
if (DbgFileName) { if (DbgFileName)
sprintf(buf[1], sprintf(buf[1], "FILE: %s, LINE: %d", DbgFileName, DbgLineNo);
"FILE: %s, LINE: %d",
DbgFileName,
DbgLineNo);
}
/* Show the lines centered */ /* Show the lines centered */
UpnpDisplayBanner(fd, lines, NLINES, NUMBER_OF_STARS); UpnpDisplayBanner(fd, lines, NLINES, NUMBER_OF_STARS);
fflush(fd); fflush(fd);
} }
void UpnpDisplayBanner( void UpnpDisplayBanner(FILE * fd,
FILE * fd, const char **lines, size_t size, size_t starLength)
const char **lines,
size_t size,
size_t starLength)
{ {
size_t leftMarginLength = starLength / 2 + 1; size_t leftMarginLength = starLength / 2 + 1;
size_t rightMarginLength = starLength / 2 + 1; size_t rightMarginLength = starLength / 2 + 1;
@ -263,11 +226,10 @@ void UpnpDisplayBanner(
line += starLengthMinus2; line += starLengthMinus2;
} }
leftMarginLength = (starLengthMinus2 - LineSize) / 2; leftMarginLength = (starLengthMinus2 - LineSize) / 2;
if (LineSize % 2 == 0) { if (LineSize % 2 == 0)
rightMarginLength = leftMarginLength; rightMarginLength = leftMarginLength;
} else { else
rightMarginLength = leftMarginLength + 1; rightMarginLength = leftMarginLength + 1;
}
memset(leftMargin, ' ', leftMarginLength); memset(leftMargin, ' ', leftMarginLength);
memset(rightMargin, ' ', rightMarginLength); memset(rightMargin, ' ', rightMarginLength);
leftMargin[leftMarginLength] = 0; leftMargin[leftMarginLength] = 0;
@ -282,15 +244,13 @@ void UpnpDisplayBanner(
free(leftMargin); free(leftMargin);
} }
void PrintThreadPoolStats( void PrintThreadPoolStats(ThreadPool *tp,
ThreadPool *tp, const char *DbgFileName,
const char *DbgFileName, int DbgLineNo, const char *msg)
int DbgLineNo,
const char *msg)
{ {
ThreadPoolStats stats; ThreadPoolStats stats;
ThreadPoolGetStats(tp, &stats); ThreadPoolGetStats(tp, &stats);
UpnpPrintf(UPNP_INFO, API, DbgFileName, DbgLineNo, UpnpPrintf(UPNP_INFO, API, DbgFileName, DbgLineNo,
"%s\n" "%s\n"
"High Jobs pending: %d\n" "High Jobs pending: %d\n"
"Med Jobs Pending: %d\n" "Med Jobs Pending: %d\n"
@ -321,6 +281,4 @@ void PrintThreadPoolStats(
stats.totalIdleTime); stats.totalIdleTime);
} }
#endif /* DEBUG */ #endif /* DEBUG */