[libimdb-commit] CVS: libimdb/libjlog jlog.c,1.1,1.2 jlog2File.c,1.1,1.2
Status: Pre-Alpha
Brought to you by:
jveldhuis
|
From: Jerry V. <jve...@us...> - 2003-08-21 22:45:21
|
Update of /cvsroot/libimdb/libimdb/libjlog
In directory sc8-pr-cvs1:/tmp/cvs-serv6615
Modified Files:
jlog.c jlog2File.c
Log Message:
improved jlog interface and compile-time options
Index: jlog.c
===================================================================
RCS file: /cvsroot/libimdb/libimdb/libjlog/jlog.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** jlog.c 16 Jun 2003 04:18:38 -0000 1.1
--- jlog.c 21 Aug 2003 22:45:18 -0000 1.2
***************
*** 26,37 ****
typedef struct mymod_s { jlogModule_e mod; char *str; } mymod_t;
- struct jlogCallbackHandle_s {
- jlog_eventCallback_t *cb; /**< callback function pointer */
-
- void *userdata; /**< extra client data that gets handed back in callbacks */
-
- jlogReportLevel_e callbackLevel[MOD_ALL+1]; /**< one set of flags for each MODule */
- } jlogCallback_t;
-
jlogCallbackHandle_t *
jlogCallback_create(jlog_eventCallback_t *cb,
--- 26,29 ----
***************
*** 83,125 ****
/**
- * log handle.
- */
- struct jlog_s {
- int h_refcount; /**< reference count */
- int h_neverUnallocate; /**< do no unallocate log_s struct ptr in log_close */
- #if TRACK_OPEN_FROM_WHERE
- struct whereInSource_s **h_openFromWhere;
- #endif
-
- int cbCount;
- jlogCallbackHandle_t **cb;
-
- jlogModule_e h_lastMessageModule[4]; /**< last module with error, one per level */
- int h_lastMessageNumber[4]; /**< last messageNumber, one per level */
-
- int h_appendEnterExit[MOD_ALL+1];
- int h_appendEnterExitNestedLevel;
- };
-
- static struct jlog_s defaultlog = {
- 0,
- 1,
- #if TRACK_OPEN_FROM_WHERE
- NULL,
- #endif
- 0, /*cbCount*/
- NULL /*cb list */,
-
- {MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE},
- {0,0,0,0},
- {0,0,0}, /* h_appendEnterExit[MOD_ALL+1] */
- 0, /* h_appendEnterExitNestedLevel */
- };
-
-
- /**
* register an event callback.
*
- * if logh is NULL, the callback is registered with the default logh.
* returns -1 on malloc failure
*/
--- 75,80 ----
***************
*** 131,143 ****
int i, total;
- /* logh missing means set default callback */
- if ( logh == NULL ) {
- logh=&defaultlog;
- }
- else if ( logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
if ( logh->cbCount == 0 ) {
logh->cb=malloc(sizeof(jlogCallbackHandle_t *));
--- 86,89 ----
***************
*** 177,189 ****
int i;
- /* logh missing means set default callback */
- if ( logh == NULL ) {
- logh=&defaultlog;
- }
- else if ( logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
if ( logh->cbCount == 0 ) {
return(-1);
--- 123,126 ----
***************
*** 202,217 ****
}
- /**
- * reset values to be consistant with default log settings
- * reset all buffers etc
- * warning: returned jlogHandle_t has h_neverUnallocate set to 1.
- */
- static void
- jlog_resetToDefault(jlogHandle_t *logh)
- {
- memcpy(logh, &defaultlog, sizeof(struct jlog_s));
- jlog_reset(logh);
- }
-
static long opens_g=0; /* number of logh instances currently open */
static long opens_ever_g=0; /* number of logh instances ever opened */
--- 139,142 ----
***************
*** 223,236 ****
* log handle. If reuseh is not NULL, reference count of logh is
* incremented and returned.
-
- * @todo TRACK_OPEN_FROM_WHERE should be enabled in log.c - prelimary support
- * for tracking where log_openMain was called. Problem with implementation is
- * we only track reuse of handle in subsequent calls to log_openMain with a
- * counter, so being able to pop/push may or may not work. Will need to push
- * logh handle down into private struct pointer and replace logh handle with
- * something that just tracks the real logh the ref counter to get information
- * about which where it was called. Storing the location information in the
- * logh handle gives us an easy way to identify the openMain calls in order.
- * Probably a back pointer is easier to manage.
*
* @todo document LOG_DEBUG_LEVEL and LOG_DEBUG_FILE environment variables
--- 148,151 ----
***************
*** 254,258 ****
/* if this is the first ever log_open call, then intrept
! LOG_DEBUG_LEVEL for settings in defaultlog */
#if 0
should be moved to log2File or logTracker
--- 169,173 ----
/* if this is the first ever log_open call, then intrept
! LOG_DEBUG_LEVEL for settings */
#if 0
should be moved to log2File or logTracker
***************
*** 297,301 ****
};
! int fm;
/* skip to next - or + */
--- 212,216 ----
};
! jlogModule_e mod;
/* skip to next - or + */
***************
*** 303,312 ****
env++;
}
! for ( fm=0 ; fm<=MOD_ALL ; fm++ ) {
! if ( strncmp(env+1, mods[fm].str, strlen(mods[fm].str)) == 0 ) {
break;
}
}
! if ( fm == MOD_ALL+1 ) {
env++;
/* not found, so skip to next - or + */
--- 218,227 ----
env++;
}
! for (mod=MOD_NONE ; mod <= MOD_ALL ;mod++ ) {
! if ( strncmp(env+1, mods[mod].str, strlen(mods[mod].str)) == 0 ) {
break;
}
}
! if ( mod == MOD_ALL+1 ) {
env++;
/* not found, so skip to next - or + */
***************
*** 317,333 ****
else {
if ( *env == '-' ) {
! if ( mods[fm].mod == MOD_ALL ) {
jlog_setAllModuleDebugFileLevels(NULL, LOGT_WARNING);
}
else {
! if ( defaultlog.h_dbflags[ mods[fm].mod] == LOGT_DEBUG ) {
! jlog_setModuleDebugFileLevel(NULL, mods[fm].mod, LOGT_WARNING);
! }
}
}
else {
! jlog_setModuleDebugFileLevel(NULL, mods[fm].mod, LOGT_DEBUG);
}
! env+=strlen(mods[fm].str)+1;
found++;
}
--- 232,246 ----
else {
if ( *env == '-' ) {
! if ( mods[mod].mod == MOD_ALL ) {
jlog_setAllModuleDebugFileLevels(NULL, LOGT_WARNING);
}
else {
! jlog_setModuleDebugFileLevel(NULL, mods[mod].mod, LOGT_WARNING);
}
}
else {
! jlog_setModuleDebugFileLevel(NULL, mods[mod].mod, LOGT_DEBUG);
}
! env+=strlen(mods[mod].str)+1;
found++;
}
***************
*** 393,414 ****
if ( reuseh != NULL ) {
- #if TRACK_OPEN_FROM_WHERE
- struct whereInSource_s **n;
-
- n=realloc_openFromWhere(reuseh->h_openFromWhere, reuseh->h_refcount,
- file, function, lineno);
- if ( n == NULL ) {
- /* malloc failure */
- return(NULL);
- }
- free_openFromWhere(reuseh->h_openFromWhere, reuseh->h_refcount);
- #endif
reuseh->h_refcount++;
- #if TRACK_OPEN_FROM_WHERE
- reuseh->h_openFromWhere=n;
- #endif
opens_ever_g++;
-
#if 0
if ( logdebug_g && reuseh->h_dbflags[MOD_LOG] >= LOGT_DEBUG ) {
--- 306,312 ----
***************
*** 425,444 ****
opens_ever_parents_g++;
! logh=malloc(sizeof(struct jlog_s));
if ( logh==NULL ) {
return(NULL);
}
! jlog_resetToDefault(logh);
! logh->h_refcount=1;
! logh->h_neverUnallocate=0;
! #if TRACK_OPEN_FROM_WHERE
! logh->h_openFromWhere==realloc_openFromWhere(NULL, 0, file, function, lineno);
! if ( n == NULL ) {
! /* malloc failure */
! free(logh)
! return(NULL);
}
! #endif
opens_g++;
opens_ever_g++;
--- 323,346 ----
opens_ever_parents_g++;
! logh=malloc(sizeof(struct jlogHandle_s));
if ( logh==NULL ) {
return(NULL);
}
! else {
! jlogModule_e mod;
! logh->h_refcount=1;
! logh->cbCount=0;
! logh->cb=NULL;
!
! for (mod=MOD_NONE ; mod <= MOD_ALL ;mod++ ) {
! logh->h_lastMessageModule[mod]=MOD_NONE;
! logh->h_lastMessageNumber[mod]=0;
! logh->enterReturn_cb[IS_ENTER][mod]=NULL;
! logh->enterReturn_cb[IS_SUCCESS][mod]=NULL;
! logh->enterReturn_cb[IS_FAILURE][mod]=NULL;
! }
}
!
opens_g++;
opens_ever_g++;
***************
*** 505,512 ****
}
- if ( logh->h_neverUnallocate == 1 ) {
- return(0);
- }
-
#if 0
if ( logdebug_g && logh->h_dbflags[MOD_LOG] >= LOGT_DEBUG ) {
--- 407,410 ----
***************
*** 569,577 ****
jlog_getModuleShortNames(const jlogHandle_t *logh)
{
- if ( logh && logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
return(mymods);
}
--- 467,470 ----
***************
*** 585,593 ****
jlogReportLevel_e level)
{
- if ( logh && logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
if ( logh==NULL ||
(level!=JLOGT_CRITICAL &&
--- 478,481 ----
***************
*** 610,618 ****
jlogReportLevel_e level)
{
- if ( logh && logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
if ( logh==NULL ||
(level!=JLOGT_CRITICAL &&
--- 498,501 ----
***************
*** 627,639 ****
jlog_reset(jlogHandle_t *logh)
{
! int i;
! for (i=0; i<4; i++ ) {
! logh->h_lastMessageModule[i]=MOD_NONE;
! logh->h_lastMessageNumber[i]=0;
}
return(0);
}
int
jlog_setModuleDebugEnterExit(jlogHandle_t *logh,
--- 510,523 ----
jlog_reset(jlogHandle_t *logh)
{
! jlogModule_e mod;
! for (mod=MOD_NONE ; mod <= MOD_ALL ;mod++ ) {
! logh->h_lastMessageModule[mod]=MOD_NONE;
! logh->h_lastMessageNumber[mod]=0;
}
return(0);
}
+ /*
int
jlog_setModuleDebugEnterExit(jlogHandle_t *logh,
***************
*** 655,659 ****
logh->h_appendEnterExit[MOD_ALL]=on;
return(l);
! }
static int
--- 539,543 ----
logh->h_appendEnterExit[MOD_ALL]=on;
return(l);
! }*/
static int
***************
*** 703,726 ****
va_list ap)
{
- struct jlog_s mylogh_static;
- jlogHandle_t *mylogh;
char fmtstr[MAXJLOGMSGLEN+1];
- if ( logh == NULL ) {
- /* use local struct since we don't want the overhead or
- re-allocating a new logh handle every time, let alone want
- to deal with memory problems :) */
- jlog_resetToDefault(&mylogh_static);
- mylogh=&mylogh_static;
- }
- else if ( logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
- else {
- mylogh=logh;
- }
-
if ( messageNumber != -1 ) {
logh->h_lastMessageModule[jlogReportLevel]=module;
--- 587,592 ----
***************
*** 730,734 ****
/* catch NULL error case */
if ( fmt==NULL ) {
! localfmt(mylogh, JLOGT_ERROR, MOD_JLG, 1,
srcfile, srcfunc, srcline, "call to log_fmt with NULL fmt string");
return(-1);
--- 596,600 ----
/* catch NULL error case */
if ( fmt==NULL ) {
! localfmt(logh, JLOGT_ERROR, MOD_JLG, 1,
srcfile, srcfunc, srcline, "call to log_fmt with NULL fmt string");
return(-1);
***************
*** 736,745 ****
if ( ap == NULL ) {
! return(localfmt(mylogh, jlogReportLevel, module, messageNumber, srcfile, srcfunc, srcline, fmt));
}
if ( vsnprintf(fmtstr, MAXJLOGMSGLEN, fmt, ap) < 0 ) {
fmtstr[MAXJLOGMSGLEN-1]='\0';
}
! return(localfmt(mylogh, jlogReportLevel, module, messageNumber, srcfile, srcfunc, srcline, fmtstr));
}
--- 602,611 ----
if ( ap == NULL ) {
! return(localfmt(logh, jlogReportLevel, module, messageNumber, srcfile, srcfunc, srcline, fmt));
}
if ( vsnprintf(fmtstr, MAXJLOGMSGLEN, fmt, ap) < 0 ) {
fmtstr[MAXJLOGMSGLEN-1]='\0';
}
! return(localfmt(logh, jlogReportLevel, module, messageNumber, srcfile, srcfunc, srcline, fmtstr));
}
***************
*** 761,769 ****
int ret;
- if ( logh && logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
va_start(args, fmt);
ret=jlog_fmt_arglist(logh, jlogReportLevel, module, messageNumber,
--- 627,630 ----
***************
*** 773,819 ****
}
- /**
- * Log the contents of a file. Calling log_fmt with each line separately.
- */
- int
- jlog_fmtfile(jlogHandle_t *logh,
- jlogReportLevel_e jlogReportLevel,
- jlogModule_e module,
- int messageNumber,
- const char *srcfile,
- const char *srcfunc,
- int srcline,
- const char *file)
- {
- FILE *fp;
- char line[MAXJLOGMSGLEN];
-
- if ( logh && logh->h_refcount < 0 ) {
- fprintf(stderr,"log handle invalid\n");
- fflush(stderr);
- abort();
- }
- if ( (fp=fopen(file, "rb")) == NULL ) {
- return(-1);
- }
- while ( fgets(line, MAXJLOGMSGLEN, fp) != NULL ) {
- if ( line[strlen(line)-1] == '\n' ) {
- line[strlen(line)-1]='\0';
- }
- if ( jlog_fmt(logh, jlogReportLevel, module, messageNumber, srcfile, srcfunc, srcline, line)!= 0) {
- fclose(fp);
- return(-1);
- }
- }
- fclose(fp);
- return(0);
- }
-
- typedef enum {
- IS_ENTER,
- IS_SUCCESS,
- IS_FAILURE
- } enterReturnType_e;
-
/**
* routine for logging function enter operations.
--- 634,637 ----
***************
*** 821,825 ****
static int
jlog_enterReturn(jlogHandle_t *logh,
- jlogReportLevel_e jlogReportLevel,
jlogModule_e module,
enterReturnType_e type,
--- 639,642 ----
***************
*** 913,941 ****
}
}
- fmtstr[--fmtstr_len]='\0';
! if ( type == IS_ENTER ) {
! logh->h_appendEnterExitNestedLevel++;
! }
! if ( logh->h_appendEnterExitNestedLevel > 0 ) {
! char buf[20];
! sprintf(buf, "%%-%ds", logh->h_appendEnterExitNestedLevel*2);
! fprintf(stderr, buf, " ");
! }
! if ( type != IS_ENTER ) {
! logh->h_appendEnterExitNestedLevel--;
! }
! switch(type) {
! case IS_ENTER:
! fprintf(stderr, "jlog_enter:%s:%d: calling %s(%s)\n", srcfile, srcline, srcfunc, fmtstr);
! break;
! case IS_SUCCESS:
! fprintf(stderr, "jlog_success:%s:%d: from %s(rval=%s)\n", srcfile, srcline, srcfunc, fmtstr);
! break;
! case IS_FAILURE:
! fprintf(stderr, "jlog_failure:%s:%d: from %s(rval=%s)\n", srcfile, srcline, srcfunc, fmtstr);
! break;
}
return(1);
--- 730,764 ----
}
}
! /* if ( type == IS_ENTER ) { */
! /* logh->h_appendEnterExitNestedLevel++; */
! /* } */
! /* if ( logh->h_appendEnterExitNestedLevel > 0 ) { */
! /* char buf[20]; */
! /* sprintf(buf, "%%-%ds", logh->h_appendEnterExitNestedLevel*2); */
! /* fprintf(stderr, buf, " "); */
! /* } */
! /* if ( type != IS_ENTER ) { */
! /* logh->h_appendEnterExitNestedLevel--; */
! /* } */
! if ( fmtstr_len == 0 ) {
! if ( logh->enterReturn_cb[type][module] != NULL ) {
! logh->enterReturn_cb[type][module](logh, srcfile, srcfunc, srcline, module, type, "");
! }
! if ( logh->enterReturn_cb[type][MOD_ALL] != NULL ) {
! logh->enterReturn_cb[type][MOD_ALL](logh, srcfile, srcfunc, srcline, module, type, "");
! }
!
! }
! else {
! fmtstr[--fmtstr_len]='\0';
! if ( logh->enterReturn_cb[type][module] != NULL ) {
! logh->enterReturn_cb[type][module](logh, srcfile, srcfunc, srcline, module, type, fmtstr);
! }
! if ( logh->enterReturn_cb[type][MOD_ALL] != NULL ) {
! logh->enterReturn_cb[type][MOD_ALL](logh, srcfile, srcfunc, srcline, module, type, fmtstr);
! }
}
return(1);
***************
*** 944,948 ****
int
jlog_enter(jlogHandle_t *logh,
- jlogReportLevel_e jlogReportLevel,
jlogModule_e module,
const char *srcfile,
--- 767,770 ----
***************
*** 951,991 ****
...)
{
! struct jlog_s mylogh_static;
! jlogHandle_t *mylogh;
!
! if ( logh == NULL ) {
! /* use local struct since we don't want the overhead or
! re-allocating a new logh handle every time, let alone want
! to deal with memory problems :) */
! jlog_resetToDefault(&mylogh_static);
! mylogh=&mylogh_static;
! }
! else if ( logh->h_refcount < 0 ) {
! fprintf(stderr,"log handle invalid\n");
! fflush(stderr);
! abort();
! }
! else {
! mylogh=logh;
! }
! if ( mylogh->h_appendEnterExit[module] == 0 &&
! mylogh->h_appendEnterExit[MOD_ALL] == 0 ) {
! return(0);
! }
! else {
! va_list args;
! int ret;
! va_start(args, srcfunc);
! ret=jlog_enterReturn(mylogh, jlogReportLevel, module, IS_ENTER, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
! }
}
int
jlog_success(jlogHandle_t *logh,
- jlogReportLevel_e jlogReportLevel,
jlogModule_e module,
const char *srcfile,
--- 773,790 ----
...)
{
! va_list args;
! int ret;
! assert(logh != NULL);
! assert(logh->enterReturn_cb[IS_ENTER][module]!=NULL);
! va_start(args, srcfunc);
! ret=jlog_enterReturn(logh, module, IS_ENTER, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
}
int
jlog_success(jlogHandle_t *logh,
jlogModule_e module,
const char *srcfile,
***************
*** 994,1034 ****
...)
{
! struct jlog_s mylogh_static;
! jlogHandle_t *mylogh;
!
! if ( logh == NULL ) {
! /* use local struct since we don't want the overhead or
! re-allocating a new logh handle every time, let alone want
! to deal with memory problems :) */
! jlog_resetToDefault(&mylogh_static);
! mylogh=&mylogh_static;
! }
! else if ( logh->h_refcount < 0 ) {
! fprintf(stderr,"log handle invalid\n");
! fflush(stderr);
! abort();
! }
! else {
! mylogh=logh;
! }
! if ( mylogh->h_appendEnterExit[module] == 0 &&
! mylogh->h_appendEnterExit[MOD_ALL] == 0 ) {
! return(0);
! }
! else {
! va_list args;
! int ret;
! va_start(args, srcfunc);
! ret=jlog_enterReturn(mylogh, jlogReportLevel, module, IS_SUCCESS, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
! }
}
int
jlog_failure(jlogHandle_t *logh,
- jlogReportLevel_e jlogReportLevel,
jlogModule_e module,
const char *srcfile,
--- 793,810 ----
...)
{
! va_list args;
! int ret;
! assert(logh != NULL);
! assert(logh->enterReturn_cb[IS_SUCCESS][module]!=NULL);
! va_start(args, srcfunc);
! ret=jlog_enterReturn(logh, module, IS_SUCCESS, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
}
int
jlog_failure(jlogHandle_t *logh,
jlogModule_e module,
const char *srcfile,
***************
*** 1037,1072 ****
...)
{
! struct jlog_s mylogh_static;
! jlogHandle_t *mylogh;
!
! if ( logh == NULL ) {
! /* use local struct since we don't want the overhead or
! re-allocating a new logh handle every time, let alone want
! to deal with memory problems :) */
! jlog_resetToDefault(&mylogh_static);
! mylogh=&mylogh_static;
! }
! else if ( logh->h_refcount < 0 ) {
! fprintf(stderr,"log handle invalid\n");
! fflush(stderr);
! abort();
! }
! else {
! mylogh=logh;
! }
! if ( mylogh->h_appendEnterExit[module] == 0 &&
! mylogh->h_appendEnterExit[MOD_ALL] == 0 ) {
! return(0);
! }
! else {
! va_list args;
! int ret;
! va_start(args, srcfunc);
! ret=jlog_enterReturn(mylogh, jlogReportLevel, module, IS_FAILURE, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
! }
}
--- 813,826 ----
...)
{
! va_list args;
! int ret;
! assert(logh != NULL);
! assert(logh->enterReturn_cb[IS_FAILURE][module]!=NULL);
! va_start(args, srcfunc);
! ret=jlog_enterReturn(logh, module, IS_FAILURE, srcfile, srcline, srcfunc, args);
! va_end(args);
! return(ret);
}
Index: jlog2File.c
===================================================================
RCS file: /cvsroot/libimdb/libimdb/libjlog/jlog2File.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** jlog2File.c 16 Jun 2003 04:18:38 -0000 1.1
--- jlog2File.c 21 Aug 2003 22:45:18 -0000 1.2
***************
*** 22,26 ****
struct jlog2File_s {
! char filename[MAXPATHLEN];
/* for private use */
--- 22,26 ----
struct jlog2File_s {
! char *filename;
/* for private use */
***************
*** 109,114 ****
return(NULL);
}
! assert(strlen(filename) < MAXPATHLEN);
! strcpy(lt->filename, filename);
lt->tval_s=0;
lt->tim[0]='\0';
--- 109,117 ----
return(NULL);
}
! lt->filename=strdup(filename);
! if ( lt->filename == NULL ) {
! free(lt);
! return(NULL);
! }
lt->tval_s=0;
lt->tim[0]='\0';
***************
*** 119,122 ****
--- 122,126 ----
if ( jlog2File_enable(lt) != 0 ) {
+ free(lt->filename);
free(lt);
return(NULL);
***************
*** 130,133 ****
--- 134,138 ----
{
jlog2File_disable(lt);
+ free(lt->filename);
free(lt);
return(0);
|