/* * log.cpp * * Created on: 5.12.2010 * Author: msloup */ #include #include #include #include #include "protocol.h" #include "util.h" #include "log.h" using namespace std; #define LOG_SERVER_FILENAME "server.log" #define LOG_CLIENT_FILENAME "C%d.log" #define LOG_FILENAME_LEN 13 fstream log_file; void log_open(char *filename) { log_file.open(filename, ios::out); } void log_receive(struct protocol_header header, bool is_client) { int client_id = ntohl(header.client_id); if (!log_file.is_open()) { char filename[LOG_FILENAME_LEN]; if (is_client) { sprintf(filename, LOG_CLIENT_FILENAME, client_id); } else { sprintf(filename, LOG_SERVER_FILENAME); } log_open(filename); } struct timeval send_at; struct timeval current_time; gettimeofday(¤t_time, NULL); send_at.tv_sec = ntohl(header.send_at_sec); send_at.tv_usec = ntohl(header.send_at_usec); std::string colors[] = {"#00cc8e", "#00cc79", "#cc0042", "#00cc3a", "#00ccc6", "#cc1a00", "#cc00ab", "#00cc6c", "#cc00bc", "#00cc63", "#00cc81", "#cc2400", "#c7cc00", "#7bcc00"}; std::string color = colors[client_id % 14]; log_file.precision(20); log_file << "- color: '" << color << "'" << endl; log_file << " data: " << header.operation << "(" << ntohs(header.count) << ")" << endl; if (is_client) { log_file << " dst: {p: C" << client_id << ", ts: " << timeval2double(¤t_time) << "}" << endl; log_file << " src: {p: SERVER, ts: " << timeval2double(&send_at) << "}" << endl; } else { log_file << " dst: {p: SERVER, ts: " << timeval2double(¤t_time) << "}" << endl; log_file << " src: {p: C" << client_id << ", ts: " << timeval2double(&send_at) << "}" << endl; } } void log_close() { log_file.close(); }