home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / LOGGER.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  9KB  |  249 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    l o g g e r . c                                                 */
  3. /*                                                                    */
  4. /*    Logging functions for UUPC/extended                             */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992 by Kendra Electronic Wonderworks; all        */
  7. /*    rights reserved except those explicitly granted by the          */
  8. /*    UUPC/extended license.                                          */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*                          RCS Information                           */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*
  16.  *    $Header: E:\SRC\UUPC\LIB\RCS\LOGGER.C 1.4 1992/11/23 03:56:06 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: LOGGER.C $
  20.  * Revision 1.4  1992/11/23  03:56:06  ahd
  21.  * Do not use expand_path to build log file name
  22.  * Use strpool for names
  23.  *
  24.  * Revision 1.3  1992/11/22  20:58:55  ahd
  25.  * Move retry of opens to FOPEN()
  26.  *
  27.  * Revision 1.2  1992/11/19  02:58:22  ahd
  28.  * drop rcsid
  29.  *
  30.  * Revision 1.1  1992/11/16  05:00:26  ahd
  31.  * Initial revision
  32.  *
  33.  */
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*                   Standard library include files                   */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. #include <stdio.h>
  40. #include <time.h>
  41. #include <sys/types.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ssleep.h>
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    UUPC/extended include files                     */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include "lib.h"
  51. #include "dater.h"
  52. #include "expath.h"
  53. #include "logger.h"
  54. #include "hlib.h"
  55. #include "timestmp.h"
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                      Define current file name                      */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. currentfile();
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                          Local variables                           */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. static char *logname  = NULL;
  68. static char *tempname = NULL;
  69.  
  70. static void copylog( void );
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*    o p e n l o g                                                   */
  74. /*                                                                    */
  75. /*    Begin logging to a standard file name                           */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. void openlog( const char *log )
  79. {
  80.    char fname[FILENAME_MAX];
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*                Create the final log name for later                 */
  84. /*--------------------------------------------------------------------*/
  85.  
  86.    logname =  (char*) ((log == NULL) ? compilen : log);
  87.    tempname = strchr( logname, '.');
  88.    mkfilename( fname, E_spooldir, logname );
  89.  
  90.    if ( tempname == NULL )
  91.       strcat( fname, ".LOG" );
  92.    logname = newstr( fname );
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*                   Create temporary log file name                   */
  96. /*--------------------------------------------------------------------*/
  97.  
  98.    if ( bflag[F_MULTITASK] )
  99.    {
  100.       char *savedir = E_tempdir;    /* Save real tempory directory   */
  101.  
  102.       E_tempdir = E_spooldir;       /* Create log file in spool dir
  103.                                        to allow for larger files
  104.                                        and/or system crashes         */
  105.       tempname = newstr( mktempname(fname, "LOG"));
  106.                                     /* Get the file name             */
  107.       E_tempdir = savedir;          /* Restore true temp dir         */
  108.    } /* if */
  109.    else
  110.       tempname = logname;           /* Log directly to true log file */
  111.  
  112.    full_log_file_name = tempname;   /* Tell printmsg() what our log
  113.                                        file name is                  */
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*                    Open the temporary log file                     */
  117. /*--------------------------------------------------------------------*/
  118.  
  119.    logfile = FOPEN( tempname , "a", TEXT );
  120.                               /* We append in case we are not in
  121.                                  multitask mode and we do not want
  122.                                  to clobber the real log!            */
  123.  
  124.    if ( logfile == NULL )
  125.    {
  126.       printerr( tempname );
  127.       panic();
  128.    }
  129.  
  130. /*--------------------------------------------------------------------*/
  131. /*               Request the copy function be run later               */
  132. /*--------------------------------------------------------------------*/
  133.  
  134.    atexit( copylog );
  135.  
  136. /*--------------------------------------------------------------------*/
  137. /*    Tag the new log file with the current time and program date.    */
  138. /*    We don't use printmsg() because that will not display the       */
  139. /*    time if debugging is turned up.                                 */
  140. /*--------------------------------------------------------------------*/
  141.  
  142.    fprintf(logfile,
  143.                "-----------\n%s %s: %s %s (%s %s)\n",
  144.                dater( time( NULL ), NULL),
  145.                compilen, compilep, compilev, compiled, compilet);
  146.  
  147.    if ( ferror( logfile ))
  148.    {
  149.       printerr( tempname );
  150.       panic();
  151.    }
  152.  
  153. } /* openlog */
  154.  
  155. /*--------------------------------------------------------------------*/
  156. /*    c o p y l o g                                                   */
  157. /*                                                                    */
  158. /*    Close and copy a log opened by openlog                          */
  159. /*--------------------------------------------------------------------*/
  160.  
  161. static void copylog( void )
  162. {
  163.  
  164.    FILE *input;
  165.    FILE *output;
  166.    char buf[BUFSIZ];
  167.    int chars_read, chars_written;
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*   If not multitasking, just close the file and exit gracefully     */
  171. /*--------------------------------------------------------------------*/
  172.  
  173.    if ( !bflag[ F_MULTITASK ] )
  174.    {
  175.       fclose( logfile );
  176.       logfile = stdout;
  177.       return;
  178.    }
  179.  
  180. /*--------------------------------------------------------------------*/
  181. /*            We're multitasking; copy the file gracefully            */
  182. /*--------------------------------------------------------------------*/
  183.  
  184.    output = FOPEN( logname ,"a", TEXT);
  185.  
  186.    if ( output == NULL )
  187.    {
  188.       printmsg(0,"Cannot merge log %s to %s", tempname, logname );
  189.       printerr( logname );
  190.       fclose( logfile );
  191.       logfile = stderr;
  192.       return;
  193.    }
  194.  
  195.    fclose( logfile );
  196.    logfile = output;                /* Log directly into real file   */
  197.    full_log_file_name = logname;    /* Tell printerr we switched     */
  198.  
  199.    input = FOPEN( tempname, "r", TEXT );
  200.  
  201.    if ( input == NULL )
  202.    {
  203.       printerr( tempname );
  204.       fclose( input );
  205.       fclose( output );
  206.       logfile = stdout;
  207.    }
  208.  
  209. /*--------------------------------------------------------------------*/
  210. /*           File is open, copy temporary log to end of it            */
  211. /*--------------------------------------------------------------------*/
  212.  
  213.    while ((chars_read = fread(buf,sizeof(char), BUFSIZ, input)) != 0)
  214.    {
  215.       chars_written = fwrite(buf, sizeof(char), chars_read, output );
  216.  
  217.       if (chars_written != chars_read)
  218.       {
  219.          printerr( logname );
  220.          clearerr( output );
  221.          fclose( input );
  222.          fclose( output );
  223.          logfile  = stdout;
  224.          return;
  225.       }
  226.    } /* while */
  227.  
  228. /*--------------------------------------------------------------------*/
  229. /*                     Check for errors on input                      */
  230. /*--------------------------------------------------------------------*/
  231.  
  232.    if ( ferror( input ))
  233.    {
  234.       printerr( tempname );
  235.       clearerr( input );
  236.    }
  237.  
  238. /*--------------------------------------------------------------------*/
  239. /*             Close up shop and discard temporary input              */
  240. /*--------------------------------------------------------------------*/
  241.  
  242.    fclose( input );
  243.    fclose( output );
  244.    logfile  = stdout;
  245.  
  246.    unlink( tempname );
  247.  
  248. } /* copylog */
  249.