Simplified debug.
git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@51 e3e1d417-86f3-4887-817a-d78f3d33393fpull/27/merge
parent
174f046d08
commit
f382047643
225
src/zmdbg.c
225
src/zmdbg.c
|
@ -13,13 +13,6 @@
|
|||
|
||||
#define PRESERVE_ATTEMPTS 3
|
||||
|
||||
static unsigned int DbgArchive_ext = 1;
|
||||
static unsigned int DbgArchive_time = 0;
|
||||
static unsigned int DbgArchive_size = 0;
|
||||
|
||||
static unsigned long DbgArchive_sec = 0;
|
||||
static unsigned long DbgArchive_cnt = 0;
|
||||
|
||||
static char string[4096];
|
||||
static char dbg_string[4096+512];
|
||||
static const char *dbg_file;
|
||||
|
@ -39,11 +32,7 @@ int dbg_runtime = FALSE;
|
|||
int dbg_add_log_id = FALSE;
|
||||
struct timeval dbg_start;
|
||||
|
||||
/* support for automatic reopening of debug files on failure to open */
|
||||
static int dbg_running = FALSE;
|
||||
static int dbg_reopen_cnt = 10;
|
||||
static time_t dbg_log_closed = 0;
|
||||
static int dbg_trig_arc = FALSE;
|
||||
|
||||
void UsrHandler( int sig )
|
||||
{
|
||||
|
@ -54,12 +43,6 @@ void UsrHandler( int sig )
|
|||
{
|
||||
dbg_level++;
|
||||
}
|
||||
#if 0
|
||||
if ( SIGNAL( SIGUSR1,UsrHandler ) < 0 )
|
||||
{
|
||||
Error(( "%s(), error = %s", dbg_policy_nm[dbg_sig_policy], strerror(errno) ));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else if ( sig == SIGUSR2 )
|
||||
{
|
||||
|
@ -67,12 +50,6 @@ void UsrHandler( int sig )
|
|||
{
|
||||
dbg_level--;
|
||||
}
|
||||
#if 0
|
||||
if ( SIGNAL( SIGUSR2,UsrHandler ) < 0 )
|
||||
{
|
||||
Error(( "%s(), error = %s", dbg_policy_nm[dbg_sig_policy], strerror(errno) ));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Info(( "Debug Level Changed to %d", dbg_level ));
|
||||
}
|
||||
|
@ -85,7 +62,7 @@ int GetDebugEnv( const char * const command )
|
|||
/* dbg_level = 0; */
|
||||
/* dbg_log[0] = '\0'; */
|
||||
|
||||
env_ptr = getenv( "DB_PRINT" );
|
||||
env_ptr = getenv( "DBG_PRINT" );
|
||||
if ( env_ptr == (char *)NULL )
|
||||
{
|
||||
dbg_print = FALSE;
|
||||
|
@ -95,7 +72,7 @@ int GetDebugEnv( const char * const command )
|
|||
dbg_print = atoi( env_ptr );
|
||||
}
|
||||
|
||||
env_ptr = getenv( "DB_FLUSH" );
|
||||
env_ptr = getenv( "DBG_FLUSH" );
|
||||
if ( env_ptr == (char *)NULL )
|
||||
{
|
||||
dbg_flush = FALSE;
|
||||
|
@ -105,7 +82,7 @@ int GetDebugEnv( const char * const command )
|
|||
dbg_flush = atoi( env_ptr );
|
||||
}
|
||||
|
||||
env_ptr = getenv( "DB_RUNTIME" );
|
||||
env_ptr = getenv( "DBG_RUNTIME" );
|
||||
if ( env_ptr == (char *)NULL )
|
||||
{
|
||||
dbg_runtime = FALSE;
|
||||
|
@ -143,47 +120,10 @@ int GetDebugEnv( const char * const command )
|
|||
sprintf( dbg_log, "%s.%05d", env_ptr, getpid() );
|
||||
}
|
||||
}
|
||||
env_ptr = getenv( "DB_REOPEN_TRY" );
|
||||
if ( env_ptr != (char *) NULL )
|
||||
{
|
||||
/* This counts the number of times the DbgOutput function
|
||||
* has been called when the debug logs have been closed
|
||||
* before a reopen is attempted
|
||||
*/
|
||||
dbg_reopen_cnt = atoi(env_ptr);
|
||||
if (dbg_reopen_cnt < 1)
|
||||
{
|
||||
dbg_reopen_cnt = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
char *move_debug_file ( void )
|
||||
{
|
||||
int i;
|
||||
static char new_dbg_log[200];
|
||||
|
||||
for ( i=0; i<PRESERVE_ATTEMPTS; i++ )
|
||||
{
|
||||
FILE *fd = (FILE *)NULL;
|
||||
|
||||
sprintf ( new_dbg_log, "%s.%d", dbg_log, DbgArchive_ext );
|
||||
DbgArchive_ext ++;
|
||||
|
||||
if ( ( fd = fopen ( new_dbg_log, "r" ) ) == NULL )
|
||||
{
|
||||
fclose ( fd );
|
||||
rename ( dbg_log, new_dbg_log );
|
||||
return ( new_dbg_log );
|
||||
}
|
||||
}
|
||||
return ( NULL );
|
||||
}
|
||||
|
||||
|
||||
int InitialiseDebug()
|
||||
{
|
||||
char *prev_file = (char*)NULL;
|
||||
|
@ -212,10 +152,6 @@ int InitialiseDebug()
|
|||
return(DBG_ERROR);
|
||||
}
|
||||
|
||||
/* If we have purposely set the last character in the log name to a
|
||||
* tilda, rename it .....old and get ready for new debug file.
|
||||
*/
|
||||
|
||||
if ( ( dbg_add_log_id == FALSE && dbg_log[0] ) && ( dbg_log[strlen(dbg_log)-1] == '~' ) )
|
||||
{
|
||||
dbg_log[strlen(dbg_log)-1] = '\0';
|
||||
|
@ -229,20 +165,6 @@ int InitialiseDebug()
|
|||
fclose(tmp_fp); /* should maybe fclose() before rename() ? */
|
||||
}
|
||||
}
|
||||
if ( DbgArchive_time || DbgArchive_size )
|
||||
{
|
||||
/* if either of the archive parameters are set then try to archive an
|
||||
* existing log file
|
||||
*/
|
||||
DbgArchive_sec = dbg_start.tv_sec; /* time of last archive is set to now */
|
||||
DbgArchive_cnt = 0; /* running file size is set to zero */
|
||||
|
||||
if ( ( tmp_fp = fopen ( dbg_log, "r" ) ) != NULL )
|
||||
{
|
||||
fclose ( tmp_fp );
|
||||
prev_file = move_debug_file ( );
|
||||
}
|
||||
}
|
||||
|
||||
if( dbg_log[0] && (dbg_log_fd = fopen(dbg_log,"w")) == (FILE *)NULL )
|
||||
{
|
||||
|
@ -276,7 +198,6 @@ int DbgInit()
|
|||
return((InitialiseDebug() == DBG_OK ? 0 : 1));
|
||||
}
|
||||
|
||||
|
||||
int TerminateDebug()
|
||||
{
|
||||
Debug(1,("Terminating Debug"));
|
||||
|
@ -312,75 +233,6 @@ void DbgSubtractTime( struct timeval * const tp1, struct timeval * const tp2 )
|
|||
}
|
||||
}
|
||||
|
||||
static int DbgArchiveNowLocal( const unsigned long secs )
|
||||
{
|
||||
DbgArchive_sec = secs;
|
||||
DbgArchive_cnt = 0;
|
||||
|
||||
fflush ( dbg_log_fd );
|
||||
if ( dbg_log_fd != NULL && ( fclose ( dbg_log_fd ) ) == -1 )
|
||||
{
|
||||
Error ( ( "Failed to archive: fclose(), error = %s", strerror ( errno ) ) );
|
||||
/* clearing dbg_log_fd to make sure that we don't confused about
|
||||
* this debug file */
|
||||
if (dbg_log_closed == 0)
|
||||
{
|
||||
dbg_log_closed = time(NULL);
|
||||
}
|
||||
dbg_log_fd = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
char *prev_file;
|
||||
|
||||
dbg_log_fd = (FILE *)NULL;
|
||||
|
||||
prev_file = move_debug_file ( );
|
||||
|
||||
if ( prev_file == (char*)NULL )
|
||||
{
|
||||
if ( ( dbg_log_fd = fopen ( dbg_log, "a" ) ) == (FILE *)NULL )
|
||||
{
|
||||
if (dbg_log_closed == 0)
|
||||
{
|
||||
dbg_log_closed = time(NULL);
|
||||
}
|
||||
return ( DBG_ERROR );
|
||||
}
|
||||
Error ( ( "Debug Log archive failed" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ( dbg_log_fd = fopen ( dbg_log, "w" ) ) == (FILE *)NULL )
|
||||
{
|
||||
if (dbg_log_closed == 0)
|
||||
{
|
||||
dbg_log_closed = time(NULL);
|
||||
}
|
||||
return ( DBG_ERROR );
|
||||
}
|
||||
}
|
||||
|
||||
Info(("Debug Level = %d, Debug Log = %s",dbg_level,dbg_log));
|
||||
}
|
||||
|
||||
return ( DBG_OK );
|
||||
}
|
||||
|
||||
int DbgArchiveNow ( void )
|
||||
{
|
||||
struct timeval tp;
|
||||
struct timezone tzp;
|
||||
int rtn;
|
||||
|
||||
gettimeofday( &tp, &tzp );
|
||||
|
||||
rtn = DbgArchiveNowLocal ( tp.tv_sec );
|
||||
Info(("Reopening of debug logs under applications control"));
|
||||
|
||||
return ( rtn );
|
||||
}
|
||||
|
||||
int DbgPrepare( const char * const file, const int line, const int code )
|
||||
{
|
||||
dbg_file = file;
|
||||
|
@ -429,22 +281,6 @@ int DbgOutput( const char *fstring, ... )
|
|||
|
||||
gettimeofday( &tp, &tzp );
|
||||
|
||||
/* Archive processing */
|
||||
#if 0
|
||||
if ( ( DbgArchive_time && ( ( tp.tv_sec - DbgArchive_sec ) > DbgArchive_time ) ) ||
|
||||
( DbgArchive_size && ( DbgArchive_cnt > DbgArchive_size ) ) )
|
||||
{
|
||||
int rtn;
|
||||
|
||||
if ( ( rtn = DbgArchiveNowLocal ( tp.tv_sec ) ) != DBG_OK )
|
||||
{
|
||||
return ( rtn );
|
||||
}
|
||||
Info(("Reopening of debug triggered by debug library (MaxOpenTime=%d,MaxSize=%d", DbgArchive_time, DbgArchive_size));
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
if ( dbg_runtime )
|
||||
{
|
||||
DbgSubtractTime( &tp, &dbg_start );
|
||||
|
@ -471,60 +307,13 @@ int DbgOutput( const char *fstring, ... )
|
|||
}
|
||||
if ( dbg_log_fd != (FILE *)NULL )
|
||||
{
|
||||
int cnt;
|
||||
|
||||
/*
|
||||
* Attempt to seek to the end of the file.
|
||||
* This will add about 2 micro seconds to every
|
||||
* debug call. This is not considerd significant.
|
||||
* Note that if it fails it fails and we don't care
|
||||
* it's only to be nice to fstidy.
|
||||
*/
|
||||
lseek(fileno(dbg_log_fd), 0, 2);
|
||||
|
||||
if ( ( cnt = fprintf ( dbg_log_fd, "%s", dbg_string ) ) > 0 )
|
||||
{
|
||||
DbgArchive_cnt += cnt;
|
||||
}
|
||||
fprintf( dbg_log_fd, "%s", dbg_string );
|
||||
|
||||
if ( dbg_flush )
|
||||
{
|
||||
fflush(dbg_log_fd);
|
||||
}
|
||||
}
|
||||
else if (dbg_log_fd == NULL && dbg_running == TRUE)
|
||||
{
|
||||
/* in this section, debug is on because dbg_running (i.e.
|
||||
* InitialiseDebug has been called and TerminateDebug has not),
|
||||
* dbg_log_fd is NULL, probably from a failed close, so
|
||||
* what we do is increment the count of times into DbgOutput
|
||||
* function. Once this is greater than the dbg_reopen_cnt
|
||||
* we try to archive (this should hopefully tidy things up a
|
||||
* little, but even if it doesn't it should try reopening
|
||||
* the debug log. Once it is reopened we spit out an
|
||||
* extra message for information indicating the time the
|
||||
* debug log got lost. Note that if we can't archive (say
|
||||
* filesystem is still full) we just return as we would have
|
||||
* done earlier. Also note that in the case where we've
|
||||
* entered this function greater than dbg_reopen_cnt we
|
||||
* reset the counter.
|
||||
*/
|
||||
#if 0
|
||||
count++;
|
||||
if (count > dbg_reopen_cnt)
|
||||
{
|
||||
int rtn;
|
||||
|
||||
count = 0;
|
||||
if ( ( rtn = DbgArchiveNowLocal ( tp.tv_sec ) ) != DBG_OK )
|
||||
{
|
||||
return ( rtn );
|
||||
}
|
||||
Info(("Warning debug log closed since UTC=%d", dbg_log_closed));
|
||||
dbg_log_closed = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
/* For Info, Warning, Errors etc we want to log them */
|
||||
if ( dbg_code <= DBG_INF )
|
||||
{
|
||||
|
@ -560,9 +349,3 @@ int DbgOutput( const char *fstring, ... )
|
|||
}
|
||||
return( strlen( string ) );
|
||||
}
|
||||
|
||||
void DbgArchive ( const unsigned int ArchiveTime, const unsigned int ArchiveSize )
|
||||
{
|
||||
DbgArchive_time = ArchiveTime;
|
||||
DbgArchive_size = ArchiveSize;
|
||||
}
|
||||
|
|
|
@ -79,15 +79,11 @@ extern "C" {
|
|||
int DbgInit(void);
|
||||
int DbgTerm(void);
|
||||
int DbgPrepare(const char * const file,const int line, const int code);
|
||||
int DbgArchiveNow ( void );
|
||||
void DbgArchive ( const unsigned int ArchiveTime, const unsigned int ArchiveSize );
|
||||
int DbgOutput(const char *fstring, ... ) __attribute__ ((format(printf, 1, 2)));
|
||||
#else
|
||||
int DbgInit();
|
||||
int DbgTerm();
|
||||
int DbgPrepare();
|
||||
DbgArchiveNow();
|
||||
DbgArchive();
|
||||
int DbgOutput();
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue