1 #include "mrilib.h"
2
3 #include "debugtrace.h"
4
5 #include <sys/file.h>
6
7 /******************************************************************************
8 LOCK_file (FILE *) returns 0 if OK to proceed, -1 if file is already locked
9 UNLOCK_file(FILE *) return value is ignored
10 *******************************************************************************/
11
12 #if defined(USE_FLOCK)
13 # define LOCK_file(fp) flock(fileno(fp),LOCK_EX|LOCK_NB)
14 # define UNLOCK_file(fp) flock(fileno(fp),LOCK_UN)
15 #elif defined(USE_LOCKF)
16 # define LOCK_file(fp) lockf(fileno(fp),F_TLOCK,0)
17 # define UNLOCK_file(fp) lockf(fileno(fp),F_ULOCK,0)
18 #else
19 # define LOCK_file(fp) 0
20 # define UNLOCK_file(fp) 0
21 #endif
22
23 #undef LOGFILE
24 #define LOGFILE ".afni.log"
25
26 /*------------------------------------------------------------------*/
27
AFNI_sleep(int msec)28 void AFNI_sleep( int msec )
29 {
30 struct timeval tv ;
31 if( msec <= 0 ) return ;
32 tv.tv_sec = msec/1000 ;
33 tv.tv_usec = (msec%1000)*1000 ;
34 select( 1 , NULL,NULL,NULL , &tv ) ;
35 return ;
36 }
37
38 /*---------------------------------------------------------------------
39 Log command to a file; return -1 if fails, 0 if good -- 13 Aug 2001
40 -----------------------------------------------------------------------*/
41
AFNI_logger(char * pname,int argc,char ** argv)42 int AFNI_logger( char *pname , int argc , char **argv )
43 {
44 char *cline, *cdate , *eh , *fn , *logfile=LOGFILE , *eee ;
45 FILE *fp ;
46 int ll ; unsigned long fll ;
47
48 if( pname == NULL || pname[0] == '\0' ) return -1 ;
49 eh = getenv("HOME") ; if( eh == NULL ) return -1 ;
50 if( AFNI_yesenv("AFNI_DONT_LOGFILE") ) return -1 ;
51 if( argc > 1 ) cline = tross_commandline( pname , argc , argv ) ;
52 else cline = strdup(pname) ;
53 if( cline == NULL ) return -1 ;
54
55 #ifdef USE_TRACING
56 DBG_commandline = strdup(cline) ; /* 10 Mar 2016 */
57 #endif
58
59 cdate = tross_datetime() ;
60 fn = AFMALL(char, strlen(eh)+strlen(logfile)+8) ;
61 strcpy(fn,eh) ; strcat(fn,"/") ; strcat(fn,logfile) ;
62 if( (fll=THD_filesize(fn)) > 100000000 ) /* 01 Jun 2005: for Kevin */
63 fprintf(stderr,"++ WARNING: file %s is now %lu (%s) bytes long!\n",
64 fn, fll, approximate_number_string((double)fll) ) ;
65 fp = fopen(fn,"a") ;
66 if( fp == NULL ){ free(fn); free(cdate); free(cline); return -1; }
67 ll = LOCK_file(fp) ;
68 if( ll ){
69 #if 0
70 fprintf(stderr,"%s: LOCKED\n",cline);
71 #endif
72 ll = strlen(pname) ; if( ll > 11 ) ll = 11 ;
73 AFNI_sleep(ll) ; ll = LOCK_file(fp) ;
74 if( ll ){ fclose(fp); free(fn); free(cdate); free(cline); return -1; }
75 }
76 fseek(fp,0,SEEK_END) ;
77 fprintf(fp,"[%s] %s\n",cdate,cline) ;
78 UNLOCK_file(fp) ; fclose(fp) ;
79 eee = getenv("AFNI_ECHO_COMMANDLINE") ;
80 if( eee != NULL && (*eee == 'Y' || *eee == 'y') ) /* 13 Dec 2010 */
81 fprintf(stderr,"+++++===== %s\n",cline) ;
82 free(fn); free(cdate); free(cline) ; return 0;
83 }
84
85 /*-------------------------------------------------------------------------*/
86
AFNI_logfilesize(void)87 long long AFNI_logfilesize(void)
88 {
89 char *eh , *fn ; long long fs ;
90
91 eh = getenv("HOME") ; if( eh == NULL ) return 0 ;
92 fn = (char *)malloc(sizeof(char)*(strlen(eh)+strlen(LOGFILE)+8)) ;
93 strcpy(fn,eh) ; strcat(fn,"/") ; strcat(fn,LOGFILE) ;
94 fs = THD_filesize(fn); free((void *)fn); return fs;
95 }
96