-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
62 lines (50 loc) · 1.38 KB
/
Logger.cpp
File metadata and controls
62 lines (50 loc) · 1.38 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
/*
* File: Logger.cpp
* Author: Mike Curry
*
* Created on February 27, 2015, 8:56 PM
*/
#include "Logger.h"
using namespace std;
//#include"Utilities.h"
const string Logger::m_sFileName = "sockets.log";
Logger* Logger::m_pThis = NULL;
ofstream Logger::m_Logfile;
Logger::Logger() {
}
Logger* Logger::getLogger() {
if (m_pThis == NULL) {
m_pThis = new Logger();
m_Logfile.open(m_sFileName.c_str(), ios::out | ios::app);
}
return m_pThis;
}
void Logger::Log(const char * format, ...) {
char sMessage[256];
va_list args;
va_start(args, format);
vsprintf(sMessage, format, args);
m_Logfile << endl << getCurrentDateTime() << ":\t";
m_Logfile << sMessage;
va_end(args);
}
void Logger::Log(const string& sMessage) {
m_Logfile << endl << getCurrentDateTime() << ":\t";
m_Logfile << sMessage;
}
Logger& Logger::operator<<(const string& sMessage) {
m_Logfile << endl << getCurrentDateTime() << ":\t";
m_Logfile << sMessage;
return *this;
}
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const string Logger::getCurrentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof (buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}