FS2_Open
Open source remastering of the Freespace 2 engine
generic_log.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 
13 #include <stdarg.h>
14 
15 #include "cfile/cfile.h"
16 #include "globalincs/globals.h"
17 #include "parse/generic_log.h"
18 #include "parse/parselo.h"
19 
20 
21 // ----------------------------------------------------------------------------------------------------
22 // MULTI LOGFILE DEFINES/VARS
23 //
24 
25 // max length for a line of the logfile
26 #define MAX_LOGFILE_LINE_LEN 256
27 
28 // how often we'll write an update to the logfile (in seconds)
29 #define MULTI_LOGFILE_UPDATE_TIME 2520 // every 42 minutes
30 /*
31 // outfile itself
32 CFILE *log_file[];
33 */
34 
35 #define MAX_LOGFILES 2
36 
37 typedef struct logfile {
40 } logfile;
41 
43  // Filename, then the CFILE (which should usually be set to NULL)
44  {"multi.log" ,NULL},
45  {"event.log" ,NULL},
46 };
47 
48 // ----------------------------------------------------------------------------------------------------
49 // LOGFILE FUNCTIONS
50 //
51 
52 // initialize the logfile
53 bool logfile_init(int logfile_type)
54 {
55  if((logfile_type < 0) || (logfile_type >= MAX_LOGFILES)) {
56  Warning(LOCATION, "Attempt to write illegal logfile number %d", logfile_type);
57  return false;
58  }
59 
60  // attempt to open the file
61  logfiles[logfile_type].log_file = cfopen(logfiles[logfile_type].filename, "wt", CFILE_NORMAL, CF_TYPE_DATA);
62 
63  if(logfiles[logfile_type].log_file == NULL){
64  nprintf(("Network","Error opening %s for writing!!\n",logfiles[logfile_type].filename));
65  return false;
66  }
67 
68  return true;
69 }
70 
71 // close down the logfile
72 void logfile_close(int logfile_type)
73 {
74  // if we have a valid file, write a trailer and close
75  if(logfiles[logfile_type].log_file != NULL){
76 
77  cfclose(logfiles[logfile_type].log_file);
78  logfiles[logfile_type].log_file = NULL;
79  }
80 }
81 
82 // printf function itself called by the ml_printf macro
83 void log_printf(int logfile_type, char *format, ...)
84 {
86  va_list args;
87 
88  if (format == NULL) {
89  return;
90  }
91 
92  // if we don't have a valid logfile do nothing
93  if (logfiles[logfile_type].log_file == NULL) {
94  return;
95  }
96 
97  // format the text
98  va_start(args, format);
99  vsprintf(temp, format, args);
100  va_end(args);
101 
102  // log the string
103  log_string(logfile_type, temp.c_str());
104 }
105 
106 // string print function
107 void log_string(int logfile_type, const char *string, int add_time)
108 {
109  char tmp[MAX_LOGFILE_LINE_LEN*4];
110  char time_str[128];
111  time_t timer;
112 
113  // if we don't have a valid logfile do nothing
114  if(logfiles[logfile_type].log_file == NULL){
115  return;
116  }
117 
118  // if the passed string is NULL, do nothing
119  if(string == NULL){
120  return;
121  }
122 
123  // maybe add the time
124  if(add_time){
125  timer = time(NULL);
126 
127  strftime(time_str, 128, "%m/%d %H:%M:%S~ ", localtime(&timer));
128  strcpy_s(tmp, time_str);
129  strcat_s(tmp, string);
130  } else{
131  strcpy_s(tmp, string);
132  }
133  strcat_s(tmp, "\n");
134 
135  // now print it to the logfile if necessary
136  cfputs(tmp, logfiles[logfile_type].log_file);
137  cflush(logfiles[logfile_type].log_file);
138 
139 #if defined(LOGFILE_ECHO_TO_DEBUG)
140  mprintf(("Log file type %d %s",logfile_type, tmp));
141 #endif
142 }
GLenum GLsizei GLenum format
Definition: Gl.h:1509
#define CFILE_NORMAL
Definition: cfile.h:89
CFILE * log_file
Definition: generic_log.cpp:39
char filename[NAME_LENGTH]
Definition: generic_log.cpp:38
void _cdecl void void _cdecl void _cdecl Warning(char *filename, int line, SCP_FORMAT_STRING const char *format,...) SCP_FORMAT_STRING_ARGS(3
int cflush(CFILE *cfile)
Definition: cfile.cpp:1895
#define mprintf(args)
Definition: pstypes.h:238
Definition: cfile.h:28
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
void log_string(int logfile_type, const char *string, int add_time)
logfile logfiles[MAX_LOGFILES]
Definition: generic_log.cpp:42
void vsprintf(SCP_string &dest, const char *format, va_list ap)
Definition: parselo.cpp:3800
#define cfopen(...)
Definition: cfile.h:134
#define nprintf(args)
Definition: pstypes.h:239
#define MAX_LOGFILE_LINE_LEN
Definition: generic_log.cpp:26
char * filename
void logfile_close(int logfile_type)
Definition: generic_log.cpp:72
#define CF_TYPE_DATA
Definition: cfile.h:46
void log_printf(int logfile_type, char *format,...)
Definition: generic_log.cpp:83
int cfputs(const char *str, CFILE *cfile)
Definition: cfile.cpp:1504
#define strcat_s(...)
Definition: safe_strings.h:68
#define NAME_LENGTH
Definition: globals.h:15
bool logfile_init(int logfile_type)
Definition: generic_log.cpp:53
#define LOCATION
Definition: pstypes.h:245
#define MAX_LOGFILES
Definition: generic_log.cpp:35
int temp
Definition: lua.cpp:4996
int cfclose(CFILE *cfile)
Definition: cfile.cpp:895
struct logfile logfile
#define strcpy_s(...)
Definition: safe_strings.h:67