File: logfile.C

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (95 lines) | stat: -rw-r--r-- 2,075 bytes parent folder | download | duplicates (3)
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
#ifndef _global_h
#	include "global.h"
#endif
#ifndef _logfile_h
#	include "logfile.h"
#endif
#ifndef USE_EXTERNAL_STANDARD_INCLUDES
#	include <stdio.h>
#endif

#include <errno.h>
#include <unistd.h>

static char		message[200];
static time_t	start_time;

void end_log() {
FILE		*fp1,*fp2;
char		buffer[200];
time_t	end_time;
// char		new_message[200];
double	dt;
int		hh, mm;

	time(&end_time);
	dt = end_time - start_time;
	mm = (int)(dt/60);
	hh = mm/60;
	mm = mm%60;
 
	if ( rename( LOG_FILE, LOG_FILE".BAK" ) ) {
		// perror( LOG_FILE );
	}
	fp1 = fopen( LOG_FILE, "w" );
	if (!fp1)	return;

	fp2 = fopen( LOG_FILE".BAK", "r" );
	if (fp2) {
		while ( fgets( buffer, sizeof(buffer), fp2 ) ) {
			if ( !strcmp(buffer, message) ) {
				message[strlen(message)-1]='\0';
				if (dt/60>1)
					fprintf( fp1, "%s (%d:%02d)\n", message, hh, mm );
			}
			else fputs( buffer, fp1 );
		}
		fclose(fp2);
	}
	unlink( LOG_FILE".BAK" );

	fclose( fp1 );
}

void start_log( const char *game ) {
FILE	*fp1,*fp2;
char	buffer[200];
const char	*user_string = getenv("USER");
	if (!user_string || *user_string=='\0')		user_string = "unknown";
const char	*dpy_string  = getenv( "DISPLAY" );
	if (!dpy_string || *dpy_string=='\0')	      dpy_string = "localhost:0.0";

	printf( "flying:\n" );
	printf( "\t%s", flying_descriptor_string+4 );
	printf( "\t%s", flying_version_string+4 );

	time(&start_time);
	strftime( buffer, sizeof(buffer), "%d-%m-%y %H:%M:%S", localtime(&start_time) );
	sprintf( message, "%-8s%s %s: %s started by ", game, buffer,
			dpy_string,revision );
	if ( !strcmp( user_string, "emshh" ) )		return;

	strcat( message, user_string );
	strcat( message, "\n" );

	if ( rename( LOG_FILE, LOG_FILE".BAK" ) ) {
		// perror( LOG_FILE );
	}
	fp1 = fopen( LOG_FILE, "w" );
	if (!fp1)	return;
	fputs( message, fp1 );

	fp2 = fopen( LOG_FILE".BAK", "r" );
	if (fp2) {
		while ( fgets( buffer, sizeof(buffer), fp2 ) ) {
			fputs( buffer, fp1 );
		}
		fclose(fp2);
	}
	unlink( LOG_FILE".BAK" );

	fclose( fp1 );

	// only for ANSI C, therefor commented out
	// atexit( end_log );
}