-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cpp
132 lines (109 loc) · 2.83 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
/**********************************
* 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; // set log file opened
}
else
// compose a string stored as C string in buffer
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, "%s", buffer);
}
else{
fprintf(fp, "\n %s", stdstring);
fprintf(fp, "[%d] ", par->getcurrtime());
fprintf(fp, "%s", 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);
}