root / trunk / code / projects / colonet / ColonetServer / Log.cpp @ 57
History | View | Annotate | Download (2.62 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 |
*/
|
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 |
} |