Revision 57
renamed Logging.cpp to Log.cpp to match the name of the class
trunk/code/projects/colonet/ColonetServer/Logging.cpp | ||
---|---|---|
1 |
/** |
|
2 |
* @file Logging.cpp |
|
3 |
* |
|
4 |
* @brief This file contains the code to do logging for Colonet |
|
5 |
* |
|
6 |
* @author Jason Knichel |
|
7 |
* |
|
8 |
*/ |
|
9 |
|
|
10 |
#include <time.h> |
|
11 |
#include <string.h> |
|
12 |
#include <stdio.h> |
|
13 |
|
|
14 |
#include "includes/Logging.h" |
|
15 |
|
|
16 |
/** |
|
17 |
* @brief Constructor for the Log class |
|
18 |
* |
|
19 |
* @param filename The name of the file to output log statements to |
|
20 |
*/ |
|
21 |
Log::Log(char * filename) { |
|
22 |
printf("Creating log object\n"); |
|
23 |
|
|
24 |
if (!filename) { |
|
25 |
fprintf(stderr, "Provided a null filename when trying to create a log.\n"); |
|
26 |
return; |
|
27 |
} |
|
28 |
|
|
29 |
logFile = fopen(filename, "a"); //open file for appending |
|
30 |
if (!logFile) { |
|
31 |
fprintf(stderr, "Error opening %s as log file.\n", filename); |
|
32 |
return; |
|
33 |
} |
|
34 |
|
|
35 |
printf("About to log start message.\n"); |
|
36 |
logMessage(LOG_TYPE_START_LOG, "Starting server"); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* @brief Destructor for the Log class |
|
41 |
*/ |
|
42 |
Log::~Log() { |
|
43 |
if (!logFile) |
|
44 |
fclose(logFile); |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* @brief A function to get the current time |
|
49 |
* |
|
50 |
* @return A struct tm that represents the current time |
|
51 |
*/ |
|
52 |
struct tm Log::getCurrentTime() { |
|
53 |
struct tm currTime; |
|
54 |
memset(&currTime, 0, sizeof(struct tm)); |
|
55 |
|
|
56 |
time_t t = time(NULL); |
|
57 |
localtime_r(&t, &currTime); |
|
58 |
|
|
59 |
return currTime; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* @brief This method logs a message with the specified type |
|
64 |
* |
|
65 |
* @param type The type of the message to log |
|
66 |
* @param message The message to log |
|
67 |
* |
|
68 |
* @return 0 on success, negative error code on error |
|
69 |
*/ |
|
70 |
int Log::logMessage(int type, char * message) { |
|
71 |
if (!logFile) { |
|
72 |
fprintf(stderr, "Tried to log a message but the file pointer was null.\n"); |
|
73 |
return -1; |
|
74 |
} |
|
75 |
|
|
76 |
if (!message) { |
|
77 |
fprintf(stderr, "Tried to log a null message.\n"); |
|
78 |
return -1; |
|
79 |
} |
|
80 |
|
|
81 |
static char * start_log = "Starting Log"; |
|
82 |
static char * connect = "Client Connecting"; |
|
83 |
static char * disconnect = "Client Disconnecting"; |
|
84 |
static char * error = "Error"; |
|
85 |
static char * logGenMessage = "Generic Message"; |
|
86 |
|
|
87 |
char * messageType; |
|
88 |
|
|
89 |
switch(type) { |
|
90 |
case LOG_TYPE_START_LOG: |
|
91 |
messageType = start_log; |
|
92 |
break; |
|
93 |
case LOG_TYPE_CONNECT: |
|
94 |
messageType = connect; |
|
95 |
break; |
|
96 |
case LOG_TYPE_DISCONNECT: |
|
97 |
messageType = disconnect; |
|
98 |
break; |
|
99 |
case LOG_TYPE_ERROR: |
|
100 |
messageType = error; |
|
101 |
break; |
|
102 |
case LOG_TYPE_MESSAGE: |
|
103 |
messageType = logGenMessage; |
|
104 |
break; |
|
105 |
default: |
|
106 |
fprintf(stderr, "Tried to log a message with an invalid type.\n"); |
|
107 |
return -1; |
|
108 |
} |
|
109 |
|
|
110 |
struct tm currTime = getCurrentTime(); |
|
111 |
|
|
112 |
char buffer[LOG_MAX_TIME_LENGTH]; |
|
113 |
asctime_r(&currTime, buffer); |
|
114 |
int len = strlen(buffer); |
|
115 |
buffer[len-1] = '\0'; //remove the newline that is put at the end by asctime_r |
|
116 |
fprintf(logFile, "%s %*s %s\n", buffer, LOG_MAX_TYPE_LENGTH, messageType, message); |
|
117 |
fflush(logFile); |
|
118 |
|
|
119 |
return 0; |
|
120 |
} |
trunk/code/projects/colonet/ColonetServer/Log.cpp | ||
---|---|---|
1 |
/** |
|
2 |
* @file Logging.cpp |
|
3 |
* |
|
4 |
* @brief This file contains the code to do logging for Colonet |
|
5 |
* |
|
6 |
* @author Jason Knichel |
|
7 |
* |
|
8 |
*/ |
|
9 |
|
|
10 |
#include <time.h> |
|
11 |
#include <string.h> |
|
12 |
#include <stdio.h> |
|
13 |
|
|
14 |
#include "includes/Log.h" |
|
15 |
|
|
16 |
/** |
|
17 |
* @brief Constructor for the Log class |
|
18 |
* |
|
19 |
* @param filename The name of the file to output log statements to |
|
20 |
*/ |
|
21 |
Log::Log(char * filename) { |
|
22 |
printf("Creating log object\n"); |
|
23 |
|
|
24 |
if (!filename) { |
|
25 |
fprintf(stderr, "Provided a null filename when trying to create a log.\n"); |
|
26 |
return; |
|
27 |
} |
|
28 |
|
|
29 |
logFile = fopen(filename, "a"); //open file for appending |
|
30 |
if (!logFile) { |
|
31 |
fprintf(stderr, "Error opening %s as log file.\n", filename); |
|
32 |
return; |
|
33 |
} |
|
34 |
|
|
35 |
printf("About to log start message.\n"); |
|
36 |
logMessage(LOG_TYPE_START_LOG, "Starting server"); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* @brief Destructor for the Log class |
|
41 |
*/ |
|
42 |
Log::~Log() { |
|
43 |
if (!logFile) |
|
44 |
fclose(logFile); |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* @brief A function to get the current time |
|
49 |
* |
|
50 |
* @return A struct tm that represents the current time |
|
51 |
*/ |
|
52 |
struct tm Log::getCurrentTime() { |
|
53 |
struct tm currTime; |
|
54 |
memset(&currTime, 0, sizeof(struct tm)); |
|
55 |
|
|
56 |
time_t t = time(NULL); |
|
57 |
localtime_r(&t, &currTime); |
|
58 |
|
|
59 |
return currTime; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* @brief This method logs a message with the specified type |
|
64 |
* |
|
65 |
* @param type The type of the message to log |
|
66 |
* @param message The message to log |
|
67 |
* |
|
68 |
* @return 0 on success, negative error code on error |
|
69 |
*/ |
|
70 |
int Log::logMessage(int type, char * message) { |
|
71 |
if (!logFile) { |
|
72 |
fprintf(stderr, "Tried to log a message but the file pointer was null.\n"); |
|
73 |
return -1; |
|
74 |
} |
|
75 |
|
|
76 |
if (!message) { |
|
77 |
fprintf(stderr, "Tried to log a null message.\n"); |
|
78 |
return -1; |
|
79 |
} |
|
80 |
|
|
81 |
static char * start_log = "Starting Log"; |
|
82 |
static char * connect = "Client Connecting"; |
|
83 |
static char * disconnect = "Client Disconnecting"; |
|
84 |
static char * error = "Error"; |
|
85 |
static char * logGenMessage = "Generic Message"; |
|
86 |
|
|
87 |
char * messageType; |
|
88 |
|
|
89 |
switch(type) { |
|
90 |
case LOG_TYPE_START_LOG: |
|
91 |
messageType = start_log; |
|
92 |
break; |
|
93 |
case LOG_TYPE_CONNECT: |
|
94 |
messageType = connect; |
|
95 |
break; |
|
96 |
case LOG_TYPE_DISCONNECT: |
|
97 |
messageType = disconnect; |
|
98 |
break; |
|
99 |
case LOG_TYPE_ERROR: |
|
100 |
messageType = error; |
|
101 |
break; |
|
102 |
case LOG_TYPE_MESSAGE: |
|
103 |
messageType = logGenMessage; |
|
104 |
break; |
|
105 |
default: |
|
106 |
fprintf(stderr, "Tried to log a message with an invalid type.\n"); |
|
107 |
return -1; |
|
108 |
} |
|
109 |
|
|
110 |
struct tm currTime = getCurrentTime(); |
|
111 |
|
|
112 |
char buffer[LOG_MAX_TIME_LENGTH]; |
|
113 |
asctime_r(&currTime, buffer); |
|
114 |
int len = strlen(buffer); |
|
115 |
buffer[len-1] = '\0'; //remove the newline that is put at the end by asctime_r |
|
116 |
fprintf(logFile, "%s %*s %s\n", buffer, LOG_MAX_TYPE_LENGTH, messageType, message); |
|
117 |
fflush(logFile); |
|
118 |
|
|
119 |
return 0; |
|
120 |
} |
trunk/code/projects/colonet/ColonetServer/includes/Logging.h | ||
---|---|---|
1 |
//TODO: rename either the file or the class so they match |
|
2 |
/** |
|
3 |
* |
|
4 |
* @author Jason Knichel |
|
5 |
* |
|
6 |
*/ |
|
7 |
|
|
8 |
#ifndef LOGGING_H |
|
9 |
#define LOGGING_H |
|
10 |
|
|
11 |
#include <time.h> |
|
12 |
#include <stdio.h> |
|
13 |
|
|
14 |
#define LOG_TYPE_START_LOG 0 |
|
15 |
#define LOG_TYPE_CONNECT 1 |
|
16 |
#define LOG_TYPE_DISCONNECT 2 |
|
17 |
#define LOG_TYPE_ERROR 3 |
|
18 |
#define LOG_TYPE_MESSAGE 4 |
|
19 |
|
|
20 |
#define LOG_MAX_TYPE_LENGTH 25 |
|
21 |
#define LOG_MAX_TIME_LENGTH 64 |
|
22 |
|
|
23 |
class Log { |
|
24 |
public: |
|
25 |
Log(char * fileName); |
|
26 |
~Log(); |
|
27 |
|
|
28 |
int logMessage(int type, char * message); |
|
29 |
|
|
30 |
private: |
|
31 |
struct tm getCurrentTime(); |
|
32 |
|
|
33 |
FILE * logFile; |
|
34 |
|
|
35 |
}; |
|
36 |
|
|
37 |
|
|
38 |
#endif |
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h | ||
---|---|---|
8 | 8 |
|
9 | 9 |
#include <colonet_wireless.h> |
10 | 10 |
#include "ConnectionPool.h" |
11 |
#include "Logging.h"
|
|
11 |
#include "Log.h" |
|
12 | 12 |
|
13 | 13 |
class ColonetServer { |
14 | 14 |
public: |
trunk/code/projects/colonet/ColonetServer/includes/Log.h | ||
---|---|---|
1 |
//TODO: rename either the file or the class so they match |
|
2 |
/** |
|
3 |
* |
|
4 |
* @author Jason Knichel |
|
5 |
* |
|
6 |
*/ |
|
7 |
|
|
8 |
#ifndef LOGGING_H |
|
9 |
#define LOGGING_H |
|
10 |
|
|
11 |
#include <time.h> |
|
12 |
#include <stdio.h> |
|
13 |
|
|
14 |
#define LOG_TYPE_START_LOG 0 |
|
15 |
#define LOG_TYPE_CONNECT 1 |
|
16 |
#define LOG_TYPE_DISCONNECT 2 |
|
17 |
#define LOG_TYPE_ERROR 3 |
|
18 |
#define LOG_TYPE_MESSAGE 4 |
|
19 |
|
|
20 |
#define LOG_MAX_TYPE_LENGTH 25 |
|
21 |
#define LOG_MAX_TIME_LENGTH 64 |
|
22 |
|
|
23 |
class Log { |
|
24 |
public: |
|
25 |
Log(char * fileName); |
|
26 |
~Log(); |
|
27 |
|
|
28 |
int logMessage(int type, char * message); |
|
29 |
|
|
30 |
private: |
|
31 |
struct tm getCurrentTime(); |
|
32 |
|
|
33 |
FILE * logFile; |
|
34 |
|
|
35 |
}; |
|
36 |
|
|
37 |
|
|
38 |
#endif |
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp | ||
---|---|---|
23 | 23 |
#include "includes/ConnectionPool.h" |
24 | 24 |
#include "includes/client.h" |
25 | 25 |
#include "includes/options.h" |
26 |
#include "includes/Logging.h"
|
|
26 |
#include "includes/Log.h" |
|
27 | 27 |
|
28 | 28 |
#define LISTEN_BACKLOG 5 |
29 | 29 |
#define LOG_BUFFER_LENGTH 128 |
trunk/code/projects/colonet/ColonetServer/Makefile | ||
---|---|---|
7 | 7 |
COLONETCPPOBJECTS = $(COLONETCPPFILES:.cpp=.o) |
8 | 8 |
COLONETFILES = options.c |
9 | 9 |
COLONETOBJECTS = $(COLONETFILES:.c=.o) |
10 |
LOGGINGFILES = Logging.cpp
|
|
10 |
LOGGINGFILES = Log.cpp |
|
11 | 11 |
LOGGINGOBJECTS = $(LOGGINGFILES:.cpp=.o) |
12 | 12 |
|
13 | 13 |
.PHONY : all clean |
Also available in: Unified diff