Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / Log.cpp @ 453

History | View | Annotate | Download (3.16 KB)

1 57 jknichel
/**
2 141 jknichel
 * @file Log.cpp
3 57 jknichel
 *
4
 * @brief This file contains the code to do logging for Colonet
5 161 emarinel
 *
6 57 jknichel
 * @author Jason Knichel
7
 *
8
 */
9
10
#include <time.h>
11
#include <string.h>
12
#include <stdio.h>
13
14 391 emarinel
#include <Log.h>
15 57 jknichel
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 141 jknichel
  log_file = fopen(filename, "a"); //open file for appending
30
  if (!log_file) {
31 57 jknichel
    fprintf(stderr, "Error opening %s as log file.\n", filename);
32
    return;
33
  }
34
35
  printf("About to log start message.\n");
36 132 jknichel
  log_string(LOG_TYPE_START_LOG, "Starting server");
37 57 jknichel
}
38
39
/**
40
 * @brief Destructor for the Log class
41
 */
42
Log::~Log() {
43 426 emarinel
  if (!log_file) {
44 141 jknichel
    fclose(log_file);
45 426 emarinel
  }
46 57 jknichel
}
47
48
/**
49
 * @brief A function to get the current time
50
 *
51
 * @return A struct tm that represents the current time
52
 */
53 132 jknichel
struct tm Log::get_current_time() {
54 141 jknichel
  struct tm current_time;
55
  memset(&current_time, 0, sizeof(struct tm));
56 161 emarinel
57 57 jknichel
  time_t t = time(NULL);
58 141 jknichel
  localtime_r(&t, &current_time);
59 161 emarinel
60
  return current_time;
61 57 jknichel
}
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 132 jknichel
int Log::log_string(int type, char * message) {
72 141 jknichel
  if (!log_file) {
73 57 jknichel
    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 161 emarinel
82 57 jknichel
  static char * start_log = "Starting Log";
83
  static char * connect = "Client Connecting";
84
  static char * disconnect = "Client Disconnecting";
85
  static char * error = "Error";
86 141 jknichel
  static char * log_generic_message = "Generic Message";
87 57 jknichel
88 141 jknichel
  char * message_type;
89 57 jknichel
90
  switch(type) {
91
  case LOG_TYPE_START_LOG:
92 141 jknichel
    message_type = start_log;
93 57 jknichel
    break;
94
  case LOG_TYPE_CONNECT:
95 141 jknichel
    message_type = connect;
96 57 jknichel
    break;
97
  case LOG_TYPE_DISCONNECT:
98 141 jknichel
    message_type = disconnect;
99 57 jknichel
    break;
100
  case LOG_TYPE_ERROR:
101 141 jknichel
    message_type = error;
102 57 jknichel
    break;
103
  case LOG_TYPE_MESSAGE:
104 141 jknichel
    message_type = log_generic_message;
105 57 jknichel
    break;
106
  default:
107
    fprintf(stderr, "Tried to log a message with an invalid type.\n");
108
    return -1;
109
  }
110
111 141 jknichel
  struct tm current_time = get_current_time();
112 161 emarinel
113 57 jknichel
  char buffer[LOG_MAX_TIME_LENGTH];
114 141 jknichel
  asctime_r(&current_time, buffer);
115 57 jknichel
  int len = strlen(buffer);
116
  buffer[len-1] = '\0'; //remove the newline that is put at the end by asctime_r
117 141 jknichel
  fprintf(log_file, "%s %*s   %s\n", buffer, LOG_MAX_TYPE_LENGTH, message_type, message);
118
  fflush(log_file);
119 57 jknichel
120
  return 0;
121
}
122 132 jknichel
123 141 jknichel
/**
124
 * @brief An easier way to log an error message
125
 *
126
 * @param message The message to log as an error
127
 *
128
 * @return 0 on success, negative error code on error
129
 */
130 132 jknichel
int Log::log_error(char * message) {
131
  return log_string(LOG_TYPE_ERROR, message);
132
}
133
134 141 jknichel
/**
135
 * @brief An easier way to log a normal message
136
 *
137
 * @param message The message to log as a normal message
138 161 emarinel
 *
139 141 jknichel
 * @return 0 on success, negative error code on error
140
 */
141 132 jknichel
int Log::log_message(char *  message) {
142
  return log_string(LOG_TYPE_MESSAGE, message);
143
}