PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/shared/log.cpp

https://bitbucket.org/vivkin/gam3b00bs/
C++ | 63 lines | 42 code | 16 blank | 5 comment | 3 complexity | 7b42ac6dd8432f95d915745f01b79cf6 MD5 | raw file
 1#include "common.h"
 2#include "log.h"
 3#include <stdio.h>
 4#include <windows.h>
 5#include <assert.h>
 6//-----------------------------------------------------------------------------
 7
 8FILE*				log_file = 0;
 9CRITICAL_SECTION	log_mutex;
10//-----------------------------------------------------------------------------
11
12int log_open( const char* path )
13{
14	assert(path);
15
16	if( log_file )
17		fclose(log_file);
18
19	log_file = fopen(path, "w");
20
21	if( !log_file )
22		return 1;
23
24	InitializeCriticalSection( &log_mutex );
25
26	return 0;
27}
28//-----------------------------------------------------------------------------
29
30void log_write( const char* format, ... )
31{
32	assert(format);
33	assert(log_file);
34
35	EnterCriticalSection( &log_mutex );
36
37	va_list ap;
38	va_start(ap, format);
39	vfprintf(log_file, format, ap);
40	va_end(ap);
41
42	va_start(ap, format);
43	vfprintf( stderr, format, ap );
44	va_end(ap);
45
46	fprintf(stderr, "\n");
47	fprintf(log_file, "\n");
48
49	fflush(log_file);
50
51	LeaveCriticalSection( &log_mutex );
52}
53//-----------------------------------------------------------------------------
54
55void log_close()
56{
57	DeleteCriticalSection( &log_mutex );
58
59	if( log_file )
60		fclose(log_file);
61	log_file = 0;
62}
63//-----------------------------------------------------------------------------