-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.cpp
More file actions
52 lines (46 loc) · 1.11 KB
/
logger.cpp
File metadata and controls
52 lines (46 loc) · 1.11 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
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string>
using namespace std;
//Current date and time, YYYY-MM-DD.HH:mm:ss
const string currentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
long long getmsofday()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (long long)tv.tv_sec*1000 + tv.tv_usec/1000;
}
class Logger {
long long time_start;
string text_file;
public:
Logger ()
{
time_start = getmsofday();
text_file = "logs/" + currentDateTime() + ".txt";
ofstream logging;
//logging.open(text_file.c_str());
//logging.close();
}
void log (string name, string val)
{
ofstream logging;
logging.open(text_file.c_str(), ios_base::app);
long long time_current = getmsofday();
logging << time_current - time_start << " " << name << ": " << val << endl;
cout << time_current - time_start << " " << name << ": " << val << endl;
//logging.close();
}
} ;