From e08ddec31ed92563abd92bd18f7b4d9008eda7cf Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Thu, 27 Mar 2025 14:34:42 -0400 Subject: [PATCH] Merge log file is working fine --- app/feeder.c | 72 +++++++++++++++++++++++++++++++++++++--------------- lib/devlog.c | 62 ++++++++++++++++++++++++++++++++++---------- lib/devsoc.c | 14 +++++++--- lib/gestcp.c | 8 +++--- lib/lvleml.c | 5 ++-- lib/modrec.c | 7 +++-- lib/subrou.c | 2 +- lib/uniprc.c | 6 ++--- lib/unisig.c | 2 ++ 9 files changed, 124 insertions(+), 54 deletions(-) diff --git a/app/feeder.c b/app/feeder.c index 8936909..06287c2 100644 --- a/app/feeder.c +++ b/app/feeder.c @@ -93,7 +93,7 @@ return status; /* Scanning data file. */ /* */ /********************************************************/ -static _Bool scanfile(SOCPTR *socptr,const char *filename) +static _Bool scanonefile(SOCPTR *socptr,const char *filename) { _Bool status; @@ -110,6 +110,7 @@ while (proceed==true) { //(void) fprintf(stdout,"JMPDBG scanfile phase='%d'\n",phase); switch (phase) { case 0 : //checking parameters + (void) fprintf(stdout,"Scanning file <%s>\n",filename); if ((fichier=fopen(filename,"r"))==(FILE *)0) { (void) fprintf(stdout,"Unable to open file <%s> (error=<%s>\n", filename,strerror(errno)); @@ -117,7 +118,6 @@ while (proceed==true) { } break; case 1 : //reading line; - (void) fprintf(stdout,"Scanning file <%s>\n",filename); while (fgets(line,sizeof(line),fichier)!=(char *)0) { if (scanline(socptr,line)==false) { phase=999; //Trouble trouble exiting @@ -137,6 +137,51 @@ while (proceed==true) { } return status; } +/********************************************************/ +/* */ +/* procedure to scan all files */ +/* */ +/********************************************************/ +static int scanallfiles(int argc,char *argv[]) + +{ +int numfile; +SOCPTR *socptr; +int next; +int phase; + +numfile=0; +socptr=(SOCPTR *)0; +next=2; +phase=0; +while (nextargv[0],params->argv[1]); - if (socptr==(SOCPTR *)0) { - (void) fprintf(stdout,"Unable to contact remote!\n"); - phase=999; //can not go further - } - break; - case 3 : //doing main task - for (int i=2;iargc;i++) { - if (scanfile(socptr,params->argv[i])==false) - break; - numfile++; - } - (void) fprintf(stdout,"%d file successfully transmetted to <%s.%s>\n", - numfile,params->argv[0],params->argv[1]); - break; - case 4 : //closing remote channel - socptr=soc_closeonesock(socptr); + numfile=scanallfiles(params->argc,params->argv); + if (numfile==(params->argc-2)) + (void) fprintf(stdout,"%d file successfully transmitted to <%s.%s>\n", + numfile,params->argv[0],params->argv[1]); break; default : //end of task params=par_freeparams(params); diff --git a/lib/devlog.c b/lib/devlog.c index 8b1115a..dcbaa32 100644 --- a/lib/devlog.c +++ b/lib/devlog.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "subrou.h" #include "uniprc.h" @@ -60,14 +61,18 @@ static _Bool mergelog(const char *logname) #define EVENT "logevent" _Bool status; -time_t curtime; char *event; -char daily[200]; +FILE *fevent; +FILE *flog; +time_t curtime; char asctemps[100]; +char daily[200]; int phase; int proceed; status=true; +fevent=(FILE *)0; +flog=(FILE *)0; curtime=time((time_t)0); (void) strftime(asctemps,sizeof(asctemps),UFTIME,localtime(&curtime)); event=(char *)0; @@ -75,15 +80,21 @@ event=(char *)0; phase=0; proceed=true; while (proceed==true) { - (void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //do we have a log name? if (logname==(char *)0) { (void) rou_alert(0,"%s logname is missing (Bug!?)",OPEP); - phase=999; + proceed=false; //trouble trouble } break; - case 1 : //duplicat logname + case 1 : //locking access to event file + if (prc_locking(EVENT,true,5)==false) { + (void) rou_alert(0,"%s Unable to log event (Timing!?)",OPEP); + proceed=false; //trouble trouble + } + break; + case 2 : //duplicat logname (void) strncpy(daily,logname,sizeof(daily)); if (strlen(daily)>0) { char *ptr; @@ -93,17 +104,39 @@ while (proceed==true) { } (void) rou_asprintf(&event,"%s/event-%s.jrl",daily,asctemps); break; - case 2 : //locking access to event file - if (prc_locking(EVENT,true,5)==false) { - (void) rou_alert(0,"%s Unable to log event (Timing!?)",OPEP); - phase=999; //Trouble trouble + case 3 : //opening log file + if ((flog=fopen(logname,"r"))==(FILE *)0) { + (void) rou_alert(0,"%s Unable to open log file <%s> (error=<%s>)", + OPEP,logname,strerror(errno)); + phase=999; //no need to go further } break; - case 3 : //merging file - (void) rou_alert(0,"%s JMPDBG merging file=<%s>",OPEP,event); - (void) prc_locking(EVENT,false,1); + case 4 : //opening log file + if ((fevent=fopen(event,"a+"))==(FILE *)0) { + (void) rou_alert(0,"%s Unable to open event file <%s> (error=<%s>)", + OPEP,event,strerror(errno)); + (void) fclose(flog); + phase=999; //no need to go further + } + break; + case 5 : //merging file + if (fevent!=(FILE *)0) { //always + char ch; + + while ((ch=fgetc(flog))!=EOF) + fputc(ch,fevent); + } + (void) fclose(fevent); + (void) fclose(flog); + break; + case 6 : //removing log file + if (unlink(logname)<0) { + (void) rou_alert(0,"%s Unable to delete log file <%s> (error=<%s>)", + OPEP,logname,strerror(errno)); + } break; default : //SAFE guard + (void) prc_locking(EVENT,false,1); event=rou_freestr(event); proceed=false; break; @@ -141,7 +174,7 @@ if (inlog==true) phase=0; proceed=true; while (proceed==true) { - (void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //do we have a log name? if (logname==(char *)0) { @@ -168,6 +201,7 @@ while (proceed==true) { break; case 3 : //writing a minimal data (void) clock_gettime(CLOCK_REALTIME,&(log->start)); + (void) fprintf(log->file,"\n#-----------------------------\n"); (void) fprintf(log->file,"#%s",ctime(&(log->start.tv_sec))); (void) fflush(log->file); log->start.tv_sec=0; @@ -203,7 +237,7 @@ log=(LOGTYP *)logptr; phase=0; proceed=true; while (proceed==true) { - (void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //do we have a log reference? if (log==(LOGTYP *)0) diff --git a/lib/devsoc.c b/lib/devsoc.c index 66faf38..fc54b7d 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -997,8 +997,15 @@ if (soc!=(SOCTYP *)0) { break; } if (sent<0) { - (void) rou_alert(0,"%s Unable to send data, error=%d <%s> (Bug)", - OPEP,errno,strerror(errno)); + switch (errno) { + case EPIPE : + (void) rou_alert(0,"%s broken pipe",OPEP); + break; + default : + (void) rou_alert(0,"%s Unable to send data, error=%d <%s> (Bug)", + OPEP,errno,strerror(errno)); + break; + } } } return sent; @@ -1043,9 +1050,10 @@ if (soc!=(SOCTYP *)0) { errno=EAGAIN; switch (errno) { case EAGAIN : //no char available + (void) rou_alert(0,"%s JMPDBG NO char avail",OPEP); break; case ECONNRESET : //Connection reset by peer - soc->connected=false; + (void) rou_alert(0,"%s JMPDBG connection reset by peer",OPEP); break; default : (void) rou_alert(0,"%s Unexpected error=%d <%s> (Bug)", diff --git a/lib/gestcp.c b/lib/gestcp.c index dbf30cb..b70cec7 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -42,7 +42,7 @@ got=0; phase=0; proceed=true; while (proceed==true) { - (void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : if (socptr==(SOCPTR *)0) { @@ -53,19 +53,17 @@ while (proceed==true) { case 1 : //get nextline if ((got=soc_getnextline(socptr,lineptr))>0) phase=999; //we got a line. - (void) rou_alert(0,"JMPDBG %s phase='%d' got='%d'",OPEP,phase,got); break; case 2 : //waiting for new character presence got=soc_waitforchar(socptr,attend); - (void) rou_alert(0,"JMPDBG %s phase='%d' got='%d'",OPEP,phase,got); switch (got) { case -1 : //trouble? signal? if ((hangup==true)||(reload==true)) phase=999; //we got a real signal break; //no need to read line case 0 : //normal time out - phase=999; //no need to go further - break; //no need to read line + phase=999; //no need to go further, no need to read line + break; default : //char available phase=0; //check for new line if (soc_receive(socptr)<0) { diff --git a/lib/lvleml.c b/lib/lvleml.c index d42d59d..f5c506a 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -311,7 +311,7 @@ _Bool proceed; status=1; delay=300; //5 minutes standard delay -if (debug>5) +if (debug>1) delay/=5; //one minute in debug mode proceed=true; (void) signon(contact); @@ -325,7 +325,6 @@ while (proceed==true) { //(void) rou_alert(0,"%s, JMPDBG tcp_getline status='%d'",OPEP,status); if (status<=0) { //timeout or trouble? (void) log_fprintlog(contact->logptr,false,"%s","Lost contact with remote"); - (void) rou_alert(0,"%s, JMPDBG lost contact",OPEP); break; //no need to go further } (void) log_fprintlog(contact->logptr,false,"%s",line); @@ -401,7 +400,7 @@ contact=(CONTYP *)0; phase=0; proceed=true; while (proceed==true) { - (void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); + //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase){ case 0 : //check for binding if (socptr==(SOCPTR *)0) { diff --git a/lib/modrec.c b/lib/modrec.c index caa05a0..fe52337 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -137,14 +137,13 @@ while (proceed==true) { case 2 : //do contact switch (eml_docontact(contact)) { case -1 : //Signal received - (void) rou_alert(0,"JMPDBG got signal within contact"); + (void) rou_alert(0,"Signal received within contact"); break; case 0 : //exit under timeout - (void) rou_alert(0,"Contact with peer <%s> closed by timeout", - contact->peerip); + (void) rou_alert(0,"Lost contact with peer <%s>",contact->peerip); break; default : //exit under quit - (void) rou_alert(0,"JMPDBG exit under quit"); + (void) rou_alert(0,"Contact terminated by 'quit'"); break; } break; diff --git a/lib/subrou.c b/lib/subrou.c index 41e07b9..1ccf2eb 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -21,7 +21,7 @@ //version definition #define VERSION "0.6" -#define RELEASE "19" +#define RELEASE "20" //Public variables PUBLIC int debug=0; //debug level diff --git a/lib/uniprc.c b/lib/uniprc.c index 311aec9..e8d334c 100644 --- a/lib/uniprc.c +++ b/lib/uniprc.c @@ -256,9 +256,9 @@ while (proceed==true) { (void) fgets(strloc,sizeof(strloc)-1,fichier); (void) fclose(fichier); if (sscanf(strloc,"%lu",(u_long *)(&pid))==1) { - (void) rou_alert(2,"Locking, check %d process active",pid); + (void) rou_alert(5,"Locking, check %d process active",pid); if (prc_checkprocess(pid)==false) { - (void) rou_alert(2,"Locking, removing pid=%d unactive lock",pid); + (void) rou_alert(6,"Locking, removing pid=%d unactive lock",pid); (void) unlink(fullname); } else { @@ -273,7 +273,7 @@ while (proceed==true) { break; case 5 : //do we need to unlock ? if (lock==LCK_UNLOCK) { - (void) rou_alert(9,"%s Request unlocking <%s>",OPEP,fullname); + (void) rou_alert(6,"%s Request unlocking <%s>",OPEP,fullname); (void) unlink(fullname); done=true; phase=999; //No need to go further diff --git a/lib/unisig.c b/lib/unisig.c index 18eec15..69960c1 100644 --- a/lib/unisig.c +++ b/lib/unisig.c @@ -116,6 +116,8 @@ switch (sig) (void) rou_alert(0,"%s deamon, new decreased debug level now set to '%d'", APPNAME,debug); break; + case SIGPIPE : + break; default : (void) rou_alert(0,"Unexpected Signal [%d]/<%s> received", sig,strsignal(sig)); -- 2.47.3