printx.c
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include "printx.h"
#include "ui.h"
#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"};
char f_name[5][5] = {"MAIN", "UI ", "NFC ", "HVC ", "BUS "};
float start;
void removeCharFromString(char c, char *str)
{
int len = strlen(str)+1;
for(int i=0; i<len; i++)
if(str[i] == c)
strncpy(&str[i],&str[i+1],len-i);
}
bool initLog()
{
char filename[FILENAME_LENGTH];
time_t now = time(NULL);
strftime(filename, FILENAME_LENGTH, "%F-%T:%d", localtime(&now));
setStartTime(filename);
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;
}
start = clock();
return true;
}
void closeLog()
{
fclose(logfile);
}
void printx(severity s, msgfrom from, char* str, ...)
{
char buffer1[MAX_BUFFER];
char buffer2[MAX_BUFFER];
va_list arglist;
float now = clock();
va_start(arglist, str);
vsprintf(buffer1, str, arglist);
fprintf(logfile, "[%6f] : %s", (now - start)/CLOCKS_PER_SEC, buffer1);
fflush(logfile);
sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
removeCharFromString('\n', buffer2);
addToLog(buffer2);
va_end(arglist);
}