root / trunk / code / projects / colonet / ColonetServer / Log.cpp @ 132
History | View | Annotate | Download (2.85 KB)
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 |
* @TODO: change this file to follow the coding style
|
9 |
*/
|
10 |
|
11 |
#include <time.h> |
12 |
#include <string.h> |
13 |
#include <stdio.h> |
14 |
|
15 |
#include "includes/Log.h" |
16 |
|
17 |
/**
|
18 |
* @brief Constructor for the Log class
|
19 |
*
|
20 |
* @param filename The name of the file to output log statements to
|
21 |
*/
|
22 |
Log::Log(char * filename) {
|
23 |
printf("Creating log object\n");
|
24 |
|
25 |
if (!filename) {
|
26 |
fprintf(stderr, "Provided a null filename when trying to create a log.\n");
|
27 |
return;
|
28 |
} |
29 |
|
30 |
logFile = fopen(filename, "a"); //open file for appending |
31 |
if (!logFile) {
|
32 |
fprintf(stderr, "Error opening %s as log file.\n", filename);
|
33 |
return;
|
34 |
} |
35 |
|
36 |
printf("About to log start message.\n");
|
37 |
log_string(LOG_TYPE_START_LOG, "Starting server");
|
38 |
} |
39 |
|
40 |
/**
|
41 |
* @brief Destructor for the Log class
|
42 |
*/
|
43 |
Log::~Log() { |
44 |
if (!logFile)
|
45 |
fclose(logFile); |
46 |
} |
47 |
|
48 |
/**
|
49 |
* @brief A function to get the current time
|
50 |
*
|
51 |
* @return A struct tm that represents the current time
|
52 |
*/
|
53 |
struct tm Log::get_current_time() {
|
54 |
struct tm currTime;
|
55 |
memset(&currTime, 0, sizeof(struct tm)); |
56 |
|
57 |
time_t t = time(NULL);
|
58 |
localtime_r(&t, &currTime); |
59 |
|
60 |
return currTime;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* @brief This method logs a message with the specified type
|
65 |
*
|
66 |
* @param type The type of the message to log
|
67 |
* @param message The message to log
|
68 |
*
|
69 |
* @return 0 on success, negative error code on error
|
70 |
*/
|
71 |
int Log::log_string(int type, char * message) { |
72 |
if (!logFile) {
|
73 |
fprintf(stderr, "Tried to log a message but the file pointer was null.\n");
|
74 |
return -1; |
75 |
} |
76 |
|
77 |
if (!message) {
|
78 |
fprintf(stderr, "Tried to log a null message.\n");
|
79 |
return -1; |
80 |
} |
81 |
|
82 |
static char * start_log = "Starting Log"; |
83 |
static char * connect = "Client Connecting"; |
84 |
static char * disconnect = "Client Disconnecting"; |
85 |
static char * error = "Error"; |
86 |
static char * logGenMessage = "Generic Message"; |
87 |
|
88 |
char * messageType;
|
89 |
|
90 |
switch(type) {
|
91 |
case LOG_TYPE_START_LOG:
|
92 |
messageType = start_log; |
93 |
break;
|
94 |
case LOG_TYPE_CONNECT:
|
95 |
messageType = connect; |
96 |
break;
|
97 |
case LOG_TYPE_DISCONNECT:
|
98 |
messageType = disconnect; |
99 |
break;
|
100 |
case LOG_TYPE_ERROR:
|
101 |
messageType = error; |
102 |
break;
|
103 |
case LOG_TYPE_MESSAGE:
|
104 |
messageType = logGenMessage; |
105 |
break;
|
106 |
default:
|
107 |
fprintf(stderr, "Tried to log a message with an invalid type.\n");
|
108 |
return -1; |
109 |
} |
110 |
|
111 |
struct tm currTime = get_current_time();
|
112 |
|
113 |
char buffer[LOG_MAX_TIME_LENGTH];
|
114 |
asctime_r(&currTime, buffer); |
115 |
int len = strlen(buffer);
|
116 |
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); |
119 |
|
120 |
return 0; |
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 |
} |