-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.c
86 lines (67 loc) · 2.02 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
/**********************
*
* Progam Name: MP1. Membership Protocol.
*
* Current file: log.c
* About this file: Debug logging.
*
***********************/
#include "log.h"
#define MAXWRITES 1 /* num. of writes after which to flush file. */
extern long getcurrtime();
/*
Print out to file dbg.log, along with address of node.
*/
void LOG(address *addr, 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, "a+");
fp2 = fopen(stdstring3, "a+");
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(memcmp(buffer, "#STATSLOG#", 10)==0){
fprintf(fp2, "\n %s", stdstring);
fprintf(fp2, "[%ld] ", getcurrtime());
fprintf(fp2, buffer);
}
else{
fprintf(fp, "\n %s", stdstring);
fprintf(fp, "[%ld] ", getcurrtime());
fprintf(fp, buffer);
}
if(++numwrites >= MAXWRITES){
fflush(fp);
fflush(fp2);
numwrites=0;
}
}
void logNodeAdd(address *thisNode, address *addedAddr)
{
static char stdstring[30];
sprintf(stdstring, "Node %d.%d.%d.%d:%d joined", addedAddr->addr[0], addedAddr->addr[1], addedAddr->addr[2], addedAddr->addr[3], *(short *)&addedAddr->addr[4]);
LOG(thisNode, stdstring);
}
void logNodeRemove(address *thisNode, address *removedAddr)
{
static char stdstring[30];
sprintf(stdstring, "Node %d.%d.%d.%d:%d removed", removedAddr->addr[0], removedAddr->addr[1], removedAddr->addr[2], removedAddr->addr[3], *(short *)&removedAddr->addr[4]);
LOG(thisNode, stdstring);
}