Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

afni_logger.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 
00003 #include <sys/file.h>
00004 
00005 /******************************************************************************
00006   LOCK_file  (FILE *) returns 0 if OK to proceed, -1 if file is already locked
00007   UNLOCK_file(FILE *) return value is ignored
00008 *******************************************************************************/
00009 
00010 #if defined(USE_FLOCK)
00011 #  define LOCK_file(fp)   flock(fileno(fp),LOCK_EX|LOCK_NB)
00012 #  define UNLOCK_file(fp) flock(fileno(fp),LOCK_UN)
00013 #elif defined(USE_LOCKF)
00014 #  define LOCK_file(fp)   lockf(fileno(fp),F_TLOCK,0)
00015 #  define UNLOCK_file(fp) lockf(fileno(fp),F_ULOCK,0)
00016 #else
00017 #  define LOCK_file(fp)   0
00018 #  define UNLOCK_file(fp) 0
00019 #endif
00020 
00021 #undef  LOGFILE
00022 #define LOGFILE ".afni.log"
00023 
00024 /*------------------------------------------------------------------*/
00025 
00026 void AFNI_sleep( int msec )
00027 {
00028    struct timeval tv ;
00029    if( msec <= 0 ) return ;
00030    tv.tv_sec  = msec/1000 ;
00031    tv.tv_usec = (msec%1000)*1000 ;
00032    select( 1 , NULL,NULL,NULL , &tv ) ;
00033    return ;
00034 }
00035 
00036 /*---------------------------------------------------------------------
00037   Log command to a file; return -1 if fails, 0 if good -- 13 Aug 2001
00038 -----------------------------------------------------------------------*/
00039 
00040 int AFNI_logger( char * pname , int argc , char ** argv )
00041 {
00042    char *cline, *cdate , *eh , *fn , *logfile=LOGFILE ;
00043    FILE *fp ;
00044    int ll ; unsigned long fll ;
00045 
00046    if( pname == NULL || pname[0] == '\0' ) return -1 ;
00047    eh = getenv("HOME") ; if( eh == NULL )  return -1 ;
00048    if( AFNI_yesenv("AFNI_DONT_LOGFILE") )  return -1 ;
00049    if( argc > 1 ) cline = tross_commandline( pname , argc , argv ) ;
00050    else           cline = strdup(pname) ;
00051    if( cline == NULL ) return -1 ;
00052    cdate = tross_datetime() ;
00053    fn = AFMALL(char,  strlen(eh)+strlen(logfile)+8) ;
00054    strcpy(fn,eh) ; strcat(fn,"/") ; strcat(fn,logfile) ;
00055    if( (fll=THD_filesize(fn)) > 100000000 )   /* 01 Jun 2005: for Kevin */
00056      fprintf(stderr,"++ WARNING: file %s is now %lu bytes long!\n",fn,fll) ;
00057    fp = fopen(fn,"a") ;
00058    if( fp == NULL ){ free(fn); free(cdate); free(cline); return -1; }
00059    ll = LOCK_file(fp) ;
00060    if( ll ){
00061 #if 0
00062      fprintf(stderr,"%s: LOCKED\n",cline);
00063 #endif
00064      ll = strlen(pname) ; if( ll > 11 ) ll = 11 ;
00065      AFNI_sleep(ll) ; ll = LOCK_file(fp) ;
00066      if( ll ){ fclose(fp); free(fn); free(cdate); free(cline); return -1; }
00067    }
00068    fseek(fp,0,SEEK_END) ;
00069    fprintf(fp,"[%s] %s\n",cdate,cline) ;
00070    UNLOCK_file(fp) ; fclose(fp) ;
00071    free(fn); free(cdate); free(cline) ; return 0;
00072 }
 

Powered by Plone

This site conforms to the following standards: