aura  0.1
 All Data Structures Functions Variables Modules Pages
slog.c
1 /*
2  * slog is Advanced logging library for C/C++
3  *
4  * Copyright (c) 2015 Sun Dro (a.k.a. 7th Ghost)
5  * Web: http://off-sec.com/ ; E-Mail: kala0x13@gmail.com
6  *
7  * This is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 3 of the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  */
17 
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <limits.h>
24 #include <time.h>
25 #include <aura/slog.h>
26 
27 /* Supported colors */
28 #define CLR_NORM "\x1B[0m"
29 #define CLR_RED "\x1B[31m"
30 #define CLR_GREEN "\x1B[32m"
31 #define CLR_YELLOW "\x1B[33m"
32 #define CLR_BLUE "\x1B[34m"
33 #define CLR_NAGENTA "\x1B[35m"
34 #define CLR_CYAN "\x1B[36m"
35 #define CLR_WHITE "\x1B[37m"
36 #define CLR_RESET "\033[0m"
37 
38 /* Max size of string */
39 #define MAXMSG 8196
40 
41 /* Flags */
42 static slog_flags slg;
43 
44 
45 /*
46  * get_system_date - Intialize date with system date.
47  * Argument is pointer of SystemDate structure.
48  */
49 void get_system_date(SystemDate *mdate)
50 {
51  time_t rawtime;
52  struct tm *timeinfo;
53  rawtime = time(NULL);
54  timeinfo = localtime(&rawtime);
55  if (!timeinfo) {
56  memset(mdate, 0x0, sizeof(*mdate));
57  return;
58  }
59  /* Get System Date */
60  mdate->year = timeinfo->tm_year+1900;
61  mdate->mon = timeinfo->tm_mon+1;
62  mdate->day = timeinfo->tm_mday;
63  mdate->hour = timeinfo->tm_hour;
64  mdate->min = timeinfo->tm_min;
65  mdate->sec = timeinfo->tm_sec;
66 }
67 
68 
69 /*
70  * Get library version. Function returns version and build number of slog
71  * library. Return value is char pointer. Argument min is flag for output
72  * format. If min is 0, function returns version in full format, if flag
73  * is 1 function returns only version number, For examle: 1.3.0
74  */
75 const char* slog_version(int min)
76 {
77  static char verstr[128];
78 
79  /* Version short */
80  if (min) sprintf(verstr, "%d.%d.%d",
81  SLOGVERSION_MAX, SLOGVERSION_MIN, SLOGBUILD_NUM);
82 
83  /* Version long */
84  else sprintf(verstr, "%d.%d build %d (%s)",
85  SLOGVERSION_MAX, SLOGVERSION_MIN, SLOGBUILD_NUM, __DATE__);
86 
87  return verstr;
88 }
89 
90 
91 /*
92  * strclr - Colorize string. Function takes color value and string
93  * and returns colorized string as char pointer. First argument clr
94  * is color value (if it is invalid, function retunrs NULL) and second
95  * is string with va_list of arguments which one we want to colorize.
96  */
97 char* strclr(int clr, char* str, ...)
98 {
99  /* String buffers */
100  static char output[MAXMSG];
101  char string[MAXMSG];
102 
103  /* Read args */
104  va_list args;
105  va_start(args, str);
106  vsprintf(string, str, args);
107  va_end(args);
108 
109  /* Handle colors */
110  switch(clr)
111  {
112  case 0:
113  sprintf(output, "%s%s%s", CLR_NORM, string, CLR_RESET);
114  break;
115  case 1:
116  sprintf(output, "%s%s%s", CLR_GREEN, string, CLR_RESET);
117  break;
118  case 2:
119  sprintf(output, "%s%s%s", CLR_RED, string, CLR_RESET);
120  break;
121  case 3:
122  sprintf(output, "%s%s%s", CLR_YELLOW, string, CLR_RESET);
123  break;
124  case 4:
125  sprintf(output, "%s%s%s", CLR_BLUE, string, CLR_RESET);
126  break;
127  case 5:
128  sprintf(output, "%s%s%s", CLR_NAGENTA, string, CLR_RESET);
129  break;
130  case 6:
131  sprintf(output, "%s%s%s", CLR_CYAN, string, CLR_RESET);
132  break;
133  case 7:
134  sprintf(output, "%s%s%s", CLR_WHITE, string, CLR_RESET);
135  break;
136  default:
137  return NULL;
138  }
139 
140  /* Return output */
141  return output;
142 }
143 
144 
145 /*
146  * log_to_file - Save log in file. Argument aut is string which
147  * we want to log. Argument fname is log file path and mdate is
148  * SystemDate structure variable, we need it to create filename.
149  */
150 void log_to_file(char *out, const char *fname, SystemDate *mdate)
151 {
152  /* Used variables */
153  char filename[PATH_MAX];
154 
155  /* Create log filename with date */
156  sprintf(filename, "%s-%02d-%02d-%02d.log",
157  fname, mdate->year, mdate->mon, mdate->day);
158 
159  /* Open file pointer */
160  FILE *fp = fopen(filename, "a");
161  if (fp == NULL) return;
162 
163  /* Write key in file */
164  fprintf(fp, "%s", out);
165 
166  /* Close file pointer */
167  fclose(fp);
168 }
169 
170 
171 /*
172  * Retunr string in slog format. Function takes arguments
173  * and returns string in slog format without printing and
174  * saveing in file. Return value is char pointer.
175  */
176 char* slog_sprintf(char *msg, ...)
177 {
178  /* Used variables */
179  static char output[MAXMSG];
180  char string[MAXMSG];
181  SystemDate mdate;
182 
183  /* initialise system date */
184  get_system_date(&mdate);
185 
186  /* Read args */
187  va_list args;
188  va_start(args, msg);
189  vsprintf(string, msg, args);
190  va_end(args);
191 
192  /* Generate output string with date */
193  sprintf(output, "%02d.%02d.%02d-%02d:%02d:%02d - %s",
194  mdate.year, mdate.mon, mdate.day, mdate.hour,
195  mdate.min, mdate.sec, string);
196 
197  /* Return output */
198  return output;
199 }
200 
208 void slogv(int level, int flag, const char *msg, va_list args)
209 {
210  if(level > slg.level)
211  return;
212 
213  SystemDate mdate;
214  char string[MAXMSG];
215  char prints[MAXMSG];
216  char *output;
217 
218  /* Initialise system date */
219  get_system_date(&mdate);
220 
221  /* Read args */
222  vsprintf(string, msg, args);
223  /* Handle flags */
224  switch(flag) {
225  case 1:
226  sprintf(prints, "[LIVE] %s", string);
227  break;
228  case 2:
229  sprintf(prints, "[%s] %s", strclr(1, "INFO"), string);
230  break;
231  case 3:
232  sprintf(prints, "[%s] %s", strclr(3, "WARN"), string);
233  break;
234  case 4:
235  sprintf(prints, "[%s] %s", strclr(4, "DEBUG"), string);
236  break;
237  case 5:
238  sprintf(prints, "[%s] %s", strclr(2, "ERROR"), string);
239  break;
240  case 6:
241  sprintf(prints, "[%s] %s", strclr(2, "FATAL"), string);
242  break;
243  case 7:
244  sprintf(prints, "%s", string);
245  break;
246  default:
247  break;
248  }
249 
250  /* Print output */
251  fprintf(stderr, "%s", slog_sprintf("%s\n", prints));
252 
253  /* Save log in file */
254  if (slg.to_file)
255  {
256  output = slog_sprintf("%s\n", string);
257  log_to_file(output, slg.fname, &mdate);
258  }
259 }
260 
261 /*
262  * slog - Log exiting process. Function takes arguments and saves
263  * log in file if LOGTOFILE flag is enabled from config. Otherwise
264  * it just prints log without saveing in file. Argument level is
265  * logging level and flag is slog flags defined in slog.h header.
266  */
267 void slog(int level, int flag, const char *msg, ...)
268 {
269  va_list args;
270  va_start(args, msg);
271  slogv(level, flag, msg, args);
272  va_end(args);
273 }
274 
275 
276 /*
277  * Initialize slog library. Function parses config file and reads log
278  * level and save to file flag from config. First argument is file name
279  * where log will be saved and second argument conf is config file path
280  * to be parsed and third argument lvl is log level for this message.
281  */
282 void slog_init(const char* fname, int lvl)
283 {
284  slg.level = lvl;
285  slg.fname = fname;
286  if (fname)
287  slg.to_file = 1;
288  else
289  slg.to_file = 0;
290 }