-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLog.c
145 lines (116 loc) · 3.55 KB
/
Log.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
// Log.c
// log to file and optionally one other stream
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Log.h"
#define T Log_T
////////////////////////////////////////////////////////////////////////////////
// fail
////////////////////////////////////////////////////////////////////////////////
static void fail(char * msg) {
fprintf(stderr, "%s\n", msg);
exit(1);
}
////////////////////////////////////////////////////////////////////////////////
// Log-new
////////////////////////////////////////////////////////////////////////////////
static FILE* originalFile = NULL;
// return pointer to struct Log_T
T Log_new(char *path, FILE *also)
{
const unsigned debug = 0;
// allocate self
T self = malloc(sizeof(struct T));
if (!self) fail("Log_new: unable to allocate memory");
// initialize self field not set just below
self->also = also;
// open the log file
FILE *file = fopen(path, "w"); // erase file if already exists
if (file == NULL) {
perror("Log_new: unable to open log file ");
exit(1);
}
self->file = file;
if (debug) {
originalFile = file;
fprintf(stderr, "Log_new: self->file %p\n", (void *) self->file);
}
return self;
}
////////////////////////////////////////////////////////////////////////////////
// Log_free: close underlying file and free memory
////////////////////////////////////////////////////////////////////////////////
void Log_free(T *selfP)
{
assert(selfP);
assert(*selfP);
// close the main file
T self = *selfP;
if (fclose(self->file) != 0) fail("Log_free: unable to close log file");
// free self
free(self);
// set argument to NULL pointer
*selfP = NULL;
}
////////////////////////////////////////////////////////////////////////////////
// Log_printf
////////////////////////////////////////////////////////////////////////////////
void Log_printf(T self, const char *format, ...) {
// check arguments
assert(self);
assert(format);
// print to log file
va_list argptr;
va_start(argptr, format);
vfprintf(self->file, format, argptr);
// maybe print to auxillary file
if (self->also) {
va_start(argptr, format);
vfprintf(self->also,format, argptr);
}
}
////////////////////////////////////////////////////////////////////////////////
// Log_printfFunctionLineTime
////////////////////////////////////////////////////////////////////////////////
// see PC p. 458
void Log_printfFunctionLineTime(T self,
const char *functionName,
unsigned lineNum,
const char *format,
...)
{
const unsigned debug = 0;
// check arguments
assert(self);
assert(functionName);
assert(format);
if (debug)
assert(originalFile == self->file);
// print time stamp, function, line num
time_t timestamp = time(NULL);
fprintf(self->file, "%.8s %s (line %u): ",
ctime(×tamp)+11, functionName, lineNum);
if (self->also)
fprintf(self->also, "%.8s %s (line %u): ",
ctime(×tamp)+11, functionName, lineNum);
// print rest of message to log file
va_list argptr;
va_start(argptr, format);
assert(self->file);
if (debug) {
fprintf(stderr, "Log_printFunctionLineTime: self->file %p\n",
(void *) self->file);
// write to file directly
fprintf(self->file, "test record\n");
}
vfprintf(self->file, format, argptr); // Seg fault here, no such file
// print to auxillary file
if (self->also) {
va_list argptr;
va_start(argptr, format);
vfprintf(self->also, format, argptr);
}
}