Clone
ixen <ixen@copyhandler.com>
committed
on 01 Feb 09
Some project cleanups and reorganization of files in vcproj virtual folders.
LoggerImprovements + 5 more
src/ch/ExceptionEx.h (deleted)
1   /***************************************************************************
2   *   Copyright (C) 2001-2008 by J�zef Starosczyk                           *
3   *   ixen@copyhandler.com                                                  *
4   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *
6   *   it under the terms of the GNU Library General Public License          *
7   *   (version 2) as published by the Free Software Foundation;             *
8   *                                                                         *
9   *   This program is distributed in the hope that it will be useful,       *
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12   *   GNU General Public License for more details.                          *
13   *                                                                         *
14   *   You should have received a copy of the GNU Library General Public     *
15   *   License along with this program; if not, write to the                 *
16   *   Free Software Foundation, Inc.,                                       *
17   *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18   ***************************************************************************/
19   /*************************************************************************
20           File: Exception.h
21           Version: 1.0
22           Author: Ixen Gerthannes (ixen@interia.pl)
23           File description:
24                   Contain CException class - a base for any other exception
25                   types.
26           Classes:
27                   CException
28                           - provides basic exception functionality.
29                           - when used with MFC class name is CExceptionEx (it's
30                                   not based on MFC CException!).
31   *************************************************************************/
32   #ifndef __EXCEPTION_H__
33   #define __EXCEPTION_H__
34  
35   #include "stdio.h"
36  
37   #define THROW_EXCEPTIONEX(str_reason, app_code, last_error) throw new CExceptionEx(__FILE__, __LINE__, __FUNCTION__, str_reason, app_code, last_error)
38  
39   // not too specific - use specialised classes based on this one (this also could be used)
40   class CExceptionEx
41   {
42   protected:
43           enum PropType { dtString, dtPtrToString, dtDword, dtSysError };
44  
45           struct __EXCPROPINFO
46           {
47                   TCHAR szName[64];                               // name of the property (ie."Source file")
48                   PropType eType;                                 // type of the property (string, dword, bool, ...)
49                   void* pData;                                    // pointer to the value of the property
50           };
51  
52   public:
53           CExceptionEx(PCTSTR pszSrcFile, DWORD dwLine, PCTSTR pszFunc, PCTSTR pszReason, DWORD dwReason, DWORD dwLastError=0)
54           {
55                   // init the object with a given values
56                   _tcsncpy(m_szSourceFile, pszSrcFile, _MAX_PATH);
57                   m_szSourceFile[_MAX_PATH-1]=_T('\0');
58                   m_dwSourceLine=dwLine;
59                   _tcsncpy(m_szFunction, pszFunc, _MAX_PATH);
60                   m_szFunction[_MAX_PATH-1]=_T('\0');
61                   SetReason(pszReason);
62                   m_dwReason=dwReason;
63                   m_dwError=dwLastError;
64           };
65           CExceptionEx(PCTSTR pszSrcFile, DWORD dwLine, PCTSTR pszFunc, TCHAR* pszReasonDWORD dwReason, DWORD dwLastError=0)
66           {
67                   _tcsncpy(m_szSourceFile, pszSrcFile, _MAX_PATH);
68                   m_szSourceFile[_MAX_PATH-1]=_T('\0');
69                   m_dwSourceLine=dwLine;
70                   _tcsncpy(m_szFunction, pszFunc, _MAX_PATH);
71                   m_szFunction[_MAX_PATH-1]=_T('\0');
72                   m_pszReason=pszReason;
73                   m_dwReason=dwReason;
74                   m_dwError=dwLastError;
75           };
76  
77           virtual ~CExceptionEx() { delete [] m_pszReason; };
78  
79           virtual int RegisterInfo(__EXCPROPINFO* pInfo)
80           {
81                   // if the pInfo is null - return count of a needed props
82                   if (pInfo == NULL)
83                           return 6;       // +baseClass::RegisterInfo
84  
85                   // call base class RegisterInfo
86  
87                   // function has to register the info to be displayed (called from within GetInfo)
88                   RegisterProp(pInfo+0, _T("Source file"), dtString, &m_szSourceFile);
89                   RegisterProp(pInfo+1, _T("Line"), dtDword, &m_dwSourceLine);
90                   RegisterProp(pInfo+2, _T("Function"), dtString, &m_szFunction);
91                   RegisterProp(pInfo+3, _T("Reason"), dtPtrToString, &m_pszReason);
92                   RegisterProp(pInfo+4, _T("App error"), dtDword, &m_dwReason);
93                   RegisterProp(pInfo+5, _T("System error"), dtSysError, &m_dwError);
94  
95                   return 6;
96           };
97  
98   public:
99           // helpers
100           static TCHAR* FormatReason(PCTSTR pszReason, ...)
101           {
102                   const size_t stMaxReason = 1024;
103                   TCHAR szBuf[stMaxReason];
104  
105                   va_list marker;
106                   va_start(marker, pszReason);
107                   _vsntprintf(szBuf, stMaxReason - 1, pszReason, marker);
108                   szBuf[stMaxReason - 1] = _T('\0');
109                   va_end(marker);
110  
111                   TCHAR *pszData=new TCHAR[_tcslen(szBuf)+1];
112                   _tcscpy(pszData, szBuf);
113                   return pszData;
114           };
115  
116           // formats max info about this exception
117           virtual TCHAR* GetInfo(LPCTSTR pszDesc, TCHAR* pszStr, DWORD dwMaxLen)
118           {
119                   const size_t stMaxData = 1024;
120  
121                   // get the properties
122                   int iCount=RegisterInfo(NULL);
123                   __EXCPROPINFO *pepi=new __EXCPROPINFO[iCount];
124                   RegisterInfo(pepi);                     // register all the properties
125  
126                   // add the desc to the out
127                   if (pszDesc)
128                   {
129                           _tcsncpy(pszStr, pszDesc, dwMaxLen-1);
130                           pszStr[dwMaxLen-1]=_T('\0');
131                   }
132                   else
133                           pszStr[0]=_T('\0');
134  
135                   size_t tIndex=_tcslen(pszStr);
136  
137                   // format the info accordingly
138                   TCHAR szData[stMaxData];
139                   for (int i=0;i<iCount;i++)
140                   {
141                           // format this line
142                           switch(pepi[i].eType)
143                           {
144                           case dtString:
145                                   {
146                                           if (pszDesc)
147                                                   _sntprintf(szData, stMaxData - 1, _T("\r\n\t%s: %s"), pepi[i].szName, (TCHAR*)pepi[i].pData);
148                                           else
149                                                   _sntprintf(szData, stMaxData - 1, _T("%s: %s\r\n"), pepi[i].szName, (TCHAR*)pepi[i].pData);
150                                           break;
151                                   }
152                           case dtPtrToString:
153                                   {
154                                           if (pszDesc)
155                                                   _sntprintf(szData, stMaxData - 1, _T("\r\n\t%s: %s"), pepi[i].szName, *((TCHAR**)pepi[i].pData));
156                                           else
157                                                   _sntprintf(szData, stMaxData - 1, _T("%s: %s\r\n"), pepi[i].szName, *((TCHAR**)pepi[i].pData));
158                                           break;
159                                   }
160                           case dtDword:
161                                   {
162                                           if (pszDesc)
163                                                   _sntprintf(szData, stMaxData - 1, _T("\r\n\t%s: %lu"), pepi[i].szName, *((DWORD*)pepi[i].pData));
164                                           else
165                                                   _sntprintf(szData, stMaxData - 1, _T("%s: %lu\r\n"), pepi[i].szName, *((DWORD*)pepi[i].pData));
166                                           break;
167                                   }
168                           case dtSysError:
169                                   {
170                                           // get info about the last error (always treated as a system error)
171                                           TCHAR szSystem[1024];
172                                           DWORD dwPos=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, *((DWORD*)pepi[i].pData), 0, szSystem, 1024, NULL);
173  
174                                           // get rid of \r\n at the end of szSystem
175                                           while(--dwPos && (szSystem[dwPos] == 0x0a || szSystem[dwPos] == 0x0d))
176                                                   szSystem[dwPos]=_T('\0');
177  
178                                           if (pszDesc)
179                                                   _sntprintf(szData, stMaxData - 1, _T("\r\n\t%s: %lu (%s)"), pepi[i].szName, *((DWORD*)pepi[i].pData), szSystem);
180                                           else
181                                                   _sntprintf(szData, stMaxData - 1, _T("%s: %lu (%s)\r\n"), pepi[i].szName, *((DWORD*)pepi[i].pData), szSystem);
182  
183                                           break;
184                                   }
185                           }
186  
187                           szData[stMaxData - 1] = _T('\0');
188  
189                           // append the line
190                           size_t tLen=_tcslen(szData);
191                           if (tIndex+tLen < dwMaxLen-1)
192                                   _tcscat(pszStr, szData);
193                   }
194  
195                   delete [] pepi;
196                   return pszStr;
197           };
198  
199   protected:
200           void SetReason(PCTSTR pszReason) { /*delete [] m_pszReason;*/ if (pszReason) { m_pszReason=new TCHAR[_tcslen(pszReason)+1]; _tcscpy(m_pszReason, pszReason); } else m_pszReason=NULL; };
201           void RegisterProp(__EXCPROPINFO* pInfo, PCTSTR pszName, PropType type, void* pData)
202           {
203                   _tcsncpy(pInfo->szName, pszName, 63);
204                   pInfo->szName[63]=_T('\0');
205                   pInfo->eType=type;
206                   pInfo->pData=pData;
207           };
208  
209   public:
210           // exception information
211           TCHAR m_szSourceFile[_MAX_PATH];                // source file from where the exception is being thrown
212           DWORD m_dwSourceLine;                                   // line in the source file from where exception has been thrown
213           TCHAR m_szFunction[_MAX_PATH];                  // name of the function in which the exception occured
214           TCHAR *m_pszReason;                                             // description of this error (in english - internal error code - description - human readable)
215           DWORD m_dwReason;                                               // numerical value that states app-level error number
216           DWORD m_dwError;                                                // in most cases GetLastError() when it has any sense
217   };
218  
219   #endif