Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

l_log.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 #include "qbsp.h"
00028 
00029 #define MAX_LOGFILENAMESIZE     1024
00030 
00031 typedef struct logfile_s
00032 {
00033     char filename[MAX_LOGFILENAMESIZE];
00034     FILE *fp;
00035     int numwrites;
00036 } logfile_t;
00037 
00038 logfile_t logfile;
00039 
00040 //===========================================================================
00041 //
00042 // Parameter:               -
00043 // Returns:                 -
00044 // Changes Globals:     -
00045 //===========================================================================
00046 void Log_Open(char *filename)
00047 {
00048     if (!filename || !strlen(filename))
00049     {
00050         printf("openlog <filename>\n");
00051         return;
00052     } //end if
00053     if (logfile.fp)
00054     {
00055         printf("log file %s is already opened\n", logfile.filename);
00056         return;
00057     } //end if
00058     logfile.fp = fopen(filename, "wb");
00059     if (!logfile.fp)
00060     {
00061         printf("can't open the log file %s\n", filename);
00062         return;
00063     } //end if
00064     strncpy(logfile.filename, filename, MAX_LOGFILENAMESIZE);
00065     printf("Opened log %s\n", logfile.filename);
00066 } //end of the function Log_Create
00067 //===========================================================================
00068 //
00069 // Parameter:               -
00070 // Returns:                 -
00071 // Changes Globals:     -
00072 //===========================================================================
00073 void Log_Close(void)
00074 {
00075     if (!logfile.fp)
00076     {
00077         printf("no log file to close\n");
00078         return;
00079     } //end if
00080     if (fclose(logfile.fp))
00081     {
00082         printf("can't close log file %s\n", logfile.filename);
00083         return;
00084     } //end if
00085     logfile.fp = NULL;
00086     printf("Closed log %s\n", logfile.filename);
00087 } //end of the function Log_Close
00088 //===========================================================================
00089 //
00090 // Parameter:               -
00091 // Returns:                 -
00092 // Changes Globals:     -
00093 //===========================================================================
00094 void Log_Shutdown(void)
00095 {
00096     if (logfile.fp) Log_Close();
00097 } //end of the function Log_Shutdown
00098 //===========================================================================
00099 //
00100 // Parameter:           -
00101 // Returns:             -
00102 // Changes Globals:     -
00103 //===========================================================================
00104 void Log_UnifyEndOfLine(char *buf)
00105 {
00106     int i;
00107 
00108     for (i = 0; buf[i]; i++)
00109     {
00110         if (buf[i] == '\n')
00111         {
00112             if (i <= 0 || buf[i-1] != '\r')
00113             {
00114                 memmove(&buf[i+1], &buf[i], strlen(&buf[i])+1);
00115                 buf[i] = '\r';
00116                 i++;
00117             } //end if
00118         } //end if
00119     } //end for
00120 } //end of the function Log_UnifyEndOfLine
00121 //===========================================================================
00122 //
00123 // Parameter:           -
00124 // Returns:             -
00125 // Changes Globals:     -
00126 //===========================================================================
00127 void Log_Print(char *fmt, ...)
00128 {
00129     va_list ap;
00130     char buf[2048];
00131 
00132     va_start(ap, fmt);
00133     vsprintf(buf, fmt, ap);
00134     va_end(ap);
00135 
00136     if (verbose)
00137     {
00138 #ifdef WINBSPC
00139         WinBSPCPrint(buf);
00140 #else
00141         printf("%s", buf);
00142 #endif //WINBSPS
00143     } //end if
00144 
00145     if (logfile.fp)
00146     {
00147         Log_UnifyEndOfLine(buf);
00148         fprintf(logfile.fp, "%s", buf);
00149         fflush(logfile.fp);
00150     } //end if
00151 } //end of the function Log_Print
00152 //===========================================================================
00153 //
00154 // Parameter:               -
00155 // Returns:                 -
00156 // Changes Globals:     -
00157 //===========================================================================
00158 void Log_Write(char *fmt, ...)
00159 {
00160     va_list ap;
00161     char buf[2048];
00162 
00163     if (!logfile.fp) return;
00164     va_start(ap, fmt);
00165     vsprintf(buf, fmt, ap);
00166     va_end(ap);
00167     Log_UnifyEndOfLine(buf);
00168     fprintf(logfile.fp, "%s", buf);
00169     fflush(logfile.fp);
00170 } //end of the function Log_Write
00171 //===========================================================================
00172 //
00173 // Parameter:               -
00174 // Returns:                 -
00175 // Changes Globals:     -
00176 //===========================================================================
00177 void Log_WriteTimeStamped(char *fmt, ...)
00178 {
00179     va_list ap;
00180 
00181     if (!logfile.fp) return;
00182 /*  fprintf(logfile.fp, "%d   %02d:%02d:%02d:%02d   ",
00183                     logfile.numwrites,
00184                     (int) (botlibglobals.time / 60 / 60),
00185                     (int) (botlibglobals.time / 60),
00186                     (int) (botlibglobals.time),
00187                     (int) ((int) (botlibglobals.time * 100)) -
00188                             ((int) botlibglobals.time) * 100);*/
00189     va_start(ap, fmt);
00190     vfprintf(logfile.fp, fmt, ap);
00191     va_end(ap);
00192     logfile.numwrites++;
00193     fflush(logfile.fp);
00194 } //end of the function Log_Write
00195 //===========================================================================
00196 //
00197 // Parameter:               -
00198 // Returns:                 -
00199 // Changes Globals:     -
00200 //===========================================================================
00201 FILE *Log_FileStruct(void)
00202 {
00203     return logfile.fp;
00204 } //end of the function Log_FileStruct
00205 //===========================================================================
00206 //
00207 // Parameter:               -
00208 // Returns:                 -
00209 // Changes Globals:     -
00210 //===========================================================================
00211 void Log_Flush(void)
00212 {
00213     if (logfile.fp) fflush(logfile.fp);
00214 } //end of the function Log_Flush
00215 

Generated on Thu Aug 25 12:37:12 2005 for Quake III Arena by  doxygen 1.3.9.1