home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / main / BBS / ODOORS62.ZIP / ODLog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-07  |  7.4 KB  |  258 lines

  1. /* OpenDoors Online Software Programming Toolkit
  2.  * (C) Copyright 1991 - 1999 by Brian Pirie.
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Lesser General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.
  8.  *
  9.  * This library is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.  * Lesser General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with this library; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  *
  18.  *
  19.  *        File: ODLog.c
  20.  *
  21.  * Description: Implements the logfile subsystem.
  22.  *
  23.  *   Revisions: Date          Ver   Who  Change
  24.  *              ---------------------------------------------------------------
  25.  *              Oct 13, 1994  6.00  BP   New file header format.
  26.  *              Dec 09, 1994  6.00  BP   Standardized coding style.
  27.  *              Aug 19, 1995  6.00  BP   32-bit portability.
  28.  *              Nov 16, 1995  6.00  BP   Removed oddoor.h, added odcore.h.
  29.  *              Dec 30, 1995  6.00  BP   Added ODCALL for calling convention.
  30.  *              Feb 19, 1996  6.00  BP   Changed version number to 6.00.
  31.  *              Mar 03, 1996  6.10  BP   Begin version 6.10.
  32.  */
  33.  
  34. #define BUILDING_OPENDOORS
  35.  
  36. #include <stdio.h>
  37. #include <time.h>
  38.  
  39. #include "OpenDoor.h"
  40. #include "ODCore.h"
  41. #include "ODGen.h"
  42. #include "ODInEx.h"
  43. #include "ODKrnl.h"
  44.  
  45.  
  46. /* Private logfile file handle */
  47. static FILE *logfile_pointer;
  48.  
  49.  
  50. /* Private helper functions. */
  51. static BOOL ODLogWriteStandardMsg(INT nLogfileMessage);
  52. static void ODLogClose(INT nReason);
  53.  
  54.  
  55. /* ----------------------------------------------------------------------------
  56.  * ODLogEnable()
  57.  *
  58.  * This function is called from od_init() when the user explicitly includes the
  59.  * OpenDoors logfile option using the od_control.od_logfile setting.
  60.  *
  61.  * Parameters: None.
  62.  *
  63.  *     Return: void
  64.  */
  65. ODAPIDEF void ODCALL ODLogEnable(void)
  66. {
  67.    /* At this time, this function simply maps to a call to od_log_open(). */
  68.    od_log_open();
  69. }
  70.  
  71.  
  72. /* ----------------------------------------------------------------------------
  73.  * od_log_open()
  74.  *
  75.  * Called to begin logfile operations. This is when the first message is
  76.  * written to the logfile, indicating that the user is entering OpenDoors.
  77.  *
  78.  * Parameters: None.
  79.  *
  80.  *     Return: TRUE on success, or FALSE on failure.
  81.  */
  82. ODAPIDEF BOOL ODCALL od_log_open()
  83. {
  84.    time_t nUnixTime;
  85.    struct tm *ptmTimeRecord;
  86.  
  87.    /* Log function entry if running in trace mode. */
  88.    TRACE(TRACE_API, "od_log_open()");
  89.  
  90.    /* Initialize OpenDoors if not already done. */
  91.    if(!bODInitialized) od_init();
  92.  
  93.    /* Don't open logfile if it has been disabled in config file, etc. */
  94.    if(od_control.od_logfile_disable) return(TRUE);
  95.  
  96.    /* Open actual logfile. */
  97.    if((logfile_pointer=fopen(od_control.od_logfile_name, "a")) == NULL)
  98.    {
  99.       return(FALSE);
  100.    }
  101.  
  102.    /* Get the current time. */
  103.    nUnixTime = time(NULL);
  104.    ptmTimeRecord = localtime(&nUnixTime);
  105.  
  106.    /* Print logfile tear line. */
  107.    fprintf(logfile_pointer, "\n----------  %s %02.2d %s %02.2d, %s\n",
  108.       od_control.od_day[ptmTimeRecord->tm_wday],
  109.       ptmTimeRecord->tm_mday,
  110.       od_control.od_month[ptmTimeRecord->tm_mon],
  111.       ptmTimeRecord->tm_year,
  112.       od_program_name);
  113.  
  114.    /* Print message of door start up. */
  115.    sprintf(szODWorkString, (char *)od_control.od_logfile_messages[11],
  116.       od_control.user_name);
  117.    od_log_write(szODWorkString);
  118.  
  119.    /* Set internal function hooks to enable calling of logfile features */
  120.    /* from elsewhere in OpenDoors. */
  121.    pfLogWrite = ODLogWriteStandardMsg;
  122.    pfLogClose = ODLogClose;
  123.  
  124.    return(TRUE);
  125. }
  126.  
  127.  
  128. /* ----------------------------------------------------------------------------
  129.  * ODLogWriteStandardMsg()                             *** PRIVATE FUNCTION ***
  130.  *
  131.  * Function called to write a standard message to the logfile.
  132.  *
  133.  * Parameters: nLogfileMessage   - Index of the standard message to write to
  134.  *                                 the logfile.
  135.  *
  136.  *     Return: TRUE on success, or FALSE on failure.
  137.  */
  138. static BOOL ODLogWriteStandardMsg(INT nLogfileMessage)
  139. {
  140.    if(nLogfileMessage < 0 || nLogfileMessage > 11)
  141.    {
  142.       return(FALSE);
  143.    }
  144.  
  145.    od_log_write((char *)od_control.od_logfile_messages[nLogfileMessage]);
  146.  
  147.    if(nLogfileMessage == 8)
  148.    {
  149.       sprintf(szODWorkString, od_control.od_logfile_messages[12],
  150.          od_control.user_reasonforchat);
  151.       szODWorkString[67] = '\0';
  152.       od_log_write(szODWorkString);
  153.    }
  154.  
  155.    return(TRUE);
  156. }
  157.  
  158.  
  159. /* ----------------------------------------------------------------------------
  160.  * od_log_write()
  161.  *
  162.  * Called to write a message to the logfile.
  163.  *
  164.  * Parameters: pszMessage  - Pointer to a string containing the message text.
  165.  *
  166.  *     Return: TRUE on success, or FALSE on failure.
  167.  */
  168. ODAPIDEF BOOL ODCALL od_log_write(char *pszMessage)
  169. {
  170.    char *pszFormat;
  171.    time_t nUnixTime;
  172.    struct tm *ptmTimeRecord;
  173.  
  174.    /* Verify that OpenDoors has been initialized. */
  175.    if(!bODInitialized) od_init();
  176.  
  177.    OD_API_ENTRY();
  178.  
  179.    /* Stop if logfile has been disabled in config file, etc. */
  180.    if(od_control.od_logfile_disable)
  181.    {
  182.       OD_API_EXIT();
  183.       return(TRUE);
  184.    }
  185.  
  186.    /* If logfile has not yet been opened, then open it. */
  187.    if(logfile_pointer==NULL)
  188.    {
  189.       if(!od_log_open())
  190.       {
  191.          OD_API_EXIT();
  192.          return(FALSE);
  193.       }
  194.    }
  195.  
  196.    /* Get the current system time. */
  197.    nUnixTime=time(NULL);
  198.    ptmTimeRecord=localtime(&nUnixTime);
  199.  
  200.    /* Determine which logfile format string to use. */
  201.    if(ptmTimeRecord->tm_hour<10)
  202.    {
  203.       pszFormat=(char *)">  %1.1d:%02.2d:%02.2d  %s\n";
  204.    }
  205.    else
  206.    {
  207.       pszFormat=(char *)"> %2.2d:%02.2d:%02.2d  %s\n";
  208.    }
  209.  
  210.    /* Write a line to the logfile. */
  211.    fprintf(logfile_pointer, pszFormat, ptmTimeRecord->tm_hour,
  212.       ptmTimeRecord->tm_min, ptmTimeRecord->tm_sec, pszMessage);
  213.  
  214.    OD_API_EXIT();
  215.    return(TRUE);
  216. }
  217.  
  218.  
  219. /* ----------------------------------------------------------------------------
  220.  * ODLogClose()                                        *** PRIVATE FUNCTION ***
  221.  *
  222.  * Writes final entry to the logfile when OpenDoors is exiting.
  223.  *
  224.  * Parameters: nReason  - Specifies the reason why OpenDoors is exiting.
  225.  *
  226.  *     Return: void
  227.  */
  228. static void ODLogClose(INT nReason)
  229. {
  230.    /* Stop if logfile has been disabled in the config file, etc. */
  231.    if(od_control.od_logfile_disable) return;
  232.  
  233.    /* If logfile has not been opened, then abort. */
  234.    if(logfile_pointer==NULL) return;
  235.  
  236.    if(bPreOrExit)
  237.    {
  238.       od_log_write((char *)od_control.od_logfile_messages[13]);
  239.    }
  240.    else if(btExitReason<=5 && btExitReason>=1)
  241.    {
  242.       od_log_write((char *)od_control.od_logfile_messages[btExitReason-1]);
  243.    }
  244.    else
  245.    {
  246.       sprintf(szODWorkString,(char *)od_control.od_logfile_messages[5],nReason);
  247.       od_log_write(szODWorkString);
  248.    }
  249.  
  250.    /* Close the logfile. */
  251.    fclose(logfile_pointer);
  252.  
  253.    /* Prevent further use of logfile without first re-opening it. */
  254.    pfLogWrite = NULL;
  255.    pfLogClose = NULL;
  256.    logfile_pointer = NULL;
  257. }
  258.