-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
287 lines (231 loc) · 6.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "logger.h"
short isInitialized_bo = FALSE;
FILE *file;
char fileNameArray[FILE_NAME_ARRAY_LENGTH] = {0};
pthread_t importanceThread, switchLoggingThread, dumpThread;
atomic_int importanceState;
atomic_int flag_change_importance;
atomic_int loggingEnabledState;
atomic_int flag_switch_logging;
atomic_int flag_dump;
pthread_mutex_t saveToFileLock;
void (*dumpFuncPtr)(void) = NULL;
void handler_change_importance()
{
atomic_store(&flag_change_importance, TRUE);
}
void handler_switch_logging()
{
atomic_store(&flag_switch_logging, TRUE);
}
void handler_dump()
{
atomic_store(&flag_dump, TRUE);
}
void *dumpHandler(void *args)
{
sigset_t set;
sigfillset(&set);
sigdelset(&set, DUMP_SIGNAL);
pthread_sigmask(SIG_SETMASK, &set, NULL);
while (TRUE)
{
if (TRUE == atomic_load(&flag_dump))
{
if (dumpFuncPtr != NULL)
{
(*dumpFuncPtr)();
}
printf("Dump function used has been used (if defined)\n");
atomic_store(&flag_dump, FALSE);
}
else if (THREAD_END_VALUE == atomic_load(&flag_dump))
{
break;
}
usleep(200);
}
}
void *switchLoggingHandler(void *args)
{
sigset_t set;
sigfillset(&set);
sigdelset(&set, SWITCH_LOGGING_SIGNAL);
pthread_sigmask(SWITCH_LOGGING_SIGNAL, &set, NULL);
while (TRUE)
{
// printf("Wciaz dzialam 1\n");
if (TRUE == atomic_load(&flag_switch_logging))
{
if (FALSE == atomic_load(&loggingEnabledState))
{
atomic_store(&loggingEnabledState, TRUE);
}
else
{
atomic_store(&loggingEnabledState, FALSE);
}
printf("Current logging flag: %d\n", atomic_load(&loggingEnabledState));
atomic_store(&flag_switch_logging, FALSE);
}
else if (THREAD_END_VALUE == atomic_load(&flag_switch_logging))
{
break;
}
usleep(200);
}
return NULL;
}
void *importanceHandler(void *args)
{
sigset_t set;
sigfillset(&set);
sigdelset(&set, CHANGE_IMPORTANCE_SIGNAL);
pthread_sigmask(SIG_SETMASK, &set, NULL);
while (TRUE)
{
// printf("Wciaz dzialam 2\n");
if (TRUE == atomic_load(&flag_change_importance))
{
if (MIN == atomic_load(&importanceState))
{
atomic_store(&importanceState, STD);
printf("Current logging importance is STD\n");
}
else if (STD == atomic_load(&importanceState))
{
atomic_store(&importanceState, MAX);
printf("Current logging importance is MAX\n");
}
else if (MAX == atomic_load(&importanceState))
{
atomic_store(&importanceState, MIN);
printf("Current logging importance is MIN\n");
}
atomic_store(&flag_change_importance, FALSE);
}
else if (THREAD_END_VALUE == atomic_load(&flag_change_importance))
{
break;
}
usleep(200);
}
return NULL;
}
void myLog(const int level, const char* format, ...)
{
char buffer[256];
va_list args;
va_start (args, format);
vsnprintf (buffer, 256, format, args);
va_end (args);
if (NULL == file)
{
puts("file is null\n");
}
// When this is the only saving to file at the moment
if (0 == pthread_mutex_lock(&saveToFileLock))
{
if (TRUE == atomic_load(&loggingEnabledState) && level >= atomic_load(&importanceState))
{
file = fopen(fileNameArray, "a");
if (file)
{
// Save data to file
switch (level)
{
case MIN:
fprintf(file, "MIN: ");
break;
case STD:
fprintf(file, "STD: ");
break;
case MAX:
fprintf(file, "MAX: ");
break;
}
fprintf(file, "%s\n", buffer);
}
}
// When the saving is finished
pthread_mutex_unlock(&saveToFileLock);
}
}
void setFileName(char *fileNameArray)
{
time_t currentTime = time(NULL);
struct tm timeStruct = *localtime(¤tTime);
snprintf(fileNameArray, FILE_NAME_ARRAY_LENGTH - 1, "%d-%02d-%02d %02d:%02d:%02d.log", timeStruct.tm_year + 1900,
timeStruct.tm_mon + 1, timeStruct.tm_mday, timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);
}
short init(void (*dumpFuncPtrDefinition)(void))
{
atomic_store(&importanceState, MIN);
atomic_store(&loggingEnabledState, TRUE);
atomic_store(&flag_change_importance, FALSE);
atomic_store(&flag_switch_logging, FALSE);
atomic_store(&flag_dump, FALSE);
dumpFuncPtr = dumpFuncPtrDefinition;
if (TRUE == isInitialized_bo)
{
return EXIT_FAILURE;
}
else
{
isInitialized_bo = TRUE;
}
setFileName(fileNameArray);
file = fopen(fileNameArray, "w");
// Error on creating a file
if (NULL == file)
{
return EXIT_FAILURE;
}
// Success on creating, the file will be opened again in a 'save to log' function
else
{
fclose(file);
}
if (0 != pthread_mutex_init(&saveToFileLock, NULL))
{
fclose(file);
}
pthread_create(&importanceThread, NULL, importanceHandler, NULL);
pthread_create(&switchLoggingThread, NULL, switchLoggingHandler, NULL);
pthread_create(&dumpThread, NULL, dumpHandler, NULL);
struct sigaction act;
sigset_t set;
sigfillset(&set);
act.sa_sigaction = handler_change_importance;
act.sa_mask = set;
act.sa_flags = 0;
sigaction(CHANGE_IMPORTANCE_SIGNAL, &act, NULL);
act.sa_sigaction = handler_switch_logging;
act.sa_mask = set;
act.sa_flags = 0;
sigaction(SWITCH_LOGGING_SIGNAL, &act, NULL);
act.sa_sigaction = handler_dump;
act.sa_mask = set;
act.sa_flags = 0;
sigaction(DUMP_SIGNAL, &act, NULL);
act.sa_handler = SIG_IGN;
for (int i = DUMP_SIGNAL + 1; i <= SIGRTMAX; i++)
{
sigaction(i, &act, NULL);
}
return EXIT_SUCCESS;
}
void destroy(void)
{
// while (atomic_load(&flag_change_importance) != THREAD_END_VALUE && atomic_load(&flag_switch_logging) != THREAD_END_VALUE && atomic_load(&flag_dump) != THREAD_END_VALUE)
// {
atomic_store(&flag_change_importance, THREAD_END_VALUE);
atomic_store(&flag_switch_logging, THREAD_END_VALUE);
atomic_store(&flag_dump, THREAD_END_VALUE);
// printf("flag change importance: %d, flag switch logging: %d\n", atomic_load(&flag_change_importance), atomic_load(&flag_switch_logging));
// }
pthread_join(importanceThread, NULL);
pthread_join(switchLoggingThread, NULL);
pthread_join(dumpThread, NULL);
pthread_mutex_destroy(&saveToFileLock);
}