forked from OFLOPS-Turbo/oflops-turbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
63 lines (54 loc) · 1.27 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
#include "log.h"
const char *msg_type[] = {
"OFPT_FLOW_MOD_ADD",
"OFPT_STATS_REQUEST_FLOW",
"OFPT_STATS_REPLY_FLOW",
"OFPT_STATS_REQUEST_FLOW",
"OFPT_STATS_REPLY_PORT",
"OFPT_HELLO",
"OFPT_ECHO_REPLY",
"OFPT_ECHO_REQUEST",
"OFPT_ERROR",
"GENERIC_MSG",
"SNMP_MSG",
"PCAP_MSG",
"OFPT_PACKET_IN_MSG",
"PKTGEN_MSG"
};
FILE* logger;
/**
* Initializes the logging system of oflops.
* \param filename The file where the logging messages are stored.
*/
void
oflops_log_init(const char *filename) {
//openning the logging file
logger = fopen(filename, "w");
if(logger == NULL) {
perror_and_exit("failed to open log file", 1);
}
}
/**
* log message on the logging file
* \param ts a timestamp of the logging event
* \param type the type of the logging event
* \param details the string to be appented on the logging file
*/
void
oflops_log(struct timeval ts, int type, const char *details) {
if (!logger)
return;
//print the log message
fprintf(logger, "%lu.%06lu:%s:%s\n",(long unsigned int)ts.tv_sec,
(long unsigned int)ts.tv_usec, msg_type[type], details);
//force system to print the line on the file
fflush(logger);
}
/**
* Close the logging system of oflops.
*/
void
oflops_log_close() {
if(logger)
fclose(logger);
}