-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
56 lines (49 loc) · 1.26 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
/*******************************************************************************
* Author: Andrew Seligman
* Date: March 18, 2016
* File: logger.c
* Purpose: This file contains the implementation of logger.h. See logger.h
* for documentation.
*******************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <logger.h>
void logEvent(char* filename, char* message) {
FILE* fPtr;
if(fPtr = fopen(filename, "a")) {
writeEvent(message, fPtr);
fclose(fPtr);
}
else
perror("logger: open file");
}
void writeEvent(char* message, FILE* fPtr) {
if(fprintf(fPtr, "%.24s", getDate()) < 0) {
perror("logger: write date");
return;
}
if(fprintf(fPtr, "%s", " ") < 0) {
perror("logger: write space");
return;
}
if(fprintf(fPtr, "%s", message) < 0) {
perror("logger: write message");
return;
}
if(fprintf(fPtr, "%s", "\n") < 0)
perror("logger: write newline");
}
char* getDate() {
time_t timer;
time(&timer);
return ctime(&timer);
}
/* functionality test
int main(void) {
logEvent("test.txt", "a test message");
sleep(2);
logEvent("test.txt", "a test message 2");
return 0;
}
*/