l_log.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "qbsp.h"
  22. #define MAX_LOGFILENAMESIZE 1024
  23. typedef struct logfile_s
  24. {
  25. char filename[MAX_LOGFILENAMESIZE];
  26. FILE *fp;
  27. int numwrites;
  28. } logfile_t;
  29. logfile_t logfile;
  30. //===========================================================================
  31. //
  32. // Parameter: -
  33. // Returns: -
  34. // Changes Globals: -
  35. //===========================================================================
  36. void Log_Open(char *filename)
  37. {
  38. if (!filename || !strlen(filename))
  39. {
  40. printf("openlog <filename>\n");
  41. return;
  42. } //end if
  43. if (logfile.fp)
  44. {
  45. printf("log file %s is already opened\n", logfile.filename);
  46. return;
  47. } //end if
  48. logfile.fp = fopen(filename, "wb");
  49. if (!logfile.fp)
  50. {
  51. printf("can't open the log file %s\n", filename);
  52. return;
  53. } //end if
  54. strncpy(logfile.filename, filename, MAX_LOGFILENAMESIZE);
  55. printf("Opened log %s\n", logfile.filename);
  56. } //end of the function Log_Create
  57. //===========================================================================
  58. //
  59. // Parameter: -
  60. // Returns: -
  61. // Changes Globals: -
  62. //===========================================================================
  63. void Log_Close(void)
  64. {
  65. if (!logfile.fp)
  66. {
  67. printf("no log file to close\n");
  68. return;
  69. } //end if
  70. if (fclose(logfile.fp))
  71. {
  72. printf("can't close log file %s\n", logfile.filename);
  73. return;
  74. } //end if
  75. logfile.fp = NULL;
  76. printf("Closed log %s\n", logfile.filename);
  77. } //end of the function Log_Close
  78. //===========================================================================
  79. //
  80. // Parameter: -
  81. // Returns: -
  82. // Changes Globals: -
  83. //===========================================================================
  84. void Log_Shutdown(void)
  85. {
  86. if (logfile.fp) Log_Close();
  87. } //end of the function Log_Shutdown
  88. //===========================================================================
  89. //
  90. // Parameter: -
  91. // Returns: -
  92. // Changes Globals: -
  93. //===========================================================================
  94. void Log_UnifyEndOfLine(char *buf)
  95. {
  96. int i;
  97. for (i = 0; buf[i]; i++)
  98. {
  99. if (buf[i] == '\n')
  100. {
  101. if (i <= 0 || buf[i-1] != '\r')
  102. {
  103. memmove(&buf[i+1], &buf[i], strlen(&buf[i])+1);
  104. buf[i] = '\r';
  105. i++;
  106. } //end if
  107. } //end if
  108. } //end for
  109. } //end of the function Log_UnifyEndOfLine
  110. //===========================================================================
  111. //
  112. // Parameter: -
  113. // Returns: -
  114. // Changes Globals: -
  115. //===========================================================================
  116. void Log_Print(char *fmt, ...)
  117. {
  118. va_list ap;
  119. char buf[2048];
  120. va_start(ap, fmt);
  121. vsprintf(buf, fmt, ap);
  122. va_end(ap);
  123. if (verbose)
  124. {
  125. #ifdef WINBSPC
  126. WinBSPCPrint(buf);
  127. #else
  128. printf("%s", buf);
  129. #endif //WINBSPS
  130. } //end if
  131. if (logfile.fp)
  132. {
  133. Log_UnifyEndOfLine(buf);
  134. fprintf(logfile.fp, "%s", buf);
  135. fflush(logfile.fp);
  136. } //end if
  137. } //end of the function Log_Print
  138. //===========================================================================
  139. //
  140. // Parameter: -
  141. // Returns: -
  142. // Changes Globals: -
  143. //===========================================================================
  144. void Log_Write(char *fmt, ...)
  145. {
  146. va_list ap;
  147. char buf[2048];
  148. if (!logfile.fp) return;
  149. va_start(ap, fmt);
  150. vsprintf(buf, fmt, ap);
  151. va_end(ap);
  152. Log_UnifyEndOfLine(buf);
  153. fprintf(logfile.fp, "%s", buf);
  154. fflush(logfile.fp);
  155. } //end of the function Log_Write
  156. //===========================================================================
  157. //
  158. // Parameter: -
  159. // Returns: -
  160. // Changes Globals: -
  161. //===========================================================================
  162. void Log_WriteTimeStamped(char *fmt, ...)
  163. {
  164. va_list ap;
  165. if (!logfile.fp) return;
  166. /* fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ",
  167. logfile.numwrites,
  168. (int) (botlibglobals.time / 60 / 60),
  169. (int) (botlibglobals.time / 60),
  170. (int) (botlibglobals.time),
  171. (int) ((int) (botlibglobals.time * 100)) -
  172. ((int) botlibglobals.time) * 100);*/
  173. va_start(ap, fmt);
  174. vfprintf(logfile.fp, fmt, ap);
  175. va_end(ap);
  176. logfile.numwrites++;
  177. fflush(logfile.fp);
  178. } //end of the function Log_Write
  179. //===========================================================================
  180. //
  181. // Parameter: -
  182. // Returns: -
  183. // Changes Globals: -
  184. //===========================================================================
  185. FILE *Log_FileStruct(void)
  186. {
  187. return logfile.fp;
  188. } //end of the function Log_FileStruct
  189. //===========================================================================
  190. //
  191. // Parameter: -
  192. // Returns: -
  193. // Changes Globals: -
  194. //===========================================================================
  195. void Log_Flush(void)
  196. {
  197. if (logfile.fp) fflush(logfile.fp);
  198. } //end of the function Log_Flush