File: LogBase.h

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (369 lines) | stat: -rw-r--r-- 15,322 bytes parent folder | download
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
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
/*
This file contains 2 classes for log purpose : CLogger and CLog

CLOGGER
-------
CLogger is a singleton that keep a vector of CLog (one log by group).

Instance : -The instance() method give access to the singleton object

Initialization : -The CLogger may be init. 
                 -If it isn't, the default parameters are used.
                 -The init parameters are transfered to CLog object when the CLogger create them.
                 -The init may be call anywhere to change the parameters 
				 but these changes only affect the future created CLog

Retreive a Log : -Use the getLog() method with the name of the group as argument

CLOG
----
Each CLog represents a set of log file. (One set by group) 
The constructor is not enabled but objects are created by the logger when you ask for a new group.
The files are closed after each write.

PARAMETERS
----------
You pass the parameters with the init method of the CLogger and it passes them to CLog at creation.

These parameters are :
*Directory : -The folder in which the log files are created. 
             -If it doesn't exist, the current directory is used.
*Prefix :	 -Use to define the name of the files.
			 -The name is <Prefix>_[<Group>_]Index.log (Index begins to 0 until FileNr-1)
*FileSize :  -The maximum size of the log file.
			 -0 means no limit
			 -When the file is full, we use a new file with the next index.
*FileNr :	 -The maximum number of files (by group)
			 -If the FileSize is 0, this parameter is not used.
			 -The minimum value is 2
			 -When the last file (Index=FileNr-1) is full, the file with index 0 is removed,
			 the File(1) is renamed to File(0), File(i) to File(i-1), 
			 then File(FileNr-1) become free again.

*GroupInNewFile :	-If true, new log files are made for each group
					-If false, the name of the group (if any) appears on each entry line

USAGE
-----
In complement of usual object usage, some macros are defined to give simple access to methods.
These macros are define below in this file.
You will also find macro template to enable easy use of group. Copy these macros and customize them for your own purpose.

Example of usage through objects and macro are give at the end of this file
*/

#pragma once

#include <memory>
#include <string>
#include <vector>
#include "Mutex.h"

#include <stdarg.h>

#ifndef __WFILE__
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
#endif

#ifndef WIN32
#include <sys/file.h>
#endif

namespace eIDMW
{

class CLog; //define below

typedef enum {
    LEV_LEVEL_NOLOG,
    LOG_LEVEL_CRITICAL,
    LOG_LEVEL_ERROR,
    LOG_LEVEL_WARNING,
    LOG_LEVEL_INFO,
    LOG_LEVEL_DEBUG,
} tLOG_Level;

#define LOG_LEVEL_DEFAULT LOG_LEVEL_ERROR

class CLogger
{
  public:
    EIDMW_CMN_API static CLogger &instance();
    EIDMW_CMN_API ~CLogger();
 
  private:
    CLogger();
	CLogger(const CLogger &logger);
	CLogger &operator= (const CLogger &logger);

  public:
	EIDMW_CMN_API void initFromConfig();
	EIDMW_CMN_API void init(const wchar_t *directory,const wchar_t *prefix,long filesize,long filenr,tLOG_Level minlevel,bool groupinnewfile); 
	EIDMW_CMN_API void init(const char *directory,const char *prefix,long filesize,long filenr,tLOG_Level minlevel,bool groupinnewfile); 
	EIDMW_CMN_API CLog &getLogW(const wchar_t *group=L"");
	EIDMW_CMN_API CLog &getLogA(const char *group="");
	EIDMW_CMN_API void writeToGroup(const wchar_t *group,tLOG_Level level,const wchar_t *format, ...);
	EIDMW_CMN_API void writeToGroup(const char *group,tLOG_Level level,const char *format, ...);
	EIDMW_CMN_API void writeToGroup(const wchar_t *group,tLOG_Level level,const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeToGroup(const char *group,tLOG_Level level,const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const wchar_t *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const char *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const char *file,const char *format, ...);

  private:
    static std::auto_ptr<CLogger> m_instance;
    static bool m_bApplicationLeaving;

	std::wstring m_directory;
	std::wstring m_prefix;
	long m_filesize;
	long m_filenr;
	tLOG_Level m_maxlevel;
	bool m_groupinnewfile;

	std::vector <CLog*> m_logStore;
};

class CLog
{
  public:
    EIDMW_CMN_API ~CLog();
 
  private:
    CLog(const wchar_t *directory,const wchar_t *prefix,const wchar_t *group,long filesize,long filenr,tLOG_Level minlevel,bool groupinnewfile);	
	CLog(const CLog &log);				
	CLog &  operator= (const CLog &) ;

  public:
	EIDMW_CMN_API void write(tLOG_Level level,const wchar_t *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const char *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const wchar_t *format, va_list args);
	EIDMW_CMN_API void write(tLOG_Level level,const char *format, va_list args);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const wchar_t *file,const wchar_t *format, va_list args);
	EIDMW_CMN_API void write(tLOG_Level level,const int line,const char *file,const char *format, va_list args);

	EIDMW_CMN_API void writeCritical(const wchar_t *format, ...);
	EIDMW_CMN_API void writeCritical(const char *format, ...);
	EIDMW_CMN_API void writeCritical(const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeCritical(const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void writeError(const wchar_t *format, ...);
	EIDMW_CMN_API void writeError(const char *format, ...);
	EIDMW_CMN_API void writeError(const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeError(const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void writeWarning(const wchar_t *format, ...);
	EIDMW_CMN_API void writeWarning(const char *format, ...);
	EIDMW_CMN_API void writeWarning(const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeWarning(const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void writeInfo(const wchar_t *format, ...);
	EIDMW_CMN_API void writeInfo(const char *format, ...);
	EIDMW_CMN_API void writeInfo(const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeInfo(const int line,const char *file,const char *format, ...);
	EIDMW_CMN_API void writeDebug(const wchar_t *format, ...);
	EIDMW_CMN_API void writeDebug(const char *format, ...);
	EIDMW_CMN_API void writeDebug(const int line,const wchar_t *file,const wchar_t *format, ...);
	EIDMW_CMN_API void writeDebug(const int line,const char *file,const char *format, ...);

	//USE ONLY IN MACRO
	EIDMW_CMN_API bool writeLineHeaderW(tLOG_Level level,const int line=0,const wchar_t *file=L"");
	EIDMW_CMN_API bool writeLineHeaderA(tLOG_Level level,const int line=0,const char *file="");
	EIDMW_CMN_API bool writeLineMessageW(const wchar_t *format, ...);
	EIDMW_CMN_API bool writeLineMessageA(const char *format, ...);

  friend class CLogger;

private:
	void getFilename(std::wstring &filename);
	void renameFiles(const wchar_t *root_filename);
	bool open(bool bWchar);
	void close();
	void writeLineMessageW(const wchar_t *format, va_list argList);
	void writeLineMessageA(const char *format, va_list argList);
	const wchar_t *getLevel(tLOG_Level level);
	void getLocalTimeW(std::wstring &timestamp, const wchar_t *format=L"%Y-%m-%d %H:%M:%S");
	void getLocalTimeA(std::string &timestamp, const char *format="%Y-%m-%d %H:%M:%S");

	std::wstring m_directory;
	std::wstring m_prefix;
	std::wstring m_group;
	long m_filesize;
	long m_filenr;
	tLOG_Level m_maxlevel;
	bool m_groupinnewfile;

	FILE *m_f;

#ifndef WIN32
	struct flock m_flock;
#endif
};

//SHORTCUT MACRO
//General use
#define ZS_LOG_INIT			CLogger::instance().init
#define ZS_LOG				CLogger::instance().write
#define ZS_LOG_GROUP		CLogger::instance().writeToGroup

//Defining a level
#define ZS_LOG_CRITICAL		CLogger::instance().getLogW(L"").writeCritical
#define ZS_LOG_ERROR		CLogger::instance().getLogW(L"").writeError
#define ZS_LOG_WARNING		CLogger::instance().getLogW(L"").writeWarning
#define ZS_LOG_INFO			CLogger::instance().getLogW(L"").writeInfo
#define ZS_LOG_DEBUG		CLogger::instance().getLogW(L"").writeDebug

//Defining a level and trace file and line
#define ZS_LOG_CRITICAL_TRACE	!CLogger::instance().getLogW(L"").writeLineHeaderW(LOG_LEVEL_CRITICAL,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"").writeLineMessageW
#define ZS_LOG_ERROR_TRACE		!CLogger::instance().getLogW(L"").writeLineHeaderW(LOG_LEVEL_ERROR,__LINE__,__WFILE__)		? 0 : CLogger::instance().getLogW(L"").writeLineMessageW
#define ZS_LOG_WARNING_TRACE	!CLogger::instance().getLogW(L"").writeLineHeaderW(LOG_LEVEL_WARNING,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"").writeLineMessageW
#define ZS_LOG_INFO_TRACE		!CLogger::instance().getLogW(L"").writeLineHeaderW(LOG_LEVEL_INFO,__LINE__,__WFILE__)		? 0 : CLogger::instance().getLogW(L"").writeLineMessageW
#define ZS_LOG_DEBUG_TRACE		!CLogger::instance().getLogW(L"").writeLineHeaderW(LOG_LEVEL_DEBUG,__LINE__,__WFILE__)		? 0 : CLogger::instance().getLogW(L"").writeLineMessageW

//MACRO EXTENSION
//Example of extanding to a groupe named Group1
#define ZS_LOG_GROUP1			CLogger::instance().getLogW(L"Group1").write

#define ZS_LOG_GROUP1_CRITICAL	CLogger::instance().getLogW(L"Group1").writeCritical
#define ZS_LOG_GROUP1_ERROR		CLogger::instance().getLogW(L"Group1").writeError
#define ZS_LOG_GROUP1_WARNING	CLogger::instance().getLogW(L"Group1").writeWarning
#define ZS_LOG_GROUP1_INFO		CLogger::instance().getLogW(L"Group1").writeInfo
#define ZS_LOG_GROUP1_DEBUG		CLogger::instance().getLogW(L"Group1").writeDebug

#define ZS_LOG_GROUP1_CRITICAL_TRACE	!CLogger::instance().getLogW(L"Group1").writeLineHeaderW(LOG_LEVEL_CRITICAL,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"Group1").writeLineMessageW
#define ZS_LOG_GROUP1_ERROR_TRACE		!CLogger::instance().getLogW(L"Group1").writeLineHeaderW(LOG_LEVEL_ERROR,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"Group1").writeLineMessageW
#define ZS_LOG_GROUP1_WARNING_TRACE		!CLogger::instance().getLogW(L"Group1").writeLineHeaderW(LOG_LEVEL_WARNING,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"Group1").writeLineMessageW
#define ZS_LOG_GROUP1_INFO_TRACE		!CLogger::instance().getLogW(L"Group1").writeLineHeaderW(LOG_LEVEL_INFO,__LINE__,__WFILE__)		? 0 : CLogger::instance().getLogW(L"Group1").writeLineMessageW
#define ZS_LOG_GROUP1_DEBUG_TRACE		!CLogger::instance().getLogW(L"Group1").writeLineHeaderW(LOG_LEVEL_DEBUG,__LINE__,__WFILE__)	? 0 : CLogger::instance().getLogW(L"Group1").writeLineMessageW

}

/*
*************
*** USAGE ***
*************
	int i=1;
//INITIALIZING THE LOGGER
//-----------------------
//Object use
	CLogger &logger = CLogger::instance();
	logger.init(L"C:\\Tmp", L"Test", 100000, 5, true);

//Or
	CLogger::instance().init(L"C:\\Tmp", L"Test", 100000, 5, true);

//Macro
	ZS_LOG_INIT(L"C:\\Tmp", L"Test", 100000, 5, true);

//SIMPLE EXAMPLE (NO GROUP, NO FILE TRACE)
//----------------------------------------
//Object use
	CLog &logA=logger.getLogW();
	logA.write(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"First test");

//Or
	logA.writeError(L"Example %d: %s", i++, L"First test");

//Or
	logger.write(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"First test");

//Or
	CLogger::instance().write(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"First test");

//Macro
	ZS_LOG(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"First test");

//Or
	ZS_LOG_ERROR(L"Example %d: %s", i++, L"First test");

//SIMPLE EXAMPLE WITH FILE TRACE (NO GROUP)
//----------------------------------------
//Object use
	CLog &logB=logger.getLogW();
	logB.write(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Second test");

//Or
	logB.writeError(__LINE__,__WFILE__,L"Example %d: %s", i++, L"Second test");

//Or
	logger.write(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Second test");

//Or
	CLogger::instance().write(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Second test");

//Macro
	ZS_LOG(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Second test");

//Or
	ZS_LOG_ERROR_TRACE(L"Example %d: %s", i++, L"Second test");

//EXAMPLE WITH GROUP (NO FILE TRACE)
//----------------------------------
//Object use
	CLog &logGroup1A=logger.getLog(L"Group1");
	logGroup1A.write(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"Third test");

//Or
	logGroup1A.writeError(L"Example %d: %s", i++, L"Third test");

//Or
	logger.writeToGroup(L"Group1",LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"Third test");

//Or
	CLogger::instance().writeToGroup(L"Group1",LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"Third test");

//Macro
	ZS_LOG_GROUP(L"Group1",LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"Third test");

//Or
	ZS_LOG_GROUP1(LOG_LEVEL_ERROR,L"Example %d: %s", i++, L"Third test");

//Or
	ZS_LOG_GROUP1_ERROR(L"Example %d: %s", i++, L"Third test");

//EXAMPLE WITH GROUP AND FILE TRACE
//---------------------------------
//Object use
	CLog &logGroup1B=logger.getLog(L"Group1");
	logGroup1B.write(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Or
	logGroup1B.writeError(__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Or
	logger.writeToGroup(L"Group1",LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Or
	CLogger::instance().writeToGroup(L"Group1",LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Macro
	ZS_LOG_GROUP(L"Group1",LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Or
	ZS_LOG_GROUP1(LOG_LEVEL_ERROR,__LINE__,__WFILE__,L"Example %d: %s", i++, L"Fourth test");

//Or
	ZS_LOG_GROUP1_ERROR_TRACE(L"Example %d: %s", i++, L"Fourth test");

*/