Revision 132
changed the Log class a little and made it conform to coding style slightly better
trunk/code/projects/colonet/ColonetServer/Log.cpp | ||
---|---|---|
5 | 5 |
* |
6 | 6 |
* @author Jason Knichel |
7 | 7 |
* |
8 |
* @TODO: change this file to follow the coding style |
|
8 | 9 |
*/ |
9 | 10 |
|
10 | 11 |
#include <time.h> |
... | ... | |
33 | 34 |
} |
34 | 35 |
|
35 | 36 |
printf("About to log start message.\n"); |
36 |
logMessage(LOG_TYPE_START_LOG, "Starting server");
|
|
37 |
log_string(LOG_TYPE_START_LOG, "Starting server");
|
|
37 | 38 |
} |
38 | 39 |
|
39 | 40 |
/** |
... | ... | |
49 | 50 |
* |
50 | 51 |
* @return A struct tm that represents the current time |
51 | 52 |
*/ |
52 |
struct tm Log::getCurrentTime() {
|
|
53 |
struct tm Log::get_current_time() {
|
|
53 | 54 |
struct tm currTime; |
54 | 55 |
memset(&currTime, 0, sizeof(struct tm)); |
55 | 56 |
|
... | ... | |
67 | 68 |
* |
68 | 69 |
* @return 0 on success, negative error code on error |
69 | 70 |
*/ |
70 |
int Log::logMessage(int type, char * message) {
|
|
71 |
int Log::log_string(int type, char * message) {
|
|
71 | 72 |
if (!logFile) { |
72 | 73 |
fprintf(stderr, "Tried to log a message but the file pointer was null.\n"); |
73 | 74 |
return -1; |
... | ... | |
107 | 108 |
return -1; |
108 | 109 |
} |
109 | 110 |
|
110 |
struct tm currTime = getCurrentTime();
|
|
111 |
struct tm currTime = get_current_time();
|
|
111 | 112 |
|
112 | 113 |
char buffer[LOG_MAX_TIME_LENGTH]; |
113 | 114 |
asctime_r(&currTime, buffer); |
... | ... | |
118 | 119 |
|
119 | 120 |
return 0; |
120 | 121 |
} |
122 |
|
|
123 |
int Log::log_error(char * message) { |
|
124 |
return log_string(LOG_TYPE_ERROR, message); |
|
125 |
} |
|
126 |
|
|
127 |
int Log::log_message(char * message) { |
|
128 |
return log_string(LOG_TYPE_MESSAGE, message); |
|
129 |
} |
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h | ||
---|---|---|
17 | 17 |
|
18 | 18 |
int start_listening(); |
19 | 19 |
|
20 |
int log_error(char * error_message); |
|
21 |
int log_message(char * message); |
|
22 |
|
|
23 | 20 |
int run_server(); |
24 | 21 |
|
25 | 22 |
ConnectionPool * get_connection_pool_pointer(); |
trunk/code/projects/colonet/ColonetServer/includes/Log.h | ||
---|---|---|
24 | 24 |
Log(char * fileName); |
25 | 25 |
~Log(); |
26 | 26 |
|
27 |
int logMessage(int type, char * message); |
|
27 |
int log_string(int type, char * message); |
|
28 |
int log_error(char * message); |
|
29 |
int log_message(char * message); |
|
28 | 30 |
|
29 | 31 |
private: |
30 |
struct tm getCurrentTime();
|
|
32 |
struct tm get_current_time();
|
|
31 | 33 |
|
32 | 34 |
FILE * logFile; |
33 | 35 |
}; |
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp | ||
---|---|---|
74 | 74 |
*/ |
75 | 75 |
int ColonetServer::start_listening() { |
76 | 76 |
if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
77 |
log_error("\t\nThere was an error telling the socket to " |
|
77 |
logger.log_error("\t\nThere was an error telling the socket to "
|
|
78 | 78 |
"listen for connections from clients. Terminating Server...\n"); |
79 | 79 |
return -1; |
80 | 80 |
} |
81 | 81 |
return 0; |
82 | 82 |
} |
83 | 83 |
|
84 |
/** |
|
85 |
* @brief Logs an error message to the log file |
|
86 |
*/ |
|
87 |
int ColonetServer::log_error(char * error_message) { |
|
88 |
return logger.logMessage(LOG_TYPE_ERROR, error_message); |
|
89 |
} |
|
90 | 84 |
|
91 | 85 |
/** |
92 |
* @brief Logs a message to the log file |
|
93 |
*/ |
|
94 |
int ColonetServer::log_message(char * message) { |
|
95 |
return logger.logMessage(LOG_TYPE_MESSAGE, message); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 | 86 |
* @brief Starts the server running (starts an infinite loop) |
100 | 87 |
*/ |
101 | 88 |
int ColonetServer::run_server() { |
... | ... | |
109 | 96 |
struct sockaddr_in client_addr; |
110 | 97 |
socklen_t client_addr_size = sizeof(client_addr); |
111 | 98 |
|
112 |
log_message("Server initialized. About to start listening for connections"); |
|
99 |
logger.log_message("Server initialized. About to start listening for connections");
|
|
113 | 100 |
|
114 | 101 |
while(1) { |
115 | 102 |
connection_pool.perform_select(listen_socket); |
... | ... | |
136 | 123 |
char log_buffer[LOG_BUFFER_LENGTH]; |
137 | 124 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", |
138 | 125 |
inet_ntoa(client_addr.sin_addr)); |
139 |
logger.logMessage(LOG_TYPE_CONNECT, log_buffer);
|
|
126 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer);
|
|
140 | 127 |
|
141 | 128 |
if (connection_pool.add_client(accept_socket) < 0) { |
142 | 129 |
printf("\tThere was an error when trying to add a client to the connection pool."); |
... | ... | |
145 | 132 |
|
146 | 133 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", |
147 | 134 |
inet_ntoa(client_addr.sin_addr)); |
148 |
logger.logMessage(LOG_TYPE_CONNECT, log_buffer);
|
|
135 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer);
|
|
149 | 136 |
} |
150 | 137 |
|
151 | 138 |
if (connection_pool.check_clients() < 0) { |
... | ... | |
257 | 244 |
connection_pool = colonet_server.get_connection_pool_pointer(); |
258 | 245 |
|
259 | 246 |
if (colonet_server.initialize_server(argc, argv) < 0) { |
260 |
colonet_server.log_error("\t\nThere was an error initializing the server. "
|
|
247 |
printf("\t\nThere was an error initializing the server. "
|
|
261 | 248 |
"Terminating server...\n"); |
262 | 249 |
return -1; |
263 | 250 |
} |
Also available in: Unified diff