File: generic_log.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (140 lines) | stat: -rw-r--r-- 3,141 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/




#include <cstdarg>

#include "cfile/cfile.h"
#include "globalincs/globals.h"
#include "parse/generic_log.h"
#include "parse/parselo.h"


// ----------------------------------------------------------------------------------------------------
// MULTI LOGFILE DEFINES/VARS
//

// how often we'll write an update to the logfile (in seconds)
#define MULTI_LOGFILE_UPDATE_TIME			2520			// every 42 minutes
/*
// outfile itself
CFILE *log_file[];
*/

#define MAX_LOGFILES						2

typedef struct logfile {
	char filename[NAME_LENGTH];
	CFILE *log_file;
} logfile;

logfile logfiles[MAX_LOGFILES] = {
	// Filename, then the CFILE (which should usually be set to NULL)
	{"multi.log"			,NULL}, 
	{"event.log"			,NULL},
};

// ----------------------------------------------------------------------------------------------------
// LOGFILE FUNCTIONS
//

// initialize the logfile
bool logfile_init(int logfile_type)
{
	if((logfile_type < 0) || (logfile_type >= MAX_LOGFILES)) {
		Warning(LOCATION, "Attempt to write illegal logfile number %d", logfile_type);
		return false;
	}

	// attempt to open the file
	logfiles[logfile_type].log_file = cfopen(logfiles[logfile_type].filename, "wt", CFILE_NORMAL, CF_TYPE_DATA);

	if(logfiles[logfile_type].log_file == NULL){
		nprintf(("Network","Error opening %s for writing!!\n",logfiles[logfile_type].filename));
		return false;
	}

	return true;
}

// close down the logfile
void logfile_close(int logfile_type)
{
	// if we have a valid file, write a trailer and close
	if(logfiles[logfile_type].log_file != NULL){

		cfclose(logfiles[logfile_type].log_file);
		logfiles[logfile_type].log_file = NULL;
	}
}

// printf function itself called by the ml_printf macro
void log_printf(int logfile_type, const char *format, ...)
{
	SCP_string temp;
	va_list args;

	if (format == NULL) {
		return;
	}

	// if we don't have a valid logfile do nothing
	if (logfiles[logfile_type].log_file == NULL) {
		return;
	}
	
	// format the text
	va_start(args, format);
	vsprintf(temp, format, args);
	va_end(args);
	
	// log the string
	log_string(logfile_type, temp.c_str());
}

// string print function
void log_string(int logfile_type, const char *string, int add_time)
{
	SCP_string tmp;
	char time_str[128];
	time_t timer;	

	// if we don't have a valid logfile do nothing
	if(logfiles[logfile_type].log_file == NULL){
		return;
	}

	// if the passed string is NULL, do nothing
	if(string == NULL){
		return;
	}

	// maybe add the time
	if(add_time){
		timer = time(NULL);

		strftime(time_str, 128, "%m/%d %H:%M:%S~   ", localtime(&timer));
		tmp = time_str;
		tmp += string;
	} else{
		tmp = string;
	}

	tmp += "\n";

	// now print it to the logfile if necessary	
	cfputs(tmp.c_str(), logfiles[logfile_type].log_file);
	cflush(logfiles[logfile_type].log_file);

#if defined(LOGFILE_ECHO_TO_DEBUG)
	mprintf(("Log file type %d %s\n",logfile_type, tmp.c_str()));
#endif
}