-
Notifications
You must be signed in to change notification settings - Fork 1
/
logRegister.c
97 lines (78 loc) · 2.18 KB
/
logRegister.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
#include "logRegister.h"
int fd;
struct timespec start;
double getInstant(){
struct timespec current;
clock_gettime(CLOCK_MONOTONIC, ¤t);
return ((current.tv_sec - start.tv_sec)*1000 + (current.tv_nsec - start.tv_nsec)/1e6);
}
void initLog(){
clock_gettime(CLOCK_MONOTONIC, &start);
char *logfile;
logfile = getenv("LOG_FILENAME");
if (logfile==NULL){
logfile = "log.txt";
if(setenv("LOG_FILENAME", logfile, 1)!=0){
perror("Setenv error");
logExit(1);
}
}
fd=open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1){
perror("Error openning log file");
logExit(1);
}
}
void writeLog(double instant, char *action, char *info){
char temp[256];
sprintf(temp,"%.2f - %.8d - %s - %s\n", instant, getpid(), action, info);
write(fd, temp, strlen(temp));
}
void logArgs(int argc, char *argv[]){
char temp[256] = "";
for(int i=0;i<argc;i++){
strcat(temp, argv[i]);
if(i!=argc-1){
strcat(temp, " ");
}
}
writeLog(getInstant(), "CREATE", temp);
}
void logExit(int status){
char stat[2];
sprintf(stat, "%d", status);
writeLog(getInstant(), "EXIT", stat);
close(fd);
exit(status);
}
void logEntry(char *path, long int size){
char temp[256];
sprintf(temp, "%ld %s", size, path);
writeLog(getInstant(), "ENTRY", temp);
}
void logFork(Args args){
char temp[256] = "";
if (args.countLinks) strcat(temp,"-l ");
strcat(temp,args.path);
if (args.all) strcat(temp," -a");
if(args.bytes) strcat(temp," -b");
if (args.blockSize!=1024){
char size[16];
sprintf(size," --block-size=%d",args.blockSize);
strcat(temp,size);
}
if(args.dereference) strcat(temp," -L");
if(args.separateDirs) strcat(temp," -S");
if(args.maxDepth!=__INT64_MAX__){
char depth[16];
sprintf(depth," --max-depth=%ld",args.maxDepth);
strcat(temp,depth);
}
writeLog(getInstant(), "CREATE", temp);
}
void logRecvPipe(char *msg){
writeLog(getInstant(), "RECV_SIGNAL", msg);
}
void logSendPipe(char *msg){
writeLog(getInstant(), "SEND_SIGNAL", msg);
}