aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lipi-toolkit/3rdparty/lipi-toolkit/src/util/logger/LTKLogger.cpp
blob: 75ad218ea75740eeede130520871f6085b3fc377 (plain)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*****************************************************************************************
* Copyright (c) 2007 Hewlett-Packard Development Company, L.P.
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all 
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************************/

/************************************************************************
 * SVN MACROS
 *
 * $LastChangedDate: 2008-01-10 14:36:54 +0530 (Thu, 10 Jan 2008) $
 * $Revision: 353 $
 * $Author: charakan $
 *
 ************************************************************************/
/************************************************************************
 * FILE DESCR: Implementation of the Debug Logging Module
 *
 * CONTENTS:
 *
 * AUTHOR:     Balaji R.
 *
 * DATE:       December 23, 2004
 * CHANGE HISTORY:
 * Author		Date			Description of change
 ************************************************************************/

#include "LTKLogger.h"
#include "LTKMacros.h"
#include "LTKErrorsList.h"
#include "LTKOSUtil.h"
#include "LTKOSUtilFactory.h"

LTKLoggerInterface* LTKLogger::loggerInstance = NULL;

/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: LTKLogger
* DESCRIPTION	: Default Constructor
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

LTKLogger::LTKLogger():
m_debugLevel(LTKLogger::LTK_LOGLEVEL_ERR),
m_logFileName(DEFAULT_LOG_FILE),
m_logStatus(INACTIVE),
m_isTimeStamped(true)
{
}

/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: LTKLogger
* DESCRIPTION	: destructor
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

LTKLogger::~LTKLogger()
{
	stopLog();
}

/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: operator()
* DESCRIPTION	: overloaded function call operator
* ARGUMENTS		: msgDebugLevel - log level of the messages to be logged
* RETURNS		: reference to an output stream object
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

ostream& LTKLogger::operator()(const EDebugLevel& msgDebugLevel, 
                               const string& fileName, 
                               int lineNumber)
{
	if ( m_debugLevel <= msgDebugLevel )
	{
		writeAuxInfo(fileName, lineNumber);
        
		switch ( msgDebugLevel )
		{

			case LTKLogger::LTK_LOGLEVEL_ALL:
											m_logFile << "[All] ";
											break;
			
			case LTKLogger::LTK_LOGLEVEL_VERBOSE:
											m_logFile << "[Verbose] ";
											break;
			case LTKLogger::LTK_LOGLEVEL_DEBUG:
											m_logFile << "[Debug] ";
											break;

			case LTKLogger::LTK_LOGLEVEL_INFO:
											m_logFile << "[Info] ";
											break;

			case LTKLogger::LTK_LOGLEVEL_ERR:
											m_logFile << "[Error] ";
											break;
		}
        
		m_logFile.flush();

        return m_logFile;
	}

    // if m_debugLevel is higher than the message importance,
	// then don't attempt to process it's input
	return m_ofstream;
}

/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: writeAuxInfo
* DESCRIPTION	: writes the data, time, file, line no. information into the log file
* ARGUMENTS		: fileName - name of file containing the log message
*				  lineNumber - line number of the log message
* RETURNS		: SUCCESS on successful writing of the auxiliary information
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

int LTKLogger::writeAuxInfo(const string& fileName, int lineNumber)
{

	#ifdef LOGGEROFF
	return FAILURE;
	#endif

	if(m_isTimeStamped)
	{
		LTKOSUtil *pOSUtil = LTKOSUtilFactory::getInstance() ;
		
		string formattedTimeStr ;
		pOSUtil->getSystemTimeString(formattedTimeStr) ;
		m_logFile << formattedTimeStr << ' ';
		delete pOSUtil ;

		/*
		time_t rawtime;

		time(&rawtime);

		string timeStr = ctime(&rawtime);

		string formattedTimeStr = timeStr.substr(0, 24);

		m_logFile << formattedTimeStr << ' ';
		*/
	}

	m_logFile << fileName.substr(fileName.find_last_of(SEPARATOR) + 1, fileName.size());

	if(lineNumber != 0)
	{
		m_logFile << '(' << lineNumber << "): ";
	}

	return SUCCESS;
}


/***************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: setLogLevel
* DESCRIPTION	: sets the level of the messages to be logged
* ARGUMENTS		: debugLevel - level of the messages to be logged
* RETURNS		: SUCCESS on successful set operation
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
****************************************************************************/

int LTKLogger::setLogLevel(const EDebugLevel& debugLevel)
{
	#ifdef LOGGEROFF
	return FAILURE;
	#endif

	m_debugLevel = debugLevel;

	return SUCCESS;
}

/****************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: setLogFileName
* DESCRIPTION	: sets the name of the file to be used for logging
* ARGUMENTS		: logFileName - name of the file to be used for logging
* RETURNS		: Nothing
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
* Balaji MNA        07-Apr-2009         To fix bug:	LIPTKT-600
*****************************************************************************/

void LTKLogger::setLogFileName(const string& logFileName)
{

	#ifdef LOGGEROFF
	    return;
	#endif

	m_logStatus = INACTIVE;
	m_logFileName = logFileName;
}

/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: getLogLevel
* DESCRIPTION	: returns the log of messages to be logged
* ARGUMENTS		:
* RETURNS		: log of messages to be logged
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

LTKLogger::EDebugLevel LTKLogger::getLogLevel()
{
	#ifdef LOGGEROFF
	return string("Logger is off");
	#endif
    return m_debugLevel;
}

/*****************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: getLogFileName
* DESCRIPTION	: returns the name of the log file
* ARGUMENTS		:
* RETURNS		: name of the log file
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
******************************************************************************/

const string& LTKLogger::getLogFileName()
{
	return m_logFileName;
}

/****************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: startLog
* DESCRIPTION	: starts the logging operation
* ARGUMENTS		:
* RETURNS		: SUCCESS on successful start of logging
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
****************************************************************************/

int LTKLogger::startLog(bool isTimeStamped)
{
    
	if (m_debugLevel == LTK_LOGLEVEL_OFF )
        return FAILURE;

	if(m_logStatus == INACTIVE)
	{
		m_isTimeStamped = isTimeStamped;

		if(m_logFileName != "")
		{
			m_logFile.close();

			m_logFile.clear();

			m_logFile.open(m_logFileName.c_str(), ios::app);
		}

		if(m_logFileName == "" || !m_logFile)
		{
			//	log the non-existance of m_logFileName file path is unable to open m_logFileName
		
			return (ELOG_FILE_NOT_EXIST);			

			m_logFile.close();

			m_logFile.clear();

			m_logFile.open(m_logFileName.c_str(), ios::app);
		}

		m_logStatus = ACTIVE;
	}

	return SUCCESS;
}

/****************************************************************************
* AUTHOR		: Nidhi sharma
* DATE			: 12-SEPT-2005
* NAME			: getInstance
* DESCRIPTION	: This file will return the address of the Logger instance.
* ARGUMENTS		:
* RETURNS		: Address of the Logger instance.
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
* Thanigai			28-NOV-05			returning the address of global Logger object
*										is giving runtime error, because the vfptr is not
*										getting initialized(vtable). So implementing the
*										workaround by allocating a new Logger object runtime.
*****************************************************************************/

LTKLoggerInterface* LTKLogger::getInstance()
{
	if(loggerInstance == NULL)
	{
		loggerInstance = new LTKLogger();
	}

	return loggerInstance;

}

/****************************************************************************
* AUTHOR		: Nidhi sharma
* DATE			: 12-SEPT-2005
* NAME			: destroyLoggerInstance
* DESCRIPTION	: 
* ARGUMENTS		:
* RETURNS		: 
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*****************************************************************************/
void LTKLogger::destroyLoggerInstance()
{
    if (loggerInstance != NULL)
    {
        delete loggerInstance;
        loggerInstance = NULL;
    }
}

/****************************************************************************
* AUTHOR		: Thanigai
* DATE			: 12-SEPT-2005
* NAME			: stopLog
* DESCRIPTION	: stops the logging operation
* ARGUMENTS		:
* RETURNS		: SUCCESS on successful stop of logging
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*****************************************************************************/

int LTKLogger::stopLog()
{
    if (m_debugLevel == LTK_LOGLEVEL_OFF )
        return FAILURE;

	if(m_logFileName != "")
	{
		m_logFile.close();
		m_logStatus = INACTIVE;
		return 0;
	}
	return FAILURE;
}