16 extern server_config config
;
20 static void update_stat_file (const char *fn
, const session_info
*sess
, time_t now
)
24 int stats
[4] = {1, now
- sess
->game_stat
.start_time
, sess
->game_stat
.bytes_in
, sess
->game_stat
.bytes_out
};
28 if (fn
[0] == 0) return;
30 // make directory for it
32 make_dir (dirname(buf
));
35 if ((f
= fopen(fn
, "r+")) == NULL
)
37 write_log (LOG_ERROR
, "creating stat file %s", fn
);
40 if ((f
= fopen(fn
, "w")) == NULL
)
42 write_log (LOG_ERROR
, "error creating %s", fn
);
47 // lock file to block access from other threads
48 if (lockf(fileno(f
), F_LOCK
, 0) != 0)
50 write_log (LOG_ERROR
, "error locking %s", fn
);
60 for (i
= 0; i
< 4; i
++)
62 if (fgets(buf
, BUF_SIZE
, f
) == NULL
)
64 write_log (LOG_ERROR
, "error reading %s", fn
);
69 stats
[i
] += atoi(buf
);
72 fseek (f
, SEEK_SET
, 0);
76 n
= sprintf(buf
, "%d\n%d\n%d\n%d\n", stats
[0], stats
[1], stats
[2], stats
[3]);
77 if (fwrite(buf
, n
, 1, f
) != 1) write_log (LOG_ERROR
, "error writing %s", fn
);
84 static void append_log_file (const char *fn
, const session_info
*sess
, time_t now
)
87 char buf
[BUF_SIZE
], tm
[100];
92 if (fn
[0] == 0) return;
94 // make directory for it
96 make_dir (dirname(buf
));
99 if ((f
= fopen(fn
, "a")) == NULL
)
101 write_log (LOG_ERROR
, "error opening %s");
105 // lock file to block access from other threads
106 if (lockf(fileno(f
), F_LOCK
, 0) != 0)
108 write_log (LOG_ERROR
, "error locking %s", fn
);
113 // append session data
114 strftime (tm
, 100, "%F %H:%M:%S", localtime_r(&now
, &tnow
));
115 n
= sprintf(buf
, "%s\t%s\t%d\t%d\t%d\n", tm
, sess
->user_name
, (int)(now
- sess
->game_stat
.start_time
), sess
->game_stat
.bytes_in
, sess
->game_stat
.bytes_out
);
116 if (fwrite(buf
, n
, 1, f
) != 1) write_log (LOG_ERROR
, "error writing %s", fn
);
123 void update_stats (const game_info
*game
, const session_info
*sess
)
126 time_t now
= time(NULL
);
129 str_replace (fn
, BUF_SIZE
, config
.game_stat_file
, "$GAME$", game
->id
);
130 update_stat_file (fn
, sess
, now
);
132 str_replace (fn
, BUF_SIZE
, config
.game_log_file
, "$GAME$", game
->id
);
133 append_log_file (fn
, sess
, now
);