-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cpp
More file actions
executable file
·119 lines (96 loc) · 2.33 KB
/
Log.cpp
File metadata and controls
executable file
·119 lines (96 loc) · 2.33 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
/**
* This file is a part of IC Login (c) Daniel Bratell 2000
*
* That the source code is publicized on Internet does not mean that
* it's free to use. You are allowed to be inspired by it, but if you
* do patches of your own you must submit them to the original author.
*
* Also, you mustn't redistribute the source code. If you want to
* make other people see it, point them to
*
* http://www.lysator.liu.se/~bratell/iclogin/
*
* Original Author: Daniel Bratell <bratell@lysator.liu.se>
*/
// Log.cpp: implementation of the CLog class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "iclogin.h"
#include "Log.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const int CLog::LOG_ALL = 5000;
const int CLog::LOG_DUMP = 50;
const int CLog::LOG_INFO = 20;
const int CLog::LOG_WARNING = 10;
const int CLog::LOG_ERROR = 5;
const int CLog::LOG_NONE = 0;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLog::CLog()
: m_log_to_file(false), m_loglevel(CLog::LOG_INFO)
{
}
CLog::~CLog()
{
}
void CLog::SetLogLevel(int new_log_level)
{
m_loglevel = new_log_level;
}
bool CLog::SetLogFile(const CString &filename)
{
CSingleLock lock(&m_mutex, true);
if(m_logfile.m_hFile != CFile::hFileNull)
{
try
{
m_logfile.Close();
} catch(...)
{
}
m_log_to_file = false;
}
if(filename.IsEmpty())
return true;
if(m_logfile.Open(filename,
CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite |
CFile::shareDenyWrite))
{
// Closed automagically in the destructor
try
{
m_logfile.SeekToEnd();
}
catch (...)
{
TRACE("Couldn't move to end of logfile!\n");
return false;
}
m_log_to_file = true;
return true;
}
return false;
}
void CLog::Log(const CString &text, int log_level /* = LOG_INFO */)
{
if(m_loglevel < log_level)
return;
CSingleLock lock(&m_mutex, true);
CTime now = CTime::GetCurrentTime();
// CString timestring = now.Format("%Y-%m-%d %H:%M:%S %Z: ");
CString timestring = now.Format("%Y-%m-%d %H:%M:%S ");
if(m_log_to_file)
{
try {
m_logfile.WriteString(timestring+text+_T("\n"));
m_logfile.Flush();
} catch(...) {}
}
TRACE(timestring+text+_T("\n"));
}