-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpccLog.cpp
More file actions
376 lines (287 loc) · 11.6 KB
/
io.cpccLog.cpp
File metadata and controls
376 lines (287 loc) · 11.6 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* *****************************************
* File: cpccLog.cpp
* Version: see function getClassVersion()
* Purpose: Portable (cross-platform), light-weight library
* to write application information to a log file
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2014 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "io.cpccLog.h"
#include "io.cpccFileSystemMini.h"
#include "fs.cpccPathHelper.h"
#include "cpcc_SelfTest.h"
#include "core.cpccIdeMacros.h"
#include "core.cpccTryAndCatch.h"
#include "io.cpccLogFileWriterWithBuffer.h"
#include "fs.cpccUserFolders.h"
#define cpccLogOpeningStamp _T("cpccLog starting")
#define cpccLogClosingStamp _T("cpccLog closing. Bye bye...")
int cpccLogFormatter::m_IdentLevel = 0;
bool cpccLogFormatter::m_enabled(true);
///////////////////////////////////////////////////////////////////////////////
//
// class cpccLogFormatter
//
///////////////////////////////////////////////////////////////////////////////
cpccLogFormatter::cpccLogFormatter(const cpcc_char *aTag, const bool disableIfFileDoesNotExist, const bool echoToConsole) :
m_tag(aTag ? aTag : _T("NULL aTag ")),
//m_IdentText(_T("| ")),
m_disableIfFileDoesNotExist(disableIfFileDoesNotExist),
m_echoToConsole(echoToConsole),
m_isEmpty(true)
{
// std::cout << "creating cpccLogFormatter with tag:" << m_tag << std::endl;
if (m_tag.length() == 0)
std::cerr << "Error: #5613a: m_tag.length()==0" << std::endl;
}
void cpccLogFormatter::add(const cpcc_char* txt)
{
if (!txt)
{
std::cerr << "cpccLogFormatter::add(NULL)" << std::endl;
return;
}
// todo: reorganize the sequence of tasks here.
// Consider if logs should be kept in memory while !m_enabled
if (!m_enabled)
return;
cpcc_string _fn(cpccLogFileWriterWithBuffer::getInstance().getFilename());
if ((_fn.length()>0) && !cpccFileSystemMini::fileExists(_fn.c_str()) && (!m_disableIfFileDoesNotExist))
cpccFileSystemMini::createEmptyFile(_fn.c_str()); // create a file so that the log can continue wrting on it.
cpcc_string m_outputBuffer;
if (moreThanOneSecondPassed())
{
m_outputBuffer.append( getCurrentTime(_T(" \t%F %X")) );
m_outputBuffer.append(_T("\n"));
}
// Error here under WinXP
if (! (m_tag.length()>0) )
std::cerr << "Error: #5613b: !(m_tag.length()>0) with text=" << txt << std::endl;
m_outputBuffer.append(m_tag);
for (int i = 0; i<m_IdentLevel; ++i)
m_outputBuffer.append(m_IdentText);
m_outputBuffer.append(txt);
m_outputBuffer.append(_T("\n"));
// cpccFileSystemMini::appendTextFile(m_filename, m_outputBuffer);
cpccLogFileWriterWithBuffer::getInstance().add(m_outputBuffer.c_str());
if (m_echoToConsole)
{
cpcc_cout << m_outputBuffer;
#if defined(_WIN32)
OutputDebugString(m_outputBuffer.c_str());
#endif
}
m_isEmpty = false;
}
cpcc_string cpccLogFormatter::toString(const cpcc_char* format, ...)
{
const int MAX_LOG_STRING = 8000;
cpcc_char buff[MAX_LOG_STRING + 1];
va_list args;
va_start(args, format);
#if (_MSC_VER >= 1400) // Visual Studio 2005
// vsprintf_s( buff, MAX_LOG_STRING, format, args);
CPCC_TRY_AND_CATCH_TO_CERR(_vstprintf_s(buff, MAX_LOG_STRING, format, args), "_vstprintf_s(buff, MAX_LOG_STRING, format, args)");
#else
CPCC_TRY_AND_CATCH_TO_CERR( vsprintf(buff, format, args) , "vsprintf(buff, format, args)");
#endif
va_end(args);
return buff;
}
#ifdef _WIN32
#ifdef UNICODE
void cpccLogFormatter::addf(const wchar_t* format, ...)
{
// todo: Use toString()
const int MAX_LOG_STRING = 8000;
wchar_t buff[MAX_LOG_STRING + 1]; // = { 0 };
va_list args;
va_start(args, format);
#if (_MSC_VER >= 1400) // Visual Studio 2005
// vsprintf_s( buff, MAX_LOG_STRING, format, args);
_vstprintf_s(buff, MAX_LOG_STRING, format, args);
#else
vsprintf(buff, format, args);
#endif
va_end(args);
add(buff);
}
#endif
#endif
void cpccLogFormatter::addf(const char* format, ...)
{
// todo: Use toString()
const int MAX_LOG_STRING = 8000 ;
char buff[MAX_LOG_STRING + 1]; // = { 0 };
va_list args;
va_start(args, format);
#if (_MSC_VER >= 1400) // Visual Studio 2005
// vsprintf_s( buff, MAX_LOG_STRING, format, args);
// _vstprintf_s for automatic unicode/ non-unicode
CPCC_TRY_AND_CATCH_TO_CERR( vsprintf_s(buff, MAX_LOG_STRING, format, args), "vsprintf_s(buff, MAX_LOG_STRING, format, args)");
// vsprintf_s(buff, MAX_LOG_STRING, format, args);
#else
CPCC_TRY_AND_CATCH_TO_CERR( vsprintf(buff, format, args), "vsprintf(buff, format, args)");
#endif
va_end(args);
add(buff);
}
const cpcc_string & cpccLogFormatter::getFilename(void)
{
return cpccLogFileWriterWithBuffer::getInstance().getFilename();
}
bool cpccLogFormatter::moreThanOneSecondPassed(void) // this is used to avoid writing the time for every log line
{
static time_t previousLocalEpoch=0;
// The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC
time_t localEpoch = time(NULL);
if (localEpoch==previousLocalEpoch)
return false;
previousLocalEpoch=localEpoch;
return true;
}
/// Get the current datetime as a human readable string
/**
This function is used to create the timestamp field of the logRecord
@param fmt the format specifier according to C++ strftime()
@return the current datetime formatted with the standard function strftime
*/
cpcc_string cpccLogFormatter::getCurrentTime(const cpcc_char * fmt)
{
time_t t = time(0); // get time now
#pragma warning( disable : 4996 )
struct tm timeStruct = *localtime(&t);
cpcc_char buffer[100];
// More information about date/time format
// http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// strftime(buffer, sizeof(buffer), fmt, &timeStruct);
cpcc_strftime(buffer, sizeof(buffer), fmt, &timeStruct);
return buffer; // e.g. 13:44:04
}
///////////////////////////////////////////////////////////////////////////////
//
// class cpccLogManager
//
///////////////////////////////////////////////////////////////////////////////
// constructor
cpccLogManager::cpccLogManager(void):
error(_T("ERROR>\t"), !config_CreateFileOnError, config_EchoToCOUT),
warning(_T("Warning>\t"), !config_CreateFileOnWarning, config_EchoToCOUT),
info(_T("Info>\t"), !config_CreateFileOnInfo, config_EchoToCOUT)
{
#ifdef cpccDEBUG
cpcc_cout << _T("cpccLogManager constructor\n");
#endif
info.add(cpccLogOpeningStamp);
#ifdef cpccDEBUG
info.add(_T("Compiled in DEBUG mode"));
#else
info.add(_T("Compiled in Release mode"));
#endif
info.add(_T( "Application build timestamp:" ) __DATE__ _T(" ") __TIME__);
info.add(_T("More info about the cpcc cross platform library at:\n \twww.StarMessageSoftware.com/cpcclibrary"));
#ifdef cpccDEBUG
info.add(cpccFileSystemMini::getFileSystemReport());
#endif
}
cpccLogManager::~cpccLogManager() // in MSVC, this destructor is not called
{
//cpccFileSystemMini::appendTextFile("c:\\tmp\\a.txt", cpcc_string("this is the end"));
info.add(cpccLogClosingStamp);
if (hasErrors())
copyToDesktop();
}
cpccLogManager &cpccLogManager::getInst(void)
{
// static cpccLogManager _instance; // this does not work on WinXP inside a dll
// std::cout << "getLogManagerSingleton()" << std::endl;
static cpccLogManager *_instancePtr=NULL;
// std::cout << "getLogManagerSingleton() step2" << std::endl;
if (!_instancePtr)
_instancePtr = new cpccLogManager;
return *_instancePtr;
}
void cpccLogManager::initialize(const cpcc_char *appNameStem, const cpcc_char *macBundleId, const bool checkForIncompleteLog)
{
// info.addf("cpccLogManager::initialize() called");
const cpcc_char *bundleID = NULL; // ignored in Windows
#ifdef __APPLE__
bundleID = macBundleId;
#endif
info.addf("Compiler C/C++ standard:%s", cppcIDE::getCompilerVersion());
cpcc_string fn = getAutoFullpathFilename(appNameStem, bundleID);
// check previous run
if ((checkForIncompleteLog && logfileIsIncomplete(fn.c_str()))
||
(config_CheckIfLogHasErrors && cpccFileSystemMini::fileContainsText(fn.c_str(), _T("ERROR>\t")))
)
copyToDesktop();
// empty the file
if (cpccFileSystemMini::fileExists(fn.c_str()))
cpccFileSystemMini::createEmptyFile(fn.c_str());
cpccLogFileWriterWithBuffer::getInstance().setFilename(fn.c_str());
consolePut(_T("Log filename:") << fn);
info.addf(_T("Log filename:%s"), fn.c_str());
cpcc_string appfilename(cpccFileSystemMini::getAppFullPathFilename());
consolePut(_T("c Initialize with App filename:") << appfilename);
info.addf(_T("Initialize with App filename:%s"), appfilename.c_str());
if (!cpccFileSystemMini::fileExists(fn.c_str()))
consolePut(_T("Disabling log becase file does not exist at:") << fn.c_str());
}
cpcc_string cpccLogManager::getFolderForTheLogFile(const cpcc_char *aBundleID)
{
cpccPathString result(cpccUserFolders::getUsersCacheDir());
if (aBundleID)
if (cpcc_strlen( aBundleID)>1)
result.appendPathSegment(aBundleID);
if (!result.pathExists())
{
bool folderCreated = false;
CPCC_TRY_AND_CATCH_TO_CERR(folderCreated = cpccFileSystemMini::createFolder(result.c_str()), "getFolderForTheLogFile()");
if (!folderCreated)
cpcc_cerr << _T("#8672: getFolderForTheLogFile() could not create folder:") << result << _T("\n");
}
return std::move(result);
}
cpcc_string cpccLogManager::getAutoFullpathFilename(const cpcc_char *aFilename, const cpcc_char *aBundleID)
{
cpccPathString result(getFolderForTheLogFile(aBundleID));
if (aFilename)
result.appendPathSegment(aFilename);
else // chose an automatic filename
result.appendPathSegment(cpccFileSystemMini::getAppFilename().c_str());
result.append(_T(".cpccLog.txt"));
return std::move(result);
}
bool cpccLogManager::logfileIsIncomplete(const cpcc_char *fn)
{
return (cpccFileSystemMini::fileContainsText(fn, cpccLogOpeningStamp) && !cpccFileSystemMini::fileContainsText(fn, cpccLogClosingStamp));
}
void cpccLogManager::copyToDesktop(void)
{
const cpcc_char *fn = cpccLogFileWriterWithBuffer::getInstance().getFilename().c_str();
if (!cpccFileSystemMini::fileExists(fn))
return;
cpccFileSystemMini::copyFile(fn, cpccUserFolders::getDesktop().c_str());
}
/////////////////////////////////////////////////////////////////////////
// inside your program use the following functions to write to the 3 levels of the logs.
// all the 3 logs point to the same file.
/*
cpccLogFormatter &infoLog(void) { return cpccLogManager::getInst().info; }
cpccLogFormatter &warningLog(void) { return cpccLogManager::getInst().warning; }
cpccLogFormatter &errorLog(void) { return cpccLogManager::getInst().error; }
*/
cpccLogFormatter &infoLog(void) { return cpccLogManager::getInfo(); }
cpccLogFormatter &warningLog(void) { return cpccLogManager::getWarning(); }
cpccLogFormatter &errorLog(void) { return cpccLogManager::getError(); }