1 /* "CodeWorker":	a scripting language for parsing and generating text.
2 
3 Copyright (C) 1996-1997, 1999-2002 C�dric Lemaire
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 To contact the author: codeworker@free.fr
20 */
21 
22 #ifndef _UtlTrace_h_
23 #define _UtlTrace_h_
24 
25 #include <fstream>
26 #include <string>
27 #include <memory>
28 
29 namespace CodeWorker {
30 
31 #define UTLTRACE_METHOD(iLevel, sClass, sMethod) UtlTrace traceInstance(iLevel, sClass, sMethod)
32 #define UTLTRACE_TEXT(iLevel, sText) if (UtlTrace::isValidLevel(iLevel)) UtlTrace::traceText(__FILE__, __LINE__, sText)
33 #define UTLTRACE_FORMAT UtlTrace::traceFormat
34 #define UTLTRACE_STACK_INSTRUCTION(sFileName, iLine) UtlTrace::traceStackInstruction(sFileName, iLine)
35 #define UTLTRACE_STACK_FUNCTION(sFileName, sFunctionName, iLine) UtlTraceStackFunction myTraceStackFunction(sFileName, sFunctionName, iLine)
36 
37 #ifdef THROW_UTLEXCEPTION
38 #undef THROW_UTLEXCEPTION
39 #endif
40 #define THROW_UTLEXCEPTION(sText) { std::string sExceptionTrace = std::string("EXCEPTION: \"") + sText + "\"";UtlTrace::writeText(sExceptionTrace.c_str());throw UtlException(sText); }
41 
42 #ifdef THROW_UTLEXCEPTION2
43 #undef THROW_UTLEXCEPTION2
44 #endif
45 #define THROW_UTLEXCEPTION2(myStream, sText) { std::string sExceptionTrace = std::string("EXCEPTION: \"") + sText + "\"";UtlTrace::writeText(sExceptionTrace.c_str());throw UtlException(myStream, sText); }
46 
47 #ifdef THROW_UTLEXCEPTION_STATIC
48 #undef THROW_UTLEXCEPTION_STATIC
49 #endif
50 #define THROW_UTLEXCEPTION_STATIC(sText) { std::string sExceptionTrace = std::string("EXCEPTION: \"") + sText + "\"";UtlTrace::writeText(sExceptionTrace.c_str());throw UtlException(sText); }
51 
52 	class UtlTrace {
53 	private:
54 		int _iLevel;
55 		std::string _sClass;
56 		std::string _sMethod;
57 
58 		static int _iLowerLevel;
59 		static int _iUpperLevel;
60 		static int _iCountLine;
61 		static int _iIndentation;
62 		static char* _sIndentation;
63 
64 		static const char** _tsTraceStack;
65 		static const char** _tsTraceFileStack;
66 		static int* _tiTraceLocationStack;
67 		static int _iTraceSize;
68 		static int _iMaxTraceSize;
69 
70 		static std::string _sTraceFile;
71 		static bool  _bPersistent;
72 		static std::auto_ptr<std::ofstream> _pFile;
73 
74 	public:
75 		UtlTrace(int iLevel, const char* sClass, const char* sMethod);
76 		~UtlTrace();
77 
getLevel()78 		int getLevel() const { return _iLevel; }
79 
getLowerLevel()80 		static int getLowerLevel() { return _iLowerLevel; }
getUpperLevel()81 		static int getUpperLevel() { return _iUpperLevel; }
setUpperLevel(int iLevel)82 		static void setUpperLevel(int iLevel) { _iUpperLevel = iLevel; }
isValidLevel(int iLevel)83 		static bool isValidLevel(int iLevel) { return (iLevel >= getLowerLevel()) && (iLevel <= getUpperLevel()); }
84 
85 		static void traceText(const char* sSourceFile, int iLineCode, const char* sText);
86 		static void traceFormat(int iLevel, const char* sFormat, ...);
87 		static void writeText(const char* sText);
88 
getTraceSize()89 		static inline int getTraceSize() { return _iTraceSize; }
90 		static void pushTraceMethod(const char* sTraceMethod);
91 		static void popTraceMethod();
92 
93 		static void traceStackInstruction(const char* sFileName, int iLine);
94 		static void traceStackFunction(const char* sFileName, const char* sFunctionName, int iLine);
95 		static void popTraceStack();
96 
97 		static std::string getTraceStack();
98 		static const char** copyTraceStack();
99 		static const char** copyTraceFileStack();
100 		static int* copyTraceLocationStack();
101 
102 		static void writeTraceText(const char* sText);
103 
104 		static void initialize(const char* sTraceFile, bool bPersistent, int iLowerLevel, int iUpperLevel);
105 		static void setConfiguration(const char* sTraceFile, bool bPersistent, int iLowerLevel, int iUpperLevel);
106 		static void finalize();
107 	};
108 
109 	class UtlTraceStackFunction {
110 	public:
111 		UtlTraceStackFunction(const char* sFileName, const char* sFunctionName, int iLine);
112 		~UtlTraceStackFunction();
113 	};
114 
115 	class UtlTraceSession {
116 	public:
117 		UtlTraceSession();
118 		~UtlTraceSession();
119 	};
120 }
121 
122 #endif
123