-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
113 lines (80 loc) · 2.64 KB
/
logger.cpp
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
////////////////////////////////////////////////////////////////////////////////////////
//
// humifand - the humidity fan deamon
//
// Please check www.way2.net for more information
//
// (c) Copyright 2014 by way2.net Services.
//
////////////////////////////////////////////////////////////////////////////////////////
#include <ctime>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "logger.h"
////////////////////////////////////////////////////////////////////////////////////////
CLogger* CLogger::m_pThis = NULL;
ofstream CLogger::m_Logfile;
////////////////////////////////////////////////////////////////////////////////////////
CLogger::CLogger()
{
}
////////////////////////////////////////////////////////////////////////////////////////
CLogger* CLogger::getLogger()
{
if (m_pThis == NULL)
{
m_pThis = new CLogger();
}
return m_pThis;
}
////////////////////////////////////////////////////////////////////////////////////////
string CLogger::CurrentDateTime(void)
{
time_t t = time(0);
struct tm *now = localtime(&t);
stringstream l_ss;
l_ss << setfill('0') << setw(4) << (now->tm_year + 1900) << '-'
<< setfill('0') << setw(2) << (now->tm_mon + 1) << '-'
<< setfill('0') << setw(2) << now->tm_mday << ' '
<< setfill('0') << setw(2) << now->tm_hour << ':'
<< setfill('0') << setw(2) << now->tm_min << ':'
<< setfill('0') << setw(2) << now->tm_sec;
return l_ss.str();
}
////////////////////////////////////////////////////////////////////////////////////////
bool CLogger::OpenLog(const char *f_filename)
{
m_Logfile.open(f_filename, ios::out | ios::app );
return m_Logfile.is_open();
}
////////////////////////////////////////////////////////////////////////////////////////
void CLogger::Log( const char * format, ... )
{
char sMessage[256];
va_list args;
va_start (args, format);
vsprintf (sMessage,format, args);
m_Logfile << CurrentDateTime() << " ";
m_Logfile << sMessage;
m_Logfile << "\n";
m_Logfile.flush();
va_end (args);
}
////////////////////////////////////////////////////////////////////////////////////////
void CLogger::Log( const string& sMessage )
{
m_Logfile << CurrentDateTime() << " ";
m_Logfile << sMessage;
m_Logfile << "\n";
m_Logfile.flush();
}
////////////////////////////////////////////////////////////////////////////////////////
CLogger& CLogger::operator<<(const string& sMessage )
{
m_Logfile << CurrentDateTime() << " ";
m_Logfile << sMessage;
m_Logfile << "\n";
m_Logfile.flush();
return *this;
}