-
Notifications
You must be signed in to change notification settings - Fork 0
/
loglib.c
174 lines (150 loc) · 3.34 KB
/
loglib.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
//resubmitting old files
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "log.h"
typedef struct list_struct {
data_t item;
struct list_struct *next;
} log_t;
static log_t *headptr = NULL;
static log_t *tailptr = NULL;
//90% same as the program 2.7 in pg44-45
//nothing special, just add a line for perror
int
addmsg (data_t data)
{
log_t *newnode;
int nodesize;
nodesize = sizeof(log_t) + strlen(data.string) + 1;
if ((newnode = (log_t *)(malloc(nodesize))) == NULL)
{
perror("loglib.c : fail to malloc a newnode\n");
return -1;
}
newnode->item.time = data.time;
newnode->item.string = (char *)newnode + sizeof(log_t);
strcpy(newnode->item.string, data.string);
newnode->next = NULL;
if ( headptr == NULL)
headptr = newnode;
else
tailptr->next = newnode;
tailptr = newnode;
return 0;
}
void
clearlog (void)
{
//decl variable
log_t *newnode = headptr;
//free the mem space
while (newnode != NULL)
{
//if(headptr->next != NULL)
{
newnode = headptr->next;
free(headptr);
headptr = newnode;
}
//else
//free(headptr);
}
}
char
*getlog (void)
{
//declare variables
//length for the sum of strings, will b used for malloc
//str to store strings for returning
log_t *newnode;
unsigned int length = 0;
char *str;
struct tm *timeinfo; //for timestamp
//assign headptr to newnode
newnode = headptr;
//check the total length size of the string (sum of)
//it will be used for malloc the str variable
while (newnode != NULL)
{
timeinfo = localtime(&newnode->item.time);
length += strlen(asctime(timeinfo)) + 1;
length += strlen(newnode->item.string) + 1;
newnode = newnode->next;
}
//mem allocation
str = (char*)malloc(length + 1);
strcpy(str, "\0");
//reset the position
newnode = headptr;
//now strcat string to the str var for returning
while (newnode != NULL)
{
timeinfo = localtime(&newnode->item.time);
strcat(str, asctime(timeinfo));
strcat(str, newnode->item.string);
strcat(str, "\n"); //add newline
newnode = newnode->next;
}
//if str is NULL, it means "something is wrong!"
//return NULL
if (str == NULL)
{
perror("loglibl.c : fail to get log msg\n");
return NULL;
}
return str;
}
int
savelog (char *filename)
{
//variable declaration
FILE *fp;
log_t *newnode;
struct tm * timeinfo;
//file open
//if opening is failed, return -1
if((fp = fopen(filename, "a")) == NULL)
{
perror("loglib.c : cannot open the log file\n");
return -1;
}
//point to head
newnode = headptr;
//append string to the file
while(newnode != NULL)
{
timeinfo = localtime (&newnode->item.time); //put time stamp
fputs(asctime(timeinfo),fp); //as human readable version
fputs("\t",fp);
fputs(newnode->item.string,fp); //put string
fputs("\n",fp);
newnode = newnode->next;
}
//close file
if(fclose(fp) != 0)
{
perror("loglib.c : fail to close the log file\n ");
return -1;
}
return 0;
}
//create log function
//this one is not in the temp
//it will receive string then put it into the queue
void
create_log(char *msg)
{
//decl variable
data_t data;
//put time into data
time (&data.time);
//mem alloc
data.string = (char*)malloc(strlen(msg) +1);
//string copy
strcpy (data.string, msg);
//now pass it into addmsg function for rest
addmsg(data);
//free memory
free(data.string);
}