ODLog.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * Aug 10, 2003 6.23 SH *nix support
  33. */
  34. #define BUILDING_OPENDOORS
  35. #include <stdio.h>
  36. #include <time.h>
  37. #include "OpenDoor.h"
  38. #include "ODCore.h"
  39. #include "ODGen.h"
  40. #include "ODInEx.h"
  41. #include "ODKrnl.h"
  42. /* Private logfile file handle */
  43. static FILE *logfile_pointer;
  44. /* Private helper functions. */
  45. static BOOL ODLogWriteStandardMsg(INT nLogfileMessage);
  46. static void ODLogClose(INT nReason);
  47. /* ----------------------------------------------------------------------------
  48. * ODLogEnable()
  49. *
  50. * This function is called from od_init() when the user explicitly includes the
  51. * OpenDoors logfile option using the od_control.od_logfile setting.
  52. *
  53. * Parameters: None.
  54. *
  55. * Return: void
  56. */
  57. ODAPIDEF void ODCALL ODLogEnable(void)
  58. {
  59. /* At this time, this function simply maps to a call to od_log_open(). */
  60. od_log_open();
  61. }
  62. /* ----------------------------------------------------------------------------
  63. * od_log_open()
  64. *
  65. * Called to begin logfile operations. This is when the first message is
  66. * written to the logfile, indicating that the user is entering OpenDoors.
  67. *
  68. * Parameters: None.
  69. *
  70. * Return: TRUE on success, or FALSE on failure.
  71. */
  72. ODAPIDEF BOOL ODCALL od_log_open()
  73. {
  74. time_t nUnixTime;
  75. struct tm *ptmTimeRecord;
  76. /* Log function entry if running in trace mode. */
  77. TRACE(TRACE_API, "od_log_open()");
  78. /* Initialize OpenDoors if not already done. */
  79. if(!bODInitialized) od_init();
  80. /* Don't open logfile if it has been disabled in config file, etc. */
  81. if(od_control.od_logfile_disable) return(TRUE);
  82. /* Open actual logfile. */
  83. if((logfile_pointer=fopen(od_control.od_logfile_name, "a")) == NULL)
  84. {
  85. return(FALSE);
  86. }
  87. /* Get the current time. */
  88. nUnixTime = time(NULL);
  89. ptmTimeRecord = localtime(&nUnixTime);
  90. /* Print logfile tear line. */
  91. fprintf(logfile_pointer, "\n---------- %s %02d %s %02d, %s\n",
  92. od_control.od_day[ptmTimeRecord->tm_wday],
  93. ptmTimeRecord->tm_mday,
  94. od_control.od_month[ptmTimeRecord->tm_mon],
  95. ptmTimeRecord->tm_year,
  96. od_program_name);
  97. /* Print message of door start up. */
  98. sprintf(szODWorkString, (char *)od_control.od_logfile_messages[11],
  99. od_control.user_name);
  100. od_log_write(szODWorkString);
  101. /* Set internal function hooks to enable calling of logfile features */
  102. /* from elsewhere in OpenDoors. */
  103. pfLogWrite = ODLogWriteStandardMsg;
  104. pfLogClose = ODLogClose;
  105. return(TRUE);
  106. }
  107. /* ----------------------------------------------------------------------------
  108. * ODLogWriteStandardMsg() *** PRIVATE FUNCTION ***
  109. *
  110. * Function called to write a standard message to the logfile.
  111. *
  112. * Parameters: nLogfileMessage - Index of the standard message to write to
  113. * the logfile.
  114. *
  115. * Return: TRUE on success, or FALSE on failure.
  116. */
  117. static BOOL ODLogWriteStandardMsg(INT nLogfileMessage)
  118. {
  119. if(nLogfileMessage < 0 || nLogfileMessage > 11)
  120. {
  121. return(FALSE);
  122. }
  123. od_log_write((char *)od_control.od_logfile_messages[nLogfileMessage]);
  124. if(nLogfileMessage == 8)
  125. {
  126. sprintf(szODWorkString, od_control.od_logfile_messages[12],
  127. od_control.user_reasonforchat);
  128. szODWorkString[67] = '\0';
  129. od_log_write(szODWorkString);
  130. }
  131. return(TRUE);
  132. }
  133. /* ----------------------------------------------------------------------------
  134. * od_log_write()
  135. *
  136. * Called to write a message to the logfile.
  137. *
  138. * Parameters: pszMessage - Pointer to a string containing the message text.
  139. *
  140. * Return: TRUE on success, or FALSE on failure.
  141. */
  142. ODAPIDEF BOOL ODCALL od_log_write(char *pszMessage)
  143. {
  144. char *pszFormat;
  145. time_t nUnixTime;
  146. struct tm *ptmTimeRecord;
  147. /* Verify that OpenDoors has been initialized. */
  148. if(!bODInitialized) od_init();
  149. OD_API_ENTRY();
  150. /* Stop if logfile has been disabled in config file, etc. */
  151. if(od_control.od_logfile_disable)
  152. {
  153. OD_API_EXIT();
  154. return(TRUE);
  155. }
  156. /* If logfile has not yet been opened, then open it. */
  157. if(logfile_pointer==NULL)
  158. {
  159. if(!od_log_open())
  160. {
  161. OD_API_EXIT();
  162. return(FALSE);
  163. }
  164. }
  165. /* Get the current system time. */
  166. nUnixTime=time(NULL);
  167. ptmTimeRecord=localtime(&nUnixTime);
  168. /* Determine which logfile format string to use. */
  169. if(ptmTimeRecord->tm_hour<10)
  170. {
  171. pszFormat=(char *)"> %1.1d:%02d:%02d %s\n";
  172. }
  173. else
  174. {
  175. pszFormat=(char *)"> %2.2d:%02d:%02d %s\n";
  176. }
  177. /* Write a line to the logfile. */
  178. fprintf(logfile_pointer, pszFormat, ptmTimeRecord->tm_hour,
  179. ptmTimeRecord->tm_min, ptmTimeRecord->tm_sec, pszMessage);
  180. OD_API_EXIT();
  181. return(TRUE);
  182. }
  183. /* ----------------------------------------------------------------------------
  184. * ODLogClose() *** PRIVATE FUNCTION ***
  185. *
  186. * Writes final entry to the logfile when OpenDoors is exiting.
  187. *
  188. * Parameters: nReason - Specifies the reason why OpenDoors is exiting.
  189. *
  190. * Return: void
  191. */
  192. static void ODLogClose(INT nReason)
  193. {
  194. /* Stop if logfile has been disabled in the config file, etc. */
  195. if(od_control.od_logfile_disable) return;
  196. /* If logfile has not been opened, then abort. */
  197. if(logfile_pointer==NULL) return;
  198. if(bPreOrExit)
  199. {
  200. od_log_write((char *)od_control.od_logfile_messages[13]);
  201. }
  202. else if(btExitReason<=5 && btExitReason>=1)
  203. {
  204. od_log_write((char *)od_control.od_logfile_messages[btExitReason-1]);
  205. }
  206. else
  207. {
  208. sprintf(szODWorkString,(char *)od_control.od_logfile_messages[5],nReason);
  209. od_log_write(szODWorkString);
  210. }
  211. /* Close the logfile. */
  212. fclose(logfile_pointer);
  213. /* Prevent further use of logfile without first re-opening it. */
  214. pfLogWrite = NULL;
  215. pfLogClose = NULL;
  216. logfile_pointer = NULL;
  217. }