Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / Log.cpp @ 764

History | View | Annotate | Download (3.16 KB)

1
/**
2
 * @file Log.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 <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
  log_file = fopen(filename, "a"); //open file for appending
30
  if (!log_file) {
31
    fprintf(stderr, "Error opening %s as log file.\n", filename);
32
    return;
33
  }
34

    
35
  printf("About to log start message.\n");
36
  log_string(LOG_TYPE_START_LOG, "Starting server");
37
}
38

    
39
/**
40
 * @brief Destructor for the Log class
41
 */
42
Log::~Log() {
43
  if (!log_file) {
44
    fclose(log_file);
45
  }
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 current_time;
55
  memset(&current_time, 0, sizeof(struct tm));
56

    
57
  time_t t = time(NULL);
58
  localtime_r(&t, &current_time);
59

    
60
  return current_time;
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 (!log_file) {
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 * log_generic_message = "Generic Message";
87

    
88
  char * message_type;
89

    
90
  switch(type) {
91
  case LOG_TYPE_START_LOG:
92
    message_type = start_log;
93
    break;
94
  case LOG_TYPE_CONNECT:
95
    message_type = connect;
96
    break;
97
  case LOG_TYPE_DISCONNECT:
98
    message_type = disconnect;
99
    break;
100
  case LOG_TYPE_ERROR:
101
    message_type = error;
102
    break;
103
  case LOG_TYPE_MESSAGE:
104
    message_type = log_generic_message;
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 current_time = get_current_time();
112

    
113
  char buffer[LOG_MAX_TIME_LENGTH];
114
  asctime_r(&current_time, 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(log_file, "%s %*s   %s\n", buffer, LOG_MAX_TYPE_LENGTH, message_type, message);
118
  fflush(log_file);
119

    
120
  return 0;
121
}
122

    
123
/**
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
int Log::log_error(char * message) {
131
  return log_string(LOG_TYPE_ERROR, message);
132
}
133

    
134
/**
135
 * @brief An easier way to log a normal message
136
 *
137
 * @param message The message to log as a normal message
138
 *
139
 * @return 0 on success, negative error code on error
140
 */
141
int Log::log_message(char *  message) {
142
  return log_string(LOG_TYPE_MESSAGE, message);
143
}