[Gnupos-cvs] gnp-server/src GNPS_io.c,1.2,1.3 main.c,1.3,1.4
Status: Pre-Alpha
Brought to you by:
conrad_c
From: <con...@us...> - 2003-02-21 12:33:57
|
Update of /cvsroot/gnupos/gnp-server/src In directory sc8-pr-cvs1:/tmp/cvs-serv18245/src Modified Files: GNPS_io.c main.c Log Message: Tidy up logging stuff - now works properly. Index: GNPS_io.c =================================================================== RCS file: /cvsroot/gnupos/gnp-server/src/GNPS_io.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** GNPS_io.c 20 Feb 2003 12:31:27 -0000 1.2 --- GNPS_io.c 21 Feb 2003 12:33:54 -0000 1.3 *************** *** 62,70 **** { printf("ERROR: Could not open log file. ABORTING.\n"); ! exit(-1); } logprintf("GnuPOS Server starting. Version: %s",gnps_global_data.version_str); if(gnps_debug) printf("Leaving gnps_init_logging()\n"); --- 62,76 ---- { printf("ERROR: Could not open log file. ABORTING.\n"); ! gnps_exit(GNPS_ERR_LOGOPEN); } + if(gnps_verbose) + printf("Log File \"%s\" Open.\n",gnps_global_data.log_file_name); + logprintf("GnuPOS Server starting. Version: %s",gnps_global_data.version_str); + if(gnps_global_data.verbose_logging) + logprintf("- Verbose Logging enabled.\n") + if(gnps_debug) printf("Leaving gnps_init_logging()\n"); *************** *** 77,80 **** --- 83,87 ---- struct tm *tm_p; va_list vp; + int i; if(gnps_debug) *************** *** 82,99 **** /* get the time */ ! tt_p = time(tt_p); tm_p = gmtime(&tt_p); ! fprintf(gnps_log_fh,"%02d/%02d/%4d %02d:%02d:%02d UTC - ", ! tm_p->tm_mday, tm_p->tm_mon+1, tm_p->tm_year+1900, tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec); va_start(vp, template); ! vfprintf(gnps_log_fh, template, vp); va_end(vp); ! fprintf(gnps_log_fh,"\n"); if(gnps_debug) printf("Leaving logprintf()\n"); } --- 89,155 ---- /* get the time */ ! tt_p = time(&tt_p); tm_p = gmtime(&tt_p); ! i = fprintf(gnps_log_fh,"%02d/%02d/%4d %02d:%02d:%02d UTC - ", ! tm_p->tm_mday, (tm_p->tm_mon)+1, (tm_p->tm_year)+1900, tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec); + if (i < 0) + { + printf("FATAL ERROR: Error writing to log file.\n"); + gnps_exit(GNPS_ERR_LOGWRITE); + } va_start(vp, template); ! ! i = vfprintf(gnps_log_fh, template, vp); ! if (i < 0) ! { ! printf("FATAL ERROR: Error writing to log file.\n"); ! gnps_exit(GNPS_ERR_LOGWRITE); ! } ! va_end(vp); ! i = fprintf(gnps_log_fh,"\n"); ! if (i < 0) ! { ! printf("FATAL ERROR: Error writing to log file.\n"); ! gnps_exit(GNPS_ERR_LOGWRITE); ! } if(gnps_debug) printf("Leaving logprintf()\n"); + } + + void + gnps_log_close_err() + { + /* Close the Log file, with no error checking. Error handling use only! */ + if(gnps_debug) + printf("Entering gnps_log_close_err()\n"); + + if(gnps_log_fh != NULL) + fclose(gnps_log_fh); + + if(gnps_debug) + printf("Leaving gnps_log_close_err()\n"); + } + + void + gnps_log_close() + { + /* Close the log file nicely, including sign-off message in log. */ + + if(gnps_debug) + printf("Entering gnps_log_close()\n"); + + logprintf("GnuPOS exiting normally."); + fclose(gnps_log_fh); + + if(gnps_verbose) + printf("Log file closed.\n"); + + if(gnps_debug) + printf("Leaving gnps_log_close()\n"); } Index: main.c =================================================================== RCS file: /cvsroot/gnupos/gnp-server/src/main.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** main.c 20 Feb 2003 12:31:27 -0000 1.3 --- main.c 21 Feb 2003 12:33:54 -0000 1.4 *************** *** 57,60 **** --- 57,66 ---- printf ("Thank you for using GnuPOS. Have a nice day.\n"); + printf ("Closing down now.\n"); + + /* Insert shut-down calls here. */ + + /* This must be last: log file */ + gnps_log_close(); } *************** *** 123,124 **** --- 129,180 ---- } + void + gnps_exit(int err_code) + { + /* Provide a unified point for exiting. Allows us to close down everything */ + /* that needs closing and generally exit in a relatively nice way. */ + /* If err_code = 0 : There is no error. Exit normally. */ + /* If err_code < 0 : There is an error, flag it as appropriate. */ + /* */ + /* So, why do I use a static table here? Well, its very possible that the */ + /* error we're about to report is actually an out-of-memory error, and we */ + /* don't want our error reporting code to need to allocate memory. This */ + /* seemed like the best solution at the time. */ + + int i=0; + + struct + { + int code; + char *message; + } err_struct[] = + { + GNPS_ERR_UNKNOWN, "Unknown Error. This is bad.", + GNPS_ERR_LOGOPEN, "Cannot open log file.", + GNPS_ERR_LOGWRITE, "Cannot write to log file.", + GNPS_ERR_NOMEMORY, "Out of Memory!", + GNPS_999, "" + }; + + /* INSERT SHUT-DOWN CALLS HERE */ + + /* This call should be last: logfile */ + gnps_log_close_err(); + + while (err_struct[i].code!=999) + { + if(err_code == err_struct[i].code) + { + printf("FATAL ERROR: %s\n",err_struct[i].message); + exit(err_code); + } + else + i++; + } + + /* If we get here, someone is using an undocumented error code! */ + printf("FATAL ERROR!\n"); + printf("But I cannot tell you what it was, because I don't recognise code %d\n", + err_code); + exit(err_code); + } |