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