forked from mit-ll/LL-DLEP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlepLogger.cpp
More file actions
124 lines (111 loc) · 2.32 KB
/
DlepLogger.cpp
File metadata and controls
124 lines (111 loc) · 2.32 KB
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
/*
* Dynamic Link Exchange Protocol (DLEP)
*
* Copyright (C) 2013 Massachusetts Institute of Technology
*/
#include "DlepLogger.h"
#include <time.h>
#include <ctype.h>
using namespace std;
using namespace LLDLEP::internal;
DlepLogger::DlepLogger()
{
this->run_level = DLEP_LOG_INFO;
//this->run_level = DEBUG;
level_name[1] = string("DEBUG: ");
level_name[2] = string("INFO: ");
level_name[3] = string("NOTICE: ");
level_name[4] = string("ERROR: ");
level_name[5] = string("FATAL: ");
file_name = "/tmp/dlep_log.txt";
logfile.open("/tmp/dlep_log.txt");
}
DlepLogger::DlepLogger(int run_level)
{
if (run_level < DLEP_LOG_DEBUG)
{
this->run_level = DLEP_LOG_DEBUG;
}
else if (run_level > DLEP_LOG_FATAL)
{
this->run_level = DLEP_LOG_FATAL;
}
else
{
this->run_level = run_level;
}
logfile.open("dlep_log.txt");
}
DlepLogger::~DlepLogger()
{
logfile.close();
}
void
DlepLogger::log(int level, std::string str)
{
boost::mutex::scoped_lock lock(mutex);
if (level >= run_level)
{
logfile << level_name[level] << str << endl;
}
}
void
DlepLogger::log(int level, std::ostringstream & msg)
{
log(level, msg.str());
msg.str("");
}
void
DlepLogger::log_time(int level, std::string str)
{
boost::mutex::scoped_lock lock(mutex);
if (level >= run_level)
{
logfile << time_string_get() << level_name[level] << str << endl;
}
}
void
DlepLogger::log_time(int level, std::ostringstream & msg)
{
log_time(level, msg.str());
msg.str("");
}
void
DlepLogger::set_run_level(int run_level)
{
if (run_level < DLEP_LOG_DEBUG)
{
this->run_level = DLEP_LOG_DEBUG;
}
else if (run_level > DLEP_LOG_FATAL)
{
this->run_level = DLEP_LOG_FATAL;
}
else
{
this->run_level = run_level;
}
}
void
DlepLogger::set_log_file(const char * name)
{
file_name = name;
logfile.close();
logfile.open(name);
}
string
DlepLogger::time_string_get()
{
char buf[128];
timeval tp;
gettimeofday(&tp, 0);
time_t tim = time(NULL);
tm now;
localtime_r(&tim, &now);
snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%03ld ",
now.tm_hour,
now.tm_min,
now.tm_sec,
tp.tv_usec / 1000L);
return string(buf);
}