forked from ImpulseAdventure/JPEGsnoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocLog.cpp
314 lines (277 loc) · 7.52 KB
/
DocLog.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// JPEGsnoop - JPEG Image Decoder & Analysis Utility
// Copyright (C) 2017 - Calvin Hass
// http://www.impulseadventure.com/photo/jpeg-snoop.html
//
// This program 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, see <http://www.gnu.org/licenses/>.
//
#include "StdAfx.h"
#include ".\doclog.h"
#include "JPEGsnoopDoc.h"
//
// Initialize the log
//
CDocLog::CDocLog(void)
{
m_pDoc = NULL;
m_bEn = true;
// Default to local log
m_bUseDoc = false;
// Start in quick mode
#ifdef QUICKLOG
m_bLogQuickMode = true;
#else
m_bLogQuickMode = false;
#endif
}
CDocLog::~CDocLog(void)
{
}
// Enable logging
void CDocLog::Enable() {
m_bEn = true;
};
// Disable logging
void CDocLog::Disable() {
m_bEn = false;
};
// Enable or disable the quick log mode
//
// INPUT:
// - bQuick = If true, write to log buffer, if false, write to CDocument
//
void CDocLog::SetQuickMode(bool bQuick)
{
m_bLogQuickMode = bQuick;
}
// Get the current quick log mode
//
// RETURN:
// - Are we in quick mode?
//
bool CDocLog::GetQuickMode()
{
return m_bLogQuickMode;
}
void CDocLog::Clear()
{
m_saLogQuickTxt.RemoveAll();
m_naLogQuickCol.RemoveAll();
}
// Establish linkage between log and the CDocument
// Passing NULL will force use of local log instead
void CDocLog::SetDoc(CDocument* pDoc)
{
m_pDoc = pDoc;
if (pDoc) {
// Use RichEditCtrl for the log
m_bUseDoc = true;
// Empty the buffer
Clear();
} else {
// Use local log
m_bUseDoc = false;
}
}
// Add a basic text line to the log
void CDocLog::AddLine(CString strTxt)
{
COLORREF sCol;
if (m_bEn) {
sCol = RGB(1, 1, 1);
// TODO: Do I really need newline in these line outputs?
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// Add a header text line to the log
void CDocLog::AddLineHdr(CString strTxt)
{
COLORREF sCol = RGB(1, 1, 255);
if (m_bEn) {
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// Add a header description text line to the log
void CDocLog::AddLineHdrDesc(CString strTxt)
{
COLORREF sCol = RGB(32, 32, 255);
if (m_bEn) {
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// Add a warning text line to the log
void CDocLog::AddLineWarn(CString strTxt)
{
COLORREF sCol = RGB(128, 1, 1);
if (m_bEn) {
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// Add an error text line to the log
void CDocLog::AddLineErr(CString strTxt)
{
COLORREF sCol = RGB(255, 1, 1);
if (m_bEn) {
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// Add a "good" indicator text line to the log
void CDocLog::AddLineGood(CString strTxt)
{
COLORREF sCol = RGB(16, 128, 16);
if (m_bEn) {
if (m_bUseDoc) {
if (m_bLogQuickMode) {
AppendToLogLocal(strTxt+_T("\n"),sCol);
} else {
CJPEGsnoopDoc* pSnoopDoc = (CJPEGsnoopDoc*)m_pDoc;
pSnoopDoc->AppendToLog(strTxt+_T("\n"),sCol);
}
} else {
AppendToLogLocal(strTxt+_T("\n"),sCol);
}
}
}
// ======================================================================
unsigned CDocLog::AppendToLogLocal(CString strTxt, COLORREF sColor)
{
// Don't exceed a realistic maximum!
unsigned numLines = m_saLogQuickTxt.GetCount();
if (numLines == DOCLOG_MAX_LINES) {
m_saLogQuickTxt.Add(_T("*** TOO MANY LINES IN REPORT -- TRUNCATING ***"));
m_naLogQuickCol.Add((unsigned)sColor);
return 0;
} else if (numLines > DOCLOG_MAX_LINES) {
return 0;
}
m_saLogQuickTxt.Add(strTxt);
m_naLogQuickCol.Add((unsigned)sColor);
return 0;
}
// Get the number of lines in the local log or quick buffer
unsigned CDocLog::GetNumLinesLocal()
{
return m_saLogQuickTxt.GetCount();
}
// Fetch a line from the local log
// Returns false if line number is out of range
bool CDocLog::GetLineLogLocal(unsigned nLine,CString &strOut,COLORREF &sCol)
{
unsigned numLines = m_saLogQuickTxt.GetCount();
if (nLine >= numLines) {
return false;
}
strOut = m_saLogQuickTxt[nLine];
sCol = m_naLogQuickCol[nLine];
return true;
}
// Save the current log to text file with a simple implementation
//
// - This routine is implemented with a simple output mechanism rather
// than leveraging CRichEditCtrl::DoSave() so that we can perform
// this operation in command-line mode or batch mode operations.
//
void CDocLog::DoLogSave(CString strLogName)
{
CStdioFile* pLog;
// Open the file for output
ASSERT(strLogName != _T(""));
// OLD COMMENTS FOLLOW
// This save method will only work if we were in Quick Log mode
// where we have recorded the log to a string buffer and not
// directly to the RichEdit
// Note that m_bLogQuickMode is only toggled on during processing
// so we can't check the status here (except seeing that there are no
// lines in the array)
// TODO: Ensure file doesn't exist and only overwrite if specified in command-line?
// TODO:
// Confirm that we are not writing to the same file we opened (m_strPathName)
// ASSERT(strLogName != m_strPathName);
try
{
// Open specified file
pLog = new CStdioFile(strLogName, CFile::modeCreate| CFile::modeWrite | CFile::typeText | CFile::shareDenyNone);
}
catch (CFileException* e)
{
TCHAR msg[MAX_BUF_EX_ERR_MSG];
CString strError;
e->GetErrorMessage(msg,MAX_BUF_EX_ERR_MSG);
e->Delete();
strError.Format(_T("ERROR: Couldn't open file for write [%s]: [%s]"),
(LPCTSTR)strLogName, (LPCTSTR)msg);
// FIXME: Find an alternate method of signaling error in command-line mode
AfxMessageBox(strError);
pLog = NULL;
return;
}
// Step through the current log buffer
CString strLine;
COLORREF sCol;
unsigned nQuickLines = GetNumLinesLocal();
for (unsigned nLine=0;nLine<nQuickLines;nLine++)
{
GetLineLogLocal(nLine,strLine,sCol);
pLog->WriteString(strLine);
}
// Close the file
if (pLog) {
pLog->Close();
delete pLog;
}
}