home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume10 / contool / part02 / logging.c < prev    next >
C/C++ Source or Header  |  1990-10-30  |  4KB  |  138 lines

  1. /************************************************************************/
  2. /*    Copyright 1988-1990 by Chuck Musciano and Harris Corporation    */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.  This     */
  15. /*    software may not be sold without the prior explicit permission    */
  16. /*    of Harris Corporation.                        */
  17. /************************************************************************/
  18.  
  19. /************************************************************************/
  20. /*                                    */
  21. /*    logging.c    message log management                */
  22. /*                                    */
  23. /************************************************************************/
  24.  
  25. #include    <stdio.h>
  26. #include    <sys/param.h>
  27. #include    <sys/types.h>
  28. #include    <xview/xview.h>
  29. #include    <xview/panel.h>
  30. #include    <xview/xv_xrect.h>
  31.  
  32. #include    "manifest.h"
  33. #include    "contool.h"
  34. #include    "contool_ui.h"
  35.  
  36. PUBLIC    Menu_item    start_logging();
  37. PUBLIC    Menu_item    stop_logging();
  38.  
  39. PUBLIC    contool_base_objects    *contool_base;
  40.  
  41. PRIVATE    int    logging = FALSE;
  42. PRIVATE    FILE    *logfile = NULL;
  43. PRIVATE    char    *log_path = NULL;
  44.  
  45. /************************************************************************/
  46. EXPORT    void    disable_logging()
  47.  
  48. {
  49.     if (logging) {
  50.        fclose(logfile);
  51.        cond_free(log_path);
  52.        log_path = NULL;
  53.        logging = FALSE;
  54.        }
  55.     xv_set(contool_base->base, FRAME_RIGHT_FOOTER, "", NULL);
  56. }
  57.  
  58. /************************************************************************/
  59. EXPORT    void    enable_logging()
  60.  
  61. {    char    buf[1024];
  62.  
  63.     if (logging) {
  64.        if (log_path && strcmp(log_path, defaults.log_file) == 0)
  65.           return;
  66.        disable_logging();
  67.        }
  68.     if (is_empty(defaults.log_file))
  69.        error("You must specify a log file in the Properties dialog");
  70.     else if ((logfile = fopen(defaults.log_file, "a")) == NULL)
  71.        error("Cannot open log file %s : %s", defaults.log_file, sys_errlist[errno]);
  72.     else {
  73.        logging = TRUE;
  74.        log_path = strsave(defaults.log_file);
  75.        sprintf(buf, "Logging to %s...", defaults.log_file);
  76.        xv_set(contool_base->base, FRAME_RIGHT_FOOTER, buf, NULL);
  77.        }
  78. }
  79.  
  80. /************************************************************************/
  81. EXPORT    Menu_item    start_logging(item, op)
  82.  
  83. Menu_item    item;
  84. Menu_generate    op;
  85.  
  86. {    contool_base_objects    *ip = (contool_base_objects *) xv_get(item, XV_KEY_DATA, INSTANCE);
  87.  
  88.     if (op == MENU_DISPLAY)
  89.        xv_set(item, MENU_INACTIVE, logging, NULL);
  90.     else if (op == MENU_NOTIFY)
  91.        enable_logging();
  92.     return item;
  93. }
  94.  
  95. /************************************************************************/
  96. EXPORT    Menu_item    stop_logging(item, op)
  97.  
  98. Menu_item    item;
  99. Menu_generate    op;
  100.  
  101. {    contool_base_objects    *ip = (contool_base_objects *) xv_get(item, XV_KEY_DATA, INSTANCE);
  102.     
  103.     if (op == MENU_DISPLAY)
  104.        xv_set(item, MENU_INACTIVE, !logging, NULL);
  105.     else if (op == MENU_NOTIFY)
  106.        disable_logging();
  107.     return item;
  108. }
  109.  
  110. /************************************************************************/
  111. EXPORT    update_logging()
  112.  
  113. {
  114.     if (logging) {
  115.        disable_logging();
  116.        enable_logging();
  117.        }
  118. }
  119.  
  120. /************************************************************************/
  121. EXPORT    write_log(s)
  122.  
  123. char    *s;
  124.  
  125. {    int    t;
  126.     static    char    hostname[100] = "";
  127.  
  128.     if (logging) {
  129.        if (*hostname == NULL) 
  130.           if (gethostname(hostname, 99) != 0)
  131.              strcpy(hostname, "(unknown)");
  132.        t = time(0);
  133.        fseek(logfile, 0L, 2);
  134.        fprintf(logfile, "%s\t%.16s\t%s", hostname, ctime(&t) + 4, s);
  135.        fflush(logfile);
  136.        }
  137. }
  138.