RocketLogger  1.1.4
log.c
Go to the documentation of this file.
1 
32 #include "log.h"
33 
39 void rl_log(rl_log_type type, const char* format, ...) {
40 
41  // open/init file
42  FILE* log_fp;
43  if (access(LOG_FILE, F_OK) == -1) {
44  // create file
45  log_fp = fopen(LOG_FILE, "w");
46  if (log_fp == NULL) {
47  printf("Error: failed to open log file\n");
48  return;
49  }
50  fprintf(log_fp, "--- RocketLogger Log File ---\n\n");
51  } else {
52  log_fp = fopen(LOG_FILE, "a");
53  if (log_fp == NULL) {
54  printf("Error: failed to open log file\n");
55  return;
56  }
57  }
58 
59  // reset, if file too large
60  int file_size = ftell(log_fp);
61  if (file_size > MAX_LOG_FILE_SIZE) {
62  fclose(log_fp);
63  fopen(LOG_FILE, "w");
64  fprintf(log_fp, "--- RocketLogger Log File ---\n\n");
65  }
66 
67  // print date/time
68  struct timeval current_time;
69  gettimeofday(&current_time, NULL);
70  time_t nowtime = current_time.tv_sec;
71  fprintf(log_fp, " %s", ctime(&nowtime));
72 
73  // get arguments
74  va_list args;
75  va_start(args, format);
76 
77  // print error message
78  if (type == ERROR) {
79 
80  // file
81  fprintf(log_fp, " Error: ");
82  vfprintf(log_fp, format, args);
83  fprintf(log_fp, "\n");
84  // terminal
85  printf("Error: ");
86  vprintf(format, args);
87  printf("\n\n");
88 
89  // set state to error
91 
92  } else if (type == WARNING) {
93 
94  // file
95  fprintf(log_fp, " Warning: ");
96  vfprintf(log_fp, format, args);
97  fprintf(log_fp, "\n");
98  // terminal
99  printf("Warning: ");
100  vprintf(format, args);
101  printf("\n\n");
102 
103  } else if (type == INFO) {
104 
105  fprintf(log_fp, " Info: ");
106  vfprintf(log_fp, format, args);
107  fprintf(log_fp, "\n");
108 
109  } else {
110  // for debugging purposes
111  printf("Error: wrong error-code\n");
112  }
113 
114  // facilitate return
115  va_end(args);
116 
117  // close file
118  fflush(log_fp);
119  fclose(log_fp);
120 }
#define LOG_FILE
Log file name.
Definition: types.h:84
struct rl_status status
Current status of RocketLogger.
Definition: rl_server.c:71
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:39
#define MAX_LOG_FILE_SIZE
Log file size in bytes.
Definition: types.h:91
rl_state state
State.
Definition: types.h:285
Warning.
Definition: types.h:201
enum log_type rl_log_type
Error.
Definition: types.h:200
Information.
Definition: types.h:202
Error.
Definition: types.h:143