home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / LOGGER.C < prev    next >
C/C++ Source or Header  |  1993-08-08  |  10KB  |  280 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.  *    $Id: logger.c 1.10 1993/08/08 17:39:09 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: logger.c $
  20.  *     Revision 1.10  1993/08/08  17:39:09  ahd
  21.  *     Denormalize path for opening on selected networks
  22.  *
  23.  *     Revision 1.9  1993/07/22  23:19:50  ahd
  24.  *     First pass for Robert Denny's Windows 3.x support changes
  25.  *
  26.  *     Revision 1.8  1993/06/06  15:04:05  ahd
  27.  *     Trap unable to open log file
  28.  *
  29.  *     Revision 1.7  1993/04/11  00:32:05  ahd
  30.  *     Global edits for year, TEXT, etc.
  31.  *
  32.  *     Revision 1.6  1993/03/06  22:48:23  ahd
  33.  *     Drop dashes between log entries
  34.  *
  35.  *     Revision 1.5  1993/01/23  19:08:09  ahd
  36.  *     Correct sleep.h include
  37.  *
  38.  * Revision 1.4  1992/11/23  03:56:06  ahd
  39.  * Do not use expand_path to build log file name
  40.  * Use strpool for names
  41.  *
  42.  * Revision 1.3  1992/11/22  20:58:55  ahd
  43.  * Move retry of opens to FOPEN()
  44.  *
  45.  * Revision 1.2  1992/11/19  02:58:22  ahd
  46.  * drop rcsid
  47.  *
  48.  * Revision 1.1  1992/11/16  05:00:26  ahd
  49.  * Initial revision
  50.  *
  51.  */
  52.  
  53. /*--------------------------------------------------------------------*/
  54. /*                   Standard library include files                   */
  55. /*--------------------------------------------------------------------*/
  56.  
  57. #include <stdio.h>
  58. #include <time.h>
  59. #include <sys/types.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <sys/stat.h>
  63. #include <share.h>
  64. #include <io.h>
  65.  
  66. /*--------------------------------------------------------------------*/
  67. /*                    UUPC/extended include files                     */
  68. /*--------------------------------------------------------------------*/
  69.  
  70. #include "lib.h"
  71. #include "dater.h"
  72. #include "expath.h"
  73. #include "logger.h"
  74. #include "hlib.h"
  75. #include "timestmp.h"
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*                      Define current file name                      */
  79. /*--------------------------------------------------------------------*/
  80.  
  81. currentfile();
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*                          Local variables                           */
  85. /*--------------------------------------------------------------------*/
  86.  
  87. static char *logname  = NULL;
  88. static char *tempname = NULL;
  89.  
  90. static void copylog( void );
  91.  
  92. /*--------------------------------------------------------------------*/
  93. /*    o p e n l o g                                                   */
  94. /*                                                                    */
  95. /*    Begin logging to a standard file name                           */
  96. /*--------------------------------------------------------------------*/
  97.  
  98. void openlog( const char *log )
  99. {
  100.    char fname[FILENAME_MAX];
  101.    FILE *stream = NULL;
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*                Create the final log name for later                 */
  105. /*--------------------------------------------------------------------*/
  106.  
  107.    logname =  (char*) ((log == NULL) ? compilen : log);
  108.    tempname = strchr( logname, '.');
  109.    mkfilename( fname, E_spooldir, logname );
  110.  
  111.    if ( tempname == NULL )
  112.       strcat( fname, ".LOG" );
  113.    logname = newstr( fname );
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*                   Create temporary log file name                   */
  117. /*--------------------------------------------------------------------*/
  118.  
  119.    if ( bflag[F_MULTITASK] )
  120.    {
  121.       char *savedir = E_tempdir;    /* Save real tempory directory   */
  122.       short retries = 15;
  123.  
  124.       E_tempdir = E_spooldir;       /* Create log file in spool dir
  125.                                        to allow for larger files
  126.                                        and/or system crashes         */
  127.       while (( stream == NULL ) && retries-- )
  128.       {
  129.          mktempname(fname, "LOG");  // Get a temp log file name
  130.  
  131.          denormalize( fname );
  132.          stream = _fsopen(fname, "at", SH_DENYWR);
  133.  
  134.          if ( stream == NULL )
  135.             printerr( fname );
  136.  
  137.       } /* while */
  138.  
  139.       E_tempdir = savedir;          // Restore true temp dir
  140.       tempname = newstr( fname );   // Save name we log to for posterity
  141.  
  142.    } /* if */
  143.    else {
  144.       tempname = logname;           /* Log directly to true log file */
  145.       stream  = FOPEN( tempname , "a",TEXT_MODE );
  146.                               /* We append in case we are not in
  147.                                  multitask mode and we do not want
  148.                                  to clobber the real log!            */
  149.    } /* else */
  150.  
  151.    if ( stream == NULL )
  152.    {
  153.       printmsg(0,"Cannot open any log file!");
  154.       panic();
  155.    }
  156.  
  157.    full_log_file_name = tempname;   /* Tell printmsg() what our log
  158.                                        file name is                  */
  159.    logfile  = stream;               // And of the the stream itself
  160.  
  161. /*--------------------------------------------------------------------*/
  162. /*               Request the copy function be run later               */
  163. /*--------------------------------------------------------------------*/
  164.  
  165.    atexit( copylog );
  166.  
  167. /*--------------------------------------------------------------------*/
  168. /*    Tag the new log file with the current time and program date.    */
  169. /*    We don't use printmsg() because that will not display the       */
  170. /*    time if debugging is turned up.                                 */
  171. /*--------------------------------------------------------------------*/
  172.  
  173.    fprintf(logfile,
  174.                "%s %s: %s %s (%s %s)\n",
  175.                dater( time( NULL ), NULL),
  176.                compilen, compilep, compilev, compiled, compilet);
  177.  
  178.    if ( ferror( logfile ))
  179.    {
  180.       printerr( tempname );
  181.       panic();
  182.    }
  183.  
  184. } /* openlog */
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*    c o p y l o g                                                   */
  188. /*                                                                    */
  189. /*    Close and copy a log opened by openlog                          */
  190. /*--------------------------------------------------------------------*/
  191.  
  192. static void copylog( void )
  193. {
  194.  
  195.    FILE *input;
  196.    FILE *output;
  197.    char buf[BUFSIZ];
  198.    int chars_read, chars_written;
  199.  
  200. /*--------------------------------------------------------------------*/
  201. /*   If not multitasking, just close the file and exit gracefully     */
  202. /*--------------------------------------------------------------------*/
  203.  
  204.    if ( !bflag[ F_MULTITASK ] )
  205.    {
  206.       fclose( logfile );
  207.       logfile = stdout;
  208.       return;
  209.    }
  210.  
  211. /*--------------------------------------------------------------------*/
  212. /*            We're multitasking; copy the file gracefully            */
  213. /*--------------------------------------------------------------------*/
  214.  
  215.    output = FOPEN( logname ,"a",TEXT_MODE);
  216.  
  217.    if ( output == NULL )
  218.    {
  219.       printmsg(0,"Cannot merge log %s to %s", tempname, logname );
  220.       printerr( logname );
  221.       fclose( logfile );
  222.       logfile = stderr;
  223.       return;
  224.    }
  225.  
  226.    fclose( logfile );
  227.    logfile = output;                /* Log directly into real file   */
  228.    full_log_file_name = logname;    /* Tell printerr we switched     */
  229.  
  230.    input = FOPEN( tempname, "r",TEXT_MODE );
  231.  
  232.    if ( input == NULL )
  233.    {
  234.       printerr( tempname );
  235.       fclose( input );
  236.       fclose( output );
  237.       logfile = stdout;
  238.    }
  239.  
  240. /*--------------------------------------------------------------------*/
  241. /*           File is open, copy temporary log to end of it            */
  242. /*--------------------------------------------------------------------*/
  243.  
  244.    while ((chars_read = fread(buf,sizeof(char), BUFSIZ, input)) != 0)
  245.    {
  246.       chars_written = fwrite(buf, sizeof(char), chars_read, output );
  247.  
  248.       if (chars_written != chars_read)
  249.       {
  250.          printerr( logname );
  251.          clearerr( output );
  252.          fclose( input );
  253.          fclose( output );
  254.          logfile  = stdout;
  255.          return;
  256.       }
  257.    } /* while */
  258.  
  259. /*--------------------------------------------------------------------*/
  260. /*                     Check for errors on input                      */
  261. /*--------------------------------------------------------------------*/
  262.  
  263.    if ( ferror( input ))
  264.    {
  265.       printerr( tempname );
  266.       clearerr( input );
  267.    }
  268.  
  269. /*--------------------------------------------------------------------*/
  270. /*             Close up shop and discard temporary input              */
  271. /*--------------------------------------------------------------------*/
  272.  
  273.    fclose( input );
  274.    fclose( output );
  275.    logfile  = stdout;
  276.  
  277.    unlink( tempname );
  278.  
  279. } /* copylog */
  280.