-
Notifications
You must be signed in to change notification settings - Fork 3
/
Log.cpp
executable file
·177 lines (145 loc) · 4.59 KB
/
Log.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
// This file is part of the VNC system.
//
// The VNC system is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the VNC system is not available from the place
// whence you received this file, check http://www.uk.research.att.com/vnc or
// contact the authors on [email protected] for information on obtaining it.
//
// Log.cpp: implementation of the Log class.
//
//////////////////////////////////////////////////////////////////////
#include "stdhdrs.h"
#include "Log.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const int Log::ToDebug = 1;
const int Log::ToFile = 2;
const int Log::ToConsole = 4;
const static int LINE_BUFFER_SIZE = 1024;
Log::Log(int mode, int level, LPTSTR filename, bool append)
{
hlogfile = NULL;
m_todebug = false;
m_toconsole = false;
m_tofile = false;
SetMode(mode);
if (mode & ToFile) {
SetFile(filename, append);
}
}
void Log::SetMode(int mode) {
if (mode & ToDebug)
m_todebug = true;
else
m_todebug = false;
if (mode & ToFile) {
m_tofile = true;
} else {
CloseFile();
m_tofile = false;
}
#ifdef _WIN32_WCE
m_toconsole = false;
#else
if (mode & ToConsole) {
if (!m_toconsole)
AllocConsole();
m_toconsole = true;
} else {
m_toconsole = false;
}
#endif
}
void Log::SetLevel(int level) {
m_level = level;
}
void Log::SetFile(LPTSTR filename, bool append)
{
// if a log file is open, close it now.
CloseFile();
m_tofile = true;
// If filename is NULL or invalid we should throw an exception here
hlogfile = CreateFile(
filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hlogfile == INVALID_HANDLE_VALUE) {
// We should throw an exception here
m_todebug = true;
m_tofile = false;
Print(0, _T("Error opening log file %s\n"), filename);
}
if (append) {
SetFilePointer( hlogfile, 0, NULL, FILE_END );
} else {
SetEndOfFile( hlogfile );
}
}
// if a log file is open, close it now.
void Log::CloseFile() {
if (hlogfile != NULL) {
CloseHandle(hlogfile);
hlogfile = NULL;
}
}
#ifndef UNDER_CE
// Non-CE version
void Log::ReallyPrint(LPTSTR format, va_list ap)
{
TCHAR line[LINE_BUFFER_SIZE];
_vstprintf(line, format, ap);
if (m_todebug) OutputDebugString(line);
if (m_toconsole) {
DWORD byteswritten;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), line, _tcslen(line)*sizeof(TCHAR), &byteswritten, NULL);
};
if (m_tofile && (hlogfile != NULL)) {
DWORD byteswritten;
WriteFile(hlogfile, line, _tcslen(line)*sizeof(TCHAR), &byteswritten, NULL);
}
}
#else
// CE version
void Log::ReallyPrint(LPTSTR format, va_list ap)
{
TCHAR line[LINE_BUFFER_SIZE];
_vstprintf(line, format, ap);
if (m_todebug) OutputDebugString(line);
if (m_tofile && (hlogfile != NULL)) {
DWORD byteswritten;
// Log file is more readable if non-unicode!
char ansiline[LINE_BUFFER_SIZE];
int origlen = _tcslen(line);
int newlen = WideCharToMultiByte(
CP_ACP, // code page
0, // performance and mapping flags
line, // address of wide-character string
origlen, // number of characters in string
ansiline, // address of buffer for new string
255, // size of buffer
NULL, NULL );
WriteFile(hlogfile, ansiline, newlen, &byteswritten, NULL);
}
}
#endif
Log::~Log()
{
CloseFile();
}
Log theLog;