RocketLogger  1.0
log.c
Go to the documentation of this file.
1 
5 #include "log.h"
6 
12 void rl_log(rl_log_type type, const char* format, ... ) {
13 
14  // open/init file
15  FILE* log_fp;
16  if(access( LOG_FILE, F_OK ) == -1) {
17  // create file
18  log_fp = fopen(LOG_FILE, "w");
19  if (log_fp == NULL) {
20  printf("Error: failed to open log file\n");
21  return;
22  }
23  fprintf(log_fp, "--- RocketLogger Log File ---\n\n");
24  } else {
25  log_fp = fopen(LOG_FILE, "a");
26  if (log_fp == NULL) {
27  printf("Error: failed to open log file\n");
28  return;
29  }
30  }
31 
32  // reset, if file too large
33  int file_size = ftell(log_fp);
34  if (file_size > MAX_LOG_FILE_SIZE) {
35  fclose(log_fp);
36  fopen(LOG_FILE, "w");
37  fprintf(log_fp, "--- RocketLogger Log File ---\n\n");
38  }
39 
40 
41 
42  // print date/time
43  struct timeval current_time;
44  gettimeofday(&current_time, NULL);
45  time_t nowtime = current_time.tv_sec;
46  fprintf(log_fp, " %s", ctime(&nowtime));
47 
48  // get arguments
49  va_list args;
50  va_start(args, format);
51 
52  // print error message
53  if(type == ERROR) {
54 
55  // file
56  fprintf(log_fp, " Error: ");
57  vfprintf(log_fp, format, args);
58  fprintf(log_fp, "\n");
59  // terminal
60  printf("Error: ");
61  vprintf(format, args);
62  printf("\n\n");
63 
64  // set state to error
66 
67  } else if(type == WARNING) {
68 
69  // file
70  fprintf(log_fp, " Warning: ");
71  vfprintf(log_fp, format, args);
72  fprintf(log_fp, "\n");
73  // terminal
74  printf("Warning: ");
75  vprintf(format, args);
76  printf("\n\n");
77 
78  } else if(type == INFO) {
79 
80  fprintf(log_fp, " Info: ");
81  vfprintf(log_fp, format, args);
82  fprintf(log_fp, "\n");
83 
84  } else {
85  // for debugging purposes
86  printf("Error: wrong error-code\n");
87  }
88 
89  // facilitate return
90  va_end(args);
91 
92  // close file
93  fflush(log_fp);
94  fclose(log_fp);
95 }
#define LOG_FILE
Log file name.
Definition: types.h:59
struct rl_status status
Current status of RocketLogger.
Definition: rl_server.c:42
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:12
#define MAX_LOG_FILE_SIZE
Log file size in bytes.
Definition: types.h:66
rl_state state
State.
Definition: types.h:234
Warning.
Definition: types.h:168
enum log_type rl_log_type
Error.
Definition: types.h:167
Information.
Definition: types.h:169
Error.
Definition: types.h:119