-
Notifications
You must be signed in to change notification settings - Fork 1
/
Log.cpp
260 lines (228 loc) · 6.84 KB
/
Log.cpp
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
/**********************************
* FILE NAME: Log.h
*
* DESCRIPTION: Log class definition
**********************************/
#include "Log.h"
/**
* Constructor
*/
Log::Log(Params *p) {
par = p;
firstTime = false;
}
/**
* Copy constructor
*/
Log::Log(const Log &anotherLog) {
this->par = anotherLog.par;
this->firstTime = anotherLog.firstTime;
}
/**
* Assignment Operator Overloading
*/
Log& Log::operator = (const Log& anotherLog) {
this->par = anotherLog.par;
this->firstTime = anotherLog.firstTime;
return *this;
}
/**
* Destructor
*/
Log::~Log() {}
/**
* FUNCTION NAME: LOG
*
* DESCRIPTION: Print out to file dbg.log, along with Address of node.
*/
void Log::LOG(Address *addr, const char * str, ...) {
static FILE *fp;
static FILE *fp2;
va_list vararglist;
static char buffer[30000];
static int numwrites;
static char stdstring[30];
static char stdstring2[40];
static char stdstring3[40];
static int dbg_opened=0;
if(dbg_opened != 639){
numwrites=0;
stdstring2[0]=0;
strcpy(stdstring3, stdstring2);
strcat(stdstring2, DBG_LOG);
strcat(stdstring3, STATS_LOG);
fp = fopen(stdstring2, "w");
fp2 = fopen(stdstring3, "w");
dbg_opened=639;
}
else
sprintf(stdstring, "%d.%d.%d.%d:%d ", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], *(short *)&addr->addr[4]);
va_start(vararglist, str);
vsprintf(buffer, str, vararglist);
va_end(vararglist);
if (!firstTime) {
int magicNumber = 0;
string magic = MAGIC_NUMBER;
int len = magic.length();
for ( int i = 0; i < len; i++ ) {
magicNumber += (int)magic.at(i);
}
fprintf(fp, "%x\n", magicNumber);
firstTime = true;
}
if(memcmp(buffer, "#STATSLOG#", 10)==0){
fprintf(fp2, "\n %s", stdstring);
fprintf(fp2, "[%d] ", par->getcurrtime());
fprintf(fp2, buffer);
}
else{
fprintf(fp, "\n %s", stdstring);
fprintf(fp, "[%d] ", par->getcurrtime());
fprintf(fp, buffer);
}
if(++numwrites >= MAXWRITES){
fflush(fp);
fflush(fp2);
numwrites=0;
}
}
/**
* FUNCTION NAME: logNodeAdd
*
* DESCRIPTION: To Log a node add
*/
void Log::logNodeAdd(Address *thisNode, Address *addedAddr) {
static char stdstring[100];
sprintf(stdstring, "Node %d.%d.%d.%d:%d joined at time %d", addedAddr->addr[0], addedAddr->addr[1], addedAddr->addr[2], addedAddr->addr[3], *(short *)&addedAddr->addr[4], par->getcurrtime());
LOG(thisNode, stdstring);
}
/**
* FUNCTION NAME: logNodeRemove
*
* DESCRIPTION: To log a node remove
*/
void Log::logNodeRemove(Address *thisNode, Address *removedAddr) {
static char stdstring[30];
sprintf(stdstring, "Node %d.%d.%d.%d:%d removed at time %d", removedAddr->addr[0], removedAddr->addr[1], removedAddr->addr[2], removedAddr->addr[3], *(short *)&removedAddr->addr[4], par->getcurrtime());
LOG(thisNode, stdstring);
}
/**
* FUNCTION NAME: logCreateSuccess
*
* DESCRTION: Call this function after successfully create a key value pair
*/
void Log::logCreateSuccess(Address * address, bool isCoordinator, int transID, string key, string value){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: create success at time %d, transID=%d, key=%s, value=%s", str.c_str(), par->getcurrtime(), transID, key.c_str(), value.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logReadSuccess
*
* DESCRIPTION: Call this function after successfully reading a key
*/
void Log::logReadSuccess(Address * address, bool isCoordinator, int transID, string key, string value){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: read success at time %d, transID=%d, key=%s, value=%s", str.c_str(), par->getcurrtime(), transID, key.c_str(), value.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logUpdateSuccess
*
* DESCRIPTION: Call this function after successfully updating a key
*/
void Log::logUpdateSuccess(Address * address, bool isCoordinator, int transID, string key, string newValue){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: update success at time %d, transID=%d, key=%s, value=%s", str.c_str(), par->getcurrtime(), transID, key.c_str(), newValue.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logDeleteSuccess
*
* DESCRIPTION: Call this function after successfully deleting a key
*/
void Log::logDeleteSuccess(Address * address, bool isCoordinator, int transID, string key){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: delete success at time %d, transID=%d, key=%s", str.c_str(), par->getcurrtime(), transID, key.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logCreateFail
*
* DESCRIPTION: Call this function if CREATE failed
*/
void Log::logCreateFail(Address * address, bool isCoordinator, int transID, string key, string value){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: create fail at time %d, transID=%d, key=%s, value=%s", str.c_str(), par->getcurrtime(), transID, key.c_str(), value.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logReadFail
*
* DESCRIPTION: Call this function if READ failed
*/
void Log::logReadFail(Address * address, bool isCoordinator, int transID, string key){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: read fail at time %d, transID=%d, key=%s", str.c_str(), par->getcurrtime(), transID, key.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logUpdateFail
*
* DESCRIPTION: Call this function if UPDATE failed
*/
void Log::logUpdateFail(Address * address, bool isCoordinator, int transID, string key, string newValue){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: update fail at time %d, transID=%d, key=%s, value=%s", str.c_str(), par->getcurrtime(), transID, key.c_str(), newValue.c_str());
LOG(address, stdstring);
}
/**
* FUNCTION NAME: logDeleteFail
*
* DESCRIPTION: Call this function if DELETE failed
*/
void Log::logDeleteFail(Address * address, bool isCoordinator, int transID, string key){
static char stdstring[100];
string str;
if (isCoordinator)
str = "coordinator";
else
str = "server";
sprintf(stdstring, "%s: delete fail at time %d, transID=%d, key=%s", str.c_str(), par->getcurrtime(), transID, key.c_str());
LOG(address, stdstring);
}