Blame view

printx.c 2.05 KB
ae25085d   henyxia   Output function done
1
2
3
4
5
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdarg.h>
  #include <stdbool.h>
  #include <string.h>
b8e91a2f   henyxia   Displaying everyt...
6
  #include <sys/time.h>
ae25085d   henyxia   Output function done
7
  #include "printx.h"
ea1218f1   henyxia   UI finished
8
  #include "ui.h"
ae25085d   henyxia   Output function done
9
10
11
12
13
  
  #define	FILENAME_LENGTH	24
  #define	S_RESET			"\33[0m"
  #define	MAX_BUFFER		128
  
6e35f0fb   henyxia   Temp now shown in...
14
  FILE* logfiles[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
ae25085d   henyxia   Output function done
15
  char s_color[4][12] = {"\x1b[01;31m", "\x1b[01;33m", "\x1b[01;32m", "\x1b[01;36m"};
b8e91a2f   henyxia   Displaying everyt...
16
17
  char f_name[6][5] = {"MAIN", "UI  ", "NFC ", "HVC ", "BUS ", "TEMP"};
  struct timeval tv;
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()
  {
c0152e3d   henyxia   Separated log files
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	logfiles[0] = fopen("log/main.log", "a");
  	if(logfiles[0] == NULL)
  	{
  		printf("Unable to open the main log file\n");
  		return false;
  	}
  	logfiles[1] = fopen("log/ui.log", "a");
  	if(logfiles[1] == NULL)
  	{
  		printf("Unable to open the UI log file\n");
  		return false;
  	}
  
  	logfiles[2] = fopen("log/nfc.log", "a");
  	if(logfiles[2] == NULL)
  	{
  		printf("Unable to open the NFC log file\n");
  		return false;
  	}
ae25085d   henyxia   Output function done
49
  
c0152e3d   henyxia   Separated log files
50
51
52
53
54
55
  	logfiles[3] = fopen("log/hvc.log", "a");
  	if(logfiles[3] == NULL)
  	{
  		printf("Unable to open the HVC log file\n");
  		return false;
  	}
ae25085d   henyxia   Output function done
56
  
c0152e3d   henyxia   Separated log files
57
58
59
60
61
62
63
64
  	logfiles[4] = fopen("log/bus.log", "a");
  	if(logfiles[4] == NULL)
  	{
  		printf("Unable to open the BUS log file\n");
  		return false;
  	}
  
  	logfiles[5] = fopen("log/temp.log", "a");
b8e91a2f   henyxia   Displaying everyt...
65
  	if(logfiles[5] == NULL)
ae25085d   henyxia   Output function done
66
  	{
b8e91a2f   henyxia   Displaying everyt...
67
  		printf("Unable to open the temperature log file\n");
ae25085d   henyxia   Output function done
68
69
70
  		return false;
  	}
  
ae25085d   henyxia   Output function done
71
72
73
74
75
  	return true;
  }
  
  void closeLog()
  {
c0152e3d   henyxia   Separated log files
76
77
78
79
80
  	fclose(logfiles[0]);
  	fclose(logfiles[1]);
  	fclose(logfiles[2]);
  	fclose(logfiles[3]);
  	fclose(logfiles[4]);
b8e91a2f   henyxia   Displaying everyt...
81
  	fclose(logfiles[5]);
ae25085d   henyxia   Output function done
82
83
  }
  
297a72df   henyxia   Added sender
84
  void printx(severity s, msgfrom from, char* str, ...)
ae25085d   henyxia   Output function done
85
  {
19082eeb   henyxia   UI design not wor...
86
  	char	buffer1[MAX_BUFFER];
ea1218f1   henyxia   UI finished
87
  	char	buffer2[MAX_BUFFER];
ae25085d   henyxia   Output function done
88
  	va_list	arglist;
ae25085d   henyxia   Output function done
89
  	va_start(arglist, str);
19082eeb   henyxia   UI design not wor...
90
  	vsprintf(buffer1, str, arglist);
b8e91a2f   henyxia   Displaying everyt...
91
  	gettimeofday(&tv,NULL);
c0152e3d   henyxia   Separated log files
92
93
  	fprintf(logfiles[from], "[%10ld.%06ld] : %s", tv.tv_sec, tv.tv_usec, buffer1);
  	fflush(logfiles[from]);
3bdc9daf   henyxia   NFC Added
94
  	sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
80e0082e   henyxia   Some fixes, separ...
95
96
  	if(s>DEBUG)
  		printf("%s", buffer2);
ae25085d   henyxia   Output function done
97
98
  	va_end(arglist);
  }