l_log.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. /*****************************************************************************
  19. * name: l_log.c
  20. *
  21. * desc: log file
  22. *
  23. * $Archive: /MissionPack/CODE/botlib/l_log.c $
  24. *
  25. *****************************************************************************/
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "../game/q_shared.h"
  30. #include "../game/botlib.h"
  31. #include "be_interface.h" //for botimport.Print
  32. #include "l_libvar.h"
  33. #define MAX_LOGFILENAMESIZE 1024
  34. typedef struct logfile_s
  35. {
  36. char filename[MAX_LOGFILENAMESIZE];
  37. FILE *fp;
  38. int numwrites;
  39. } logfile_t;
  40. static logfile_t logfile;
  41. //===========================================================================
  42. //
  43. // Parameter: -
  44. // Returns: -
  45. // Changes Globals: -
  46. //===========================================================================
  47. void Log_Open(char *filename)
  48. {
  49. if (!LibVarValue("log", "0")) return;
  50. if (!filename || !strlen(filename))
  51. {
  52. botimport.Print(PRT_MESSAGE, "openlog <filename>\n");
  53. return;
  54. } //end if
  55. if (logfile.fp)
  56. {
  57. botimport.Print(PRT_ERROR, "log file %s is already opened\n", logfile.filename);
  58. return;
  59. } //end if
  60. logfile.fp = fopen(filename, "wb");
  61. if (!logfile.fp)
  62. {
  63. botimport.Print(PRT_ERROR, "can't open the log file %s\n", filename);
  64. return;
  65. } //end if
  66. strncpy(logfile.filename, filename, MAX_LOGFILENAMESIZE);
  67. botimport.Print(PRT_MESSAGE, "Opened log %s\n", logfile.filename);
  68. } //end of the function Log_Create
  69. //===========================================================================
  70. //
  71. // Parameter: -
  72. // Returns: -
  73. // Changes Globals: -
  74. //===========================================================================
  75. void Log_Close(void)
  76. {
  77. if (!logfile.fp) return;
  78. if (fclose(logfile.fp))
  79. {
  80. botimport.Print(PRT_ERROR, "can't close log file %s\n", logfile.filename);
  81. return;
  82. } //end if
  83. logfile.fp = NULL;
  84. botimport.Print(PRT_MESSAGE, "Closed log %s\n", logfile.filename);
  85. } //end of the function Log_Close
  86. //===========================================================================
  87. //
  88. // Parameter: -
  89. // Returns: -
  90. // Changes Globals: -
  91. //===========================================================================
  92. void Log_Shutdown(void)
  93. {
  94. if (logfile.fp) Log_Close();
  95. } //end of the function Log_Shutdown
  96. //===========================================================================
  97. //
  98. // Parameter: -
  99. // Returns: -
  100. // Changes Globals: -
  101. //===========================================================================
  102. void QDECL Log_Write(char *fmt, ...)
  103. {
  104. va_list ap;
  105. if (!logfile.fp) return;
  106. va_start(ap, fmt);
  107. vfprintf(logfile.fp, fmt, ap);
  108. va_end(ap);
  109. //fprintf(logfile.fp, "\r\n");
  110. fflush(logfile.fp);
  111. } //end of the function Log_Write
  112. //===========================================================================
  113. //
  114. // Parameter: -
  115. // Returns: -
  116. // Changes Globals: -
  117. //===========================================================================
  118. void QDECL Log_WriteTimeStamped(char *fmt, ...)
  119. {
  120. va_list ap;
  121. if (!logfile.fp) return;
  122. fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ",
  123. logfile.numwrites,
  124. (int) (botlibglobals.time / 60 / 60),
  125. (int) (botlibglobals.time / 60),
  126. (int) (botlibglobals.time),
  127. (int) ((int) (botlibglobals.time * 100)) -
  128. ((int) botlibglobals.time) * 100);
  129. va_start(ap, fmt);
  130. vfprintf(logfile.fp, fmt, ap);
  131. va_end(ap);
  132. fprintf(logfile.fp, "\r\n");
  133. logfile.numwrites++;
  134. fflush(logfile.fp);
  135. } //end of the function Log_Write
  136. //===========================================================================
  137. //
  138. // Parameter: -
  139. // Returns: -
  140. // Changes Globals: -
  141. //===========================================================================
  142. FILE *Log_FilePointer(void)
  143. {
  144. return logfile.fp;
  145. } //end of the function Log_FilePointer
  146. //===========================================================================
  147. //
  148. // Parameter: -
  149. // Returns: -
  150. // Changes Globals: -
  151. //===========================================================================
  152. void Log_Flush(void)
  153. {
  154. if (logfile.fp) fflush(logfile.fp);
  155. } //end of the function Log_Flush