OSDN Git Service

Experiment with retrieving full log in one git.exe call
[tortoisegit/TortoiseGitJp.git] / src / Git / CrashReport.h
1 #pragma once\r
2 \r
3 // Client crash callback\r
4 typedef BOOL (CALLBACK *LPGETLOGFILE) (LPVOID lpvState);\r
5 // Stack trace callback\r
6 typedef void (*TraceCallbackFunction)(DWORD_PTR address, const char *ImageName,\r
7                                                                           const char *FunctionName, DWORD functionDisp,\r
8                                                                           const char *Filename, DWORD LineNumber, DWORD lineDisp,\r
9                                                                           void *data);\r
10 \r
11 typedef LPVOID (*InstallEx)(LPGETLOGFILE pfn, LPCSTR lpcszTo, LPCSTR lpcszSubject, BOOL bUseUI);\r
12 typedef void (*UninstallEx)(LPVOID lpState);\r
13 typedef void (*EnableUI)(void);\r
14 typedef void (*DisableUI)(void);\r
15 typedef void (*EnableHandler)(void);\r
16 typedef void (*DisableHandler)(void);\r
17 typedef void (*AddFileEx)(LPVOID lpState, LPCSTR lpFile, LPCSTR lpDesc);\r
18 typedef void (*AddRegistryEx)(LPVOID lpState, LPCSTR lpRegistry, LPCSTR lpDesc);\r
19 typedef void (*AddEventLogEx)(LPVOID lpState, LPCSTR lpEventLog, LPCSTR lpDesc);\r
20 \r
21 /**\r
22  * \ingroup CrashRpt\r
23  * This class wraps the most important functions the CrashRpt-library\r
24  * offers. To learn more about the CrashRpt-library go to\r
25  * http://www.codeproject.com/debug/crash_report.asp \n\r
26  * To compile the library you need the WTL. You can get the WTL\r
27  * directly from Microsoft: \r
28  * http://www.microsoft.com/downloads/details.aspx?FamilyID=128e26ee-2112-4cf7-b28e-7727d9a1f288&DisplayLang=en \n\r
29  * \n\r
30  * Many changes were made to the library so if you read the\r
31  * article on CodeProject also read the change log in the source\r
32  * folder.\n\r
33  * The most important changes are:\r
34  * - stack trace is included in the report, with symbols/linenumbers if available\r
35  * - "save" button so the user can save the report instead of directly send it\r
36  * - can be used by multiple applications\r
37  * - zlib linked statically, so no need to ship the zlib.dll separately\r
38  * \n\r
39  * To use the library just include the header file "CrashReport.h"\r
40  * \code\r
41  * #include "CrashReport.h"\r
42  * \endcode\r
43  * Then you can either declare an instance of the class CCrashReport\r
44  * somewhere globally in your application like this:\r
45  * \code\r
46  * CCrashReport g_crasher("report@mycompany.com", "Crash report for MyApplication");\r
47  * \endcode\r
48  * that way you can't add registry keys or additional files to the report, but\r
49  * it's the fastest and easiest way to use the library.\r
50  * Another way is to declare a global variable and initialize it in e.g. InitInstance()\r
51  * \code\r
52  * CCrashReport g_crasher;\r
53  * //then somewhere in InitInstance.\r
54  * g_crasher.AddFile("mylogfile.log", "this is a log file");\r
55  * g_crasher.AddRegistry("HKCU\\Software\\MyCompany\\MyProgram");\r
56  * \endcode\r
57  *\r
58  *\r
59  * \remark the dll is dynamically linked at runtime. So the main application\r
60  * will still work even if the dll is not shipped. \r
61  *\r
62  */\r
63 class CCrashReport\r
64 {\r
65 public:\r
66         /**\r
67          * Construct the CrashReport-Object. This loads the dll\r
68          * and initializes it.\r
69          * \param lpTo the mail address the crash report should be sent to\r
70          * \param lpSubject the mail subject\r
71          */\r
72         CCrashReport(LPCSTR lpTo = NULL, LPCSTR lpSubject = NULL, BOOL bUseUI = TRUE)\r
73         {\r
74                 InstallEx pfnInstallEx;\r
75                 TCHAR szFileName[_MAX_PATH];\r
76                 GetModuleFileName(NULL, szFileName, _MAX_FNAME);\r
77 \r
78                 // C:\Programme\TortoiseSVN\bin\TortoiseProc.exe -> C:\Programme\TortoiseSVN\bin\CrashRpt.dll\r
79                 CString strFilename = szFileName;\r
80                 strFilename = strFilename.Left(strFilename.ReverseFind(_T('\\')) + 1);\r
81                 strFilename += _T("CrashRpt.dll");\r
82 \r
83                 m_hDll = LoadLibrary(strFilename);\r
84                 if (m_hDll)\r
85                 {\r
86                         pfnInstallEx = (InstallEx)GetProcAddress(m_hDll, "InstallEx");\r
87                         if ( pfnInstallEx )\r
88                         {\r
89                                 m_lpvState = pfnInstallEx(NULL, lpTo, lpSubject, bUseUI);\r
90                         }\r
91                 }\r
92         }\r
93         ~CCrashReport()\r
94         {\r
95                 UninstallEx pfnUninstallEx;\r
96                 if ((m_hDll)&&(m_lpvState))\r
97                 {\r
98                         pfnUninstallEx = (UninstallEx)GetProcAddress(m_hDll, "UninstallEx");\r
99                         pfnUninstallEx(m_lpvState);\r
100                 }\r
101                 FreeLibrary(m_hDll);\r
102         }\r
103         /**\r
104          * Adds a file which will be included in the crash report. Use this\r
105          * if your application generates log-files or the like.\r
106          * \param lpFile the full path to the file\r
107          * \param lpDesc a description of the file, used in the crash report dialog\r
108          */\r
109         void AddFile(LPCSTR lpFile, LPCSTR lpDesc)\r
110         {\r
111                 AddFileEx pfnAddFileEx;\r
112                 if ((m_hDll)&&(m_lpvState))\r
113                 {\r
114                         pfnAddFileEx = (AddFileEx)GetProcAddress(m_hDll, "AddFileEx");\r
115                         (pfnAddFileEx)(m_lpvState, lpFile, lpDesc);\r
116                 }\r
117         }\r
118         /**\r
119          * Adds a whole registry tree to the crash report. \r
120          * \param lpFile the full registry path, e.g. "HKLM\\Software\\MyApplication"\r
121          * \param lpDesc a description of the generated registry file, used in the crash report dialog\r
122          */\r
123         void AddRegistry(LPCSTR lpFile, LPCSTR lpDesc)\r
124         {\r
125                 AddRegistryEx pfnAddRegistryEx;\r
126                 if ((m_hDll)&&(m_lpvState))\r
127                 {\r
128                         pfnAddRegistryEx = (AddRegistryEx)GetProcAddress(m_hDll, "AddRegistryHiveEx");\r
129                         (pfnAddRegistryEx)(m_lpvState, lpFile, lpDesc);\r
130                 }\r
131         }\r
132         /**\r
133          * Adds a system Event Log to the crash report.\r
134          * \param lpFile \r
135          * \param lpDesc \r
136          */\r
137         void AddEventLog(LPCSTR lpFile, LPCSTR lpDesc)\r
138         {\r
139                 AddEventLogEx pfnAddEventLogEx;\r
140                 if ((m_hDll)&&(m_lpvState))\r
141                 {\r
142                         pfnAddEventLogEx = (AddEventLogEx)GetProcAddress(m_hDll, "AddEventLogEx");\r
143                         (pfnAddEventLogEx)(m_lpvState, lpFile, lpDesc);\r
144                 }\r
145         }\r
146 \r
147 \r
148         void Enable(BOOL bEnable)\r
149         {\r
150                 EnableHandler pfnEnableHandler;\r
151                 DisableHandler pfnDisableHandler;\r
152                 if ((m_hDll)&&(m_lpvState))\r
153                 {\r
154                         if (bEnable)\r
155                         {\r
156                                 pfnEnableHandler = (EnableHandler)GetProcAddress(m_hDll, "EnableHandlerEx");\r
157                                 (pfnEnableHandler)();\r
158                         }\r
159                         else\r
160                         {\r
161                                 OutputDebugString(_T("Calling DisableHandlerEx\n"));\r
162 \r
163                                 pfnDisableHandler = (DisableHandler)GetProcAddress(m_hDll, "DisableHandlerEx");\r
164                                 (pfnDisableHandler)();\r
165                         }\r
166                 }\r
167         }\r
168 \r
169 private:\r
170         HMODULE                 m_hDll;\r
171         LPVOID                  m_lpvState;\r
172 };