Blame view

printx.c 1.46 KB
ae25085d   henyxia   Output function done
1
2
3
4
5
6
7
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdarg.h>
  #include <stdbool.h>
  #include <string.h>
  #include <time.h>
  #include "printx.h"
ea1218f1   henyxia   UI finished
8
  #include "ui.h"
ae25085d   henyxia   Output function done
9
10
11
12
13
14
15
  
  #define	FILENAME_LENGTH	24
  #define	S_RESET			"\33[0m"
  #define	MAX_BUFFER		128
  
  FILE* logfile = NULL;
  char s_color[4][12] = {"\x1b[01;31m", "\x1b[01;33m", "\x1b[01;32m", "\x1b[01;36m"};
297a72df   henyxia   Added sender
16
  char f_name[5][5] = {"MAIN", "UI  ", "NFC ", "HVC ", "BUS "};
c21a2c16   henyxia   Re added timestam...
17
  float start;
ae25085d   henyxia   Output function done
18
  
ea1218f1   henyxia   UI finished
19
20
  void removeCharFromString(char c, char *str)
  {
fd498c68   henyxia   Some more feature...
21
  	int len = strlen(str)+1;
ea1218f1   henyxia   UI finished
22
23
24
25
  
  	for(int i=0; i<len; i++)
  		if(str[i] == c)
  			strncpy(&str[i],&str[i+1],len-i);
ea1218f1   henyxia   UI finished
26
27
  }
  
ae25085d   henyxia   Output function done
28
29
  bool initLog()
  {
ae25085d   henyxia   Output function done
30
31
32
  	char	filename[FILENAME_LENGTH];
  	time_t	now = time(NULL);
  
ea1218f1   henyxia   UI finished
33
34
  	strftime(filename, FILENAME_LENGTH, "%F-%T:%d", localtime(&now));
  	setStartTime(filename);
ae25085d   henyxia   Output function done
35
36
37
38
39
40
41
42
43
44
  	strftime(filename, FILENAME_LENGTH, "log/%F-%T:%d", localtime(&now));
  	strcat(filename, ".log");
  
  	logfile = fopen(filename, "w");
  	if(logfile == NULL)
  	{
  		printf("Unable to open the log file\n");
  		return false;
  	}
  
c21a2c16   henyxia   Re added timestam...
45
46
  	start = clock();
  
ae25085d   henyxia   Output function done
47
48
49
50
51
52
53
54
  	return true;
  }
  
  void closeLog()
  {
  	fclose(logfile);
  }
  
297a72df   henyxia   Added sender
55
  void printx(severity s, msgfrom from, char* str, ...)
ae25085d   henyxia   Output function done
56
  {
19082eeb   henyxia   UI design not wor...
57
  	char	buffer1[MAX_BUFFER];
ea1218f1   henyxia   UI finished
58
  	char	buffer2[MAX_BUFFER];
ae25085d   henyxia   Output function done
59
  	va_list	arglist;
8dca7aeb   henyxia   Automatic Pumping...
60
  	float	now = clock();
ae25085d   henyxia   Output function done
61
  	va_start(arglist, str);
19082eeb   henyxia   UI design not wor...
62
  	vsprintf(buffer1, str, arglist);
8dca7aeb   henyxia   Automatic Pumping...
63
64
  	fprintf(logfile, "[%6f] : %s", (now - start)/CLOCKS_PER_SEC, buffer1);
  	fflush(logfile);
3bdc9daf   henyxia   NFC Added
65
  	sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
ea1218f1   henyxia   UI finished
66
67
  	removeCharFromString('\n', buffer2);
  	addToLog(buffer2);
ae25085d   henyxia   Output function done
68
69
  	va_end(arglist);
  }