-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.c
More file actions
85 lines (69 loc) · 2.41 KB
/
logging.c
File metadata and controls
85 lines (69 loc) · 2.41 KB
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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <ctype.h>
#include "game_util.h"
#include "logging.h"
void setTime(char* timeString) {
time_t t = time(NULL);
struct tm * p = localtime(&t);
strftime(timeString, 1000, "%X %A, %B %d %Y", p);
}
void startGameLog(char* black, char* white) {
char stringTime[100];
setTime(stringTime);
FILE *log = fopen("logfile.txt", "at");
if (!log)
log = fopen("logfile.txt", "wt");
if (!log) {
printf("can not open logfile.txt for writing.\n");
return;
}
fprintf(log, "%s%s", "\n=========================================",
"================================================\n");
fprintf(log, "Match between %s(BLACK) and %s(WHITE) /started at %s/",
black, white, stringTime);
fprintf(log, "%s%s", "\n=========================================",
"================================================\n");
fclose(log);
}
void saveMoveToLog(bool blackMove, char* black, char* white,
Point move, bool wrongMove) {
char stringTime[100];
setTime(stringTime);
FILE *log = fopen("logfile.txt", "at");
if (!log)
log = fopen("logfile.txt", "wt");
if (!log) {
printf("can not open logfile.txt for writing.\n");
return;
}
fprintf(log, "%s made move: %d-%c at %s, the move was %s.\n",
blackMove ? black : white, move.y + 1, move.x + 65, stringTime,
wrongMove ? "wrong" : "correct");
fclose(log);
}
void endGameLog(int blackScore, int whiteScore, char* black, char* white) {
FILE *log = fopen("logfile.txt", "at");
if (!log)
log = fopen("logfile.txt", "wt");
if (!log) {
printf("can not open logfile.txt for writing.\n");
return;
}
fprintf(log, "%s%s", "=========================================",
"================================================\n");
if(blackScore == whiteScore) {
fprintf(log, "The game ended in a draw.");
} else {
fprintf(log, "The winner is %s. Final score %d (%s) : %d (%s).\n",
blackScore > whiteScore ? black : white,
blackScore > whiteScore ? blackScore : whiteScore,
blackScore > whiteScore ? black : white,
blackScore > whiteScore ? whiteScore : blackScore,
blackScore > whiteScore ? white : black);
}
fprintf(log, "%s%s", "=========================================",
"================================================\n\n\n");
fclose(log);
}