Revision 141
made the log file adhere to style guidelines a bit better
Log.cpp | ||
---|---|---|
1 | 1 |
/** |
2 |
* @file Logging.cpp
|
|
2 |
* @file Log.cpp |
|
3 | 3 |
* |
4 | 4 |
* @brief This file contains the code to do logging for Colonet |
5 | 5 |
* |
6 | 6 |
* @author Jason Knichel |
7 | 7 |
* |
8 |
* @TODO: change this file to follow the coding style |
|
9 | 8 |
*/ |
10 | 9 |
|
11 | 10 |
#include <time.h> |
... | ... | |
27 | 26 |
return; |
28 | 27 |
} |
29 | 28 |
|
30 |
logFile = fopen(filename, "a"); //open file for appending
|
|
31 |
if (!logFile) {
|
|
29 |
log_file = fopen(filename, "a"); //open file for appending
|
|
30 |
if (!log_file) {
|
|
32 | 31 |
fprintf(stderr, "Error opening %s as log file.\n", filename); |
33 | 32 |
return; |
34 | 33 |
} |
... | ... | |
41 | 40 |
* @brief Destructor for the Log class |
42 | 41 |
*/ |
43 | 42 |
Log::~Log() { |
44 |
if (!logFile)
|
|
45 |
fclose(logFile);
|
|
43 |
if (!log_file)
|
|
44 |
fclose(log_file);
|
|
46 | 45 |
} |
47 | 46 |
|
48 | 47 |
/** |
... | ... | |
51 | 50 |
* @return A struct tm that represents the current time |
52 | 51 |
*/ |
53 | 52 |
struct tm Log::get_current_time() { |
54 |
struct tm currTime;
|
|
55 |
memset(&currTime, 0, sizeof(struct tm));
|
|
53 |
struct tm current_time;
|
|
54 |
memset(¤t_time, 0, sizeof(struct tm));
|
|
56 | 55 |
|
57 | 56 |
time_t t = time(NULL); |
58 |
localtime_r(&t, &currTime);
|
|
57 |
localtime_r(&t, ¤t_time);
|
|
59 | 58 |
|
60 |
return currTime;
|
|
59 |
return current_time;
|
|
61 | 60 |
} |
62 | 61 |
|
63 | 62 |
/** |
... | ... | |
69 | 68 |
* @return 0 on success, negative error code on error |
70 | 69 |
*/ |
71 | 70 |
int Log::log_string(int type, char * message) { |
72 |
if (!logFile) {
|
|
71 |
if (!log_file) {
|
|
73 | 72 |
fprintf(stderr, "Tried to log a message but the file pointer was null.\n"); |
74 | 73 |
return -1; |
75 | 74 |
} |
... | ... | |
83 | 82 |
static char * connect = "Client Connecting"; |
84 | 83 |
static char * disconnect = "Client Disconnecting"; |
85 | 84 |
static char * error = "Error"; |
86 |
static char * logGenMessage = "Generic Message";
|
|
85 |
static char * log_generic_message = "Generic Message";
|
|
87 | 86 |
|
88 |
char * messageType;
|
|
87 |
char * message_type;
|
|
89 | 88 |
|
90 | 89 |
switch(type) { |
91 | 90 |
case LOG_TYPE_START_LOG: |
92 |
messageType = start_log;
|
|
91 |
message_type = start_log;
|
|
93 | 92 |
break; |
94 | 93 |
case LOG_TYPE_CONNECT: |
95 |
messageType = connect;
|
|
94 |
message_type = connect;
|
|
96 | 95 |
break; |
97 | 96 |
case LOG_TYPE_DISCONNECT: |
98 |
messageType = disconnect;
|
|
97 |
message_type = disconnect;
|
|
99 | 98 |
break; |
100 | 99 |
case LOG_TYPE_ERROR: |
101 |
messageType = error;
|
|
100 |
message_type = error;
|
|
102 | 101 |
break; |
103 | 102 |
case LOG_TYPE_MESSAGE: |
104 |
messageType = logGenMessage;
|
|
103 |
message_type = log_generic_message;
|
|
105 | 104 |
break; |
106 | 105 |
default: |
107 | 106 |
fprintf(stderr, "Tried to log a message with an invalid type.\n"); |
108 | 107 |
return -1; |
109 | 108 |
} |
110 | 109 |
|
111 |
struct tm currTime = get_current_time();
|
|
110 |
struct tm current_time = get_current_time();
|
|
112 | 111 |
|
113 | 112 |
char buffer[LOG_MAX_TIME_LENGTH]; |
114 |
asctime_r(&currTime, buffer);
|
|
113 |
asctime_r(¤t_time, buffer);
|
|
115 | 114 |
int len = strlen(buffer); |
116 | 115 |
buffer[len-1] = '\0'; //remove the newline that is put at the end by asctime_r |
117 |
fprintf(logFile, "%s %*s %s\n", buffer, LOG_MAX_TYPE_LENGTH, messageType, message);
|
|
118 |
fflush(logFile);
|
|
116 |
fprintf(log_file, "%s %*s %s\n", buffer, LOG_MAX_TYPE_LENGTH, message_type, message);
|
|
117 |
fflush(log_file);
|
|
119 | 118 |
|
120 | 119 |
return 0; |
121 | 120 |
} |
122 | 121 |
|
122 |
/** |
|
123 |
* @brief An easier way to log an error message |
|
124 |
* |
|
125 |
* @param message The message to log as an error |
|
126 |
* |
|
127 |
* @return 0 on success, negative error code on error |
|
128 |
*/ |
|
123 | 129 |
int Log::log_error(char * message) { |
124 | 130 |
return log_string(LOG_TYPE_ERROR, message); |
125 | 131 |
} |
126 | 132 |
|
133 |
/** |
|
134 |
* @brief An easier way to log a normal message |
|
135 |
* |
|
136 |
* @param message The message to log as a normal message |
|
137 |
* |
|
138 |
* @return 0 on success, negative error code on error |
|
139 |
*/ |
|
127 | 140 |
int Log::log_message(char * message) { |
128 | 141 |
return log_string(LOG_TYPE_MESSAGE, message); |
129 | 142 |
} |
Also available in: Unified diff