forked from NachiketKelkar/AESD-Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
136 lines (111 loc) · 3.6 KB
/
logger.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/************************************************************************************************
* File name : logger.c *
* Authors : Puneet Bansal and Nachiket Kelkar *
* Description : The function definition used for communication by logger task. *
* Tools used : GNU make, gcc, arm-linux-gcc *
************************************************************************************************/
#include "logger.h"
extern int count;
void logToFile(char *fileName, logStruct dataToReceive)
{
//printf("value of count is %d",count);
FILE *logging;
char level[20];
//char state[20];
if(dataToReceive.logLevel==alert)
{
strcpy(level,"[ALERT]");
}
else if(dataToReceive.logLevel==info)
{
strcpy(level,"[INFO]");
}
else
{
strcpy(level,"[DEBUG]");
}
//mainInfoToOthers fileInfo;
//printf("file name in logtofile function is %s\n",fileName);
//printf("debug level is %s\n",level);
//printf("value of timestamp in logtofile function is %s\n",printTimeStamp());
if(strcmp(dataToReceive.source,"temperature")==0)
{
//printf("Recevied unit is %s\n",dataToReceive.unit);
if(count ==1)
{
logging = fopen(fileName,"w");
count=0;
}
else
{
logging = fopen(fileName,"a");
}
if(dataToReceive.status==success)
{
fprintf(logging,"%s %s [%s] : %f %s\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.value,dataToReceive.unit);
}
else
{
fprintf(logging, "%s %s [%s] (Failure message) - %s\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.message);
}
fclose(logging);
}
if(strcmp(dataToReceive.source,"light")==0)
{
if(count ==1)
{
logging = fopen(fileName,"w");
count=0;
}
else
{
logging = fopen(fileName,"a");
}
if(dataToReceive.status==success)
{
fprintf(logging,"%s %s [%s] : %f\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.value);
}
else
{
fprintf(logging, "%s %s [%s] %s\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.message);
}
fclose(logging);
}
if(strcmp(dataToReceive.source,"socket")==0)
{
if(count ==1)
{
logging = fopen(fileName,"w");
count=0;
}
else
{
logging = fopen(fileName,"a");
}
fprintf(logging, "%s %s [%s] %s\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.message);
fclose(logging);
}
if(strcmp(dataToReceive.source,"main")==0)
{
if(count ==1)
{
logging = fopen(fileName,"w");
count=0;
}
else
{
logging = fopen(fileName,"a");
}
fprintf(logging, "%s %s [%s] %s\n",printTimeStamp(),level,dataToReceive.source,dataToReceive.message);
fclose(logging);
}
}
char* printTimeStamp()
{
char* time_stamp=malloc(40);
struct timespec thTimeSpec;
clock_gettime(CLOCK_REALTIME, &thTimeSpec);
sprintf(time_stamp,"[s: %ld, ns: %ld]",thTimeSpec.tv_sec,thTimeSpec.tv_nsec);
//printf("Value of time_stamp is %s",time_stamp);
return time_stamp;
}