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