ErrLib
ErrLib.h
Go to the documentation of this file.
1//Project: ErrLib
2//Author: MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
3
8#ifndef ErrLib_H_INCLUDED
9#define ErrLib_H_INCLUDED
10#include <stdlib.h>
11#include <stdio.h>
12#include <stdint.h>
13
14#include <windows.h>
15#include <strsafe.h>
16#include <process.h>
17#include <DbgHelp.h>
18#include <Shlobj.h>
19
20#ifdef __cplusplus
21#include <exception>
22#include <comdef.h>
23#define ERRLIB_REFERENCE
24#define ERRLIB_INLINE inline
25#else
26#define ERRLIB_REFERENCE &
27#define ERRLIB_INLINE __inline
28#endif
29
30#pragma comment(lib, "advapi32.lib")
31#pragma comment(lib, "Dbghelp.lib")
32
33#define ErrLib_MaxNameLen 300
34
38#define ErrLib_MessageLen 1024
39
43#define ErrLib_StackLen 10000
44
45#ifdef ERRLIB_EXPORTS
46#define ERRLIB_API __declspec(dllexport)
47#else
48#define ERRLIB_API __declspec(dllimport)
49#pragma comment(lib, "ErrLib.lib")
50#endif
51
52// *** The following are the message definitions. ***
53
57#define MSG_ERROR ((DWORD)0xC0020100L)
58
62#define MSG_WARNING ((DWORD)0x80020101L)
63
67#define MSG_INFORMATION ((DWORD)0x40020102L)
68
69// *** Configuration flags ***
70
75#define ERRLIB_OUTPUT_LOGFILE 1
76
81#define ERRLIB_OUTPUT_STDERR 2
82
87#define ERRLIB_OUTPUT_MBOX 3
88
93#define ERRLIB_OUTPUT_EVENT_LOG 4
94
100#define ERRLIB_OUTPUT_CUSTOM 5
101
102#define ERRLIB_PARAM_VISUALCPPVERSION 100
103#define ERRLIB_PARAM_ISDEBUGBUILD 101
104
108#define ERRLIB_SYMBOL_NAME 1
109
113#define ERRLIB_SYMBOL_MODULE 2
114
118#define ERRLIB_SYMBOL_SOURCE 3
119
120// *** Typedefs ***
121
122//Function pointer type used as unhandled exception callback
123typedef LONG (WINAPI * ERRLIB_EXCEPTION_CALLBACK) ( struct _EXCEPTION_POINTERS *,LPCWSTR,LPCWSTR);
124
125//Function pointer type used for custom logging targets
126typedef void (WINAPI * ERRLIB_LOGGING_CALLBACK) (LPCWSTR, void*);
127
134 uint64_t addr;
135 uint64_t displacement;
136 WCHAR symbol[MAX_SYM_NAME];
137 WCHAR module[MAX_PATH];
138 WCHAR src_file[MAX_PATH];
139 DWORD src_line;
141
148 ERRLIB_STACK_FRAME *data;
149 int capacity;
150 int count;
151 BOOL isOnHeap;
153
154// *** Custom exception codes for SEH ***
155
156//Win32 Exception
157#define ERRLIB_WIN32_EXCEPTION 0xC0400000
158
159//Com Exception
160#define ERRLIB_COM_EXCEPTION 0xC0400001
161
162//Application Exception
163#define ERRLIB_APP_EXCEPTION 0xC0400002
164
165// *** exported function declarations ***
166
167#ifdef __cplusplus
168extern "C" {
169#endif
170
178ERRLIB_API void __stdcall ErrLib_ErrorMes(LPTSTR lpszFunction,DWORD dw,WCHAR* buf);
179
180ERRLIB_API DWORD __stdcall ErrLib_GetWinapiErrorMessage(DWORD dwCode, BOOL localized, WCHAR* pOutput, int cch);
181
182//Gets filename from full path
183ERRLIB_API WCHAR* __stdcall ErrLib_FileNameFromPathW(WCHAR* path);
184
197ERRLIB_API void __stdcall ErrLib_SetLoggingCallback(ERRLIB_LOGGING_CALLBACK pCallback);
198
215ERRLIB_API void __stdcall ErrLib_SetExceptionCallback(ERRLIB_EXCEPTION_CALLBACK pCallback);
216
222ERRLIB_API void __stdcall ErrLib_SetLogFilePath(LPCWSTR path);
223
238ERRLIB_API BOOL __stdcall ErrLib_SetParameter(UINT param, UINT_PTR value);
239
240//Initializes the library.
241ERRLIB_API BOOL __stdcall ErrLib_InitializeInternal();
242
249BOOL ERRLIB_INLINE ErrLib_Initialize(){
250 BOOL ret = ErrLib_InitializeInternal();
251#ifdef _MSC_VER
252 ErrLib_SetParameter(ERRLIB_PARAM_VISUALCPPVERSION, (UINT_PTR)_MSC_VER);
253#endif
254
255#ifdef _DEBUG
256 ErrLib_SetParameter(ERRLIB_PARAM_ISDEBUGBUILD, (UINT_PTR)TRUE);
257#endif
258
259 return ret;
260}
261
262ERRLIB_API BOOL __stdcall ErrLib_InitTLS();
263ERRLIB_API BOOL __stdcall ErrLib_InitThread();
264ERRLIB_API void __stdcall ErrLib_FreeThread();
265
272ERRLIB_API BOOL __stdcall ErrLib_RegisterEventSource();
273
281ERRLIB_API BOOL __stdcall ErrLib_UnregisterEventSource();
282
292ERRLIB_API ERRLIB_STACK_TRACE __stdcall ErrLib_GetStackTrace(CONTEXT* ctx);
293
300ERRLIB_API int __stdcall ErrLib_ST_GetFramesCount(const ERRLIB_STACK_TRACE* pStack);
301
311ERRLIB_API const ERRLIB_STACK_FRAME* __stdcall ErrLib_ST_GetFrame(const ERRLIB_STACK_TRACE* pStack, int n);
312
319ERRLIB_API uint64_t __stdcall ErrLib_ST_GetAddress(const ERRLIB_STACK_FRAME* pFrame);
320
328ERRLIB_API uint64_t __stdcall ErrLib_ST_GetDisplacement(const ERRLIB_STACK_FRAME* pFrame);
329
341ERRLIB_API const WCHAR* __stdcall ErrLib_ST_GetStringProperty(const ERRLIB_STACK_FRAME* pFrame, int propId);
342
350ERRLIB_API DWORD __stdcall ErrLib_ST_GetSymLine(const ERRLIB_STACK_FRAME* pFrame);
351
358ERRLIB_API void __stdcall ErrLib_FreeStackTrace(ERRLIB_STACK_TRACE* pStack);
359
371ERRLIB_API void __stdcall ErrLib_PrintStack(CONTEXT* ctx, WCHAR* dest, size_t cch);
372
381ERRLIB_API void __stdcall ErrLib_GetExceptionMessage(struct _EXCEPTION_POINTERS* ExceptionInfo, LPWSTR dest, size_t cch);
382
396ERRLIB_API void __stdcall ErrLib_LogExceptionInfo(DWORD dwExcCode,LPCWSTR lpwsMessage,LPCWSTR lpwsStackTrace, BOOL visible);
397
412ERRLIB_API void __stdcall ErrLib_LogMessage(LPCWSTR lpwsMessage, BOOL visible, DWORD type, BOOL bIncludeStack );
413
419ERRLIB_API DWORD __stdcall ErrLib_Except_GetCode();
420
426ERRLIB_API LPWSTR __stdcall ErrLib_Except_GetMessage();
427
433ERRLIB_API LPWSTR __stdcall ErrLib_Except_GetStackTrace();
434
443
444ERRLIB_API LONG __stdcall ErrLib_CatchCode( struct _EXCEPTION_POINTERS * ex, DWORD FilteredCode);
445ERRLIB_API LONG __stdcall ErrLib_CatchAll( struct _EXCEPTION_POINTERS * ex);
446
447ERRLIB_API LPVOID __stdcall ErrLib_StrBuf_GetPointer();
448ERRLIB_API LPVOID __stdcall ErrLib_ExArgs_GetPointer();
449
450#ifdef __cplusplus
451ERRLIB_API void __stdcall ErrLib_HResultToString(HRESULT hr,LPTSTR lpszFunction,WCHAR* buf);
452ERRLIB_API void __stdcall ErrLib_GetHResultMessage(HRESULT hr,WCHAR* lpOutput, int cch);
453}//extern "C"
454#endif
455
456
457//**** Helper macro functions *****
458
464#define ERRLIB_THROW(mes) {((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[0]=(ULONG_PTR)mes;\
465RaiseException(ERRLIB_APP_EXCEPTION,0,1,((ULONG_PTR*)ErrLib_ExArgs_GetPointer()));}
466
477#define ERRLIB_THROW_IF_EQUAL(var,value,func) if((var)==(value)){DWORD ErrLibLocal_LastError=GetLastError();\
478ErrLib_ErrorMes(L#func,ErrLibLocal_LastError,(WCHAR*)ErrLib_StrBuf_GetPointer());((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[0]=(ULONG_PTR)ErrLibLocal_LastError;\
479((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[1]=(ULONG_PTR)ErrLib_StrBuf_GetPointer();RaiseException(ERRLIB_WIN32_EXCEPTION,0,2,(ULONG_PTR*)ErrLib_ExArgs_GetPointer());}
480
490#define ERRLIB_THROW_IF_FAILED(var,func) if(FAILED(var)){ErrLib_HResultToString((DWORD)var,L#func,(WCHAR*)ErrLib_StrBuf_GetPointer());\
491((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[0]=(ULONG_PTR)var;((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[1]=(ULONG_PTR)ErrLib_StrBuf_GetPointer();RaiseException(ERRLIB_COM_EXCEPTION,0,2,((ULONG_PTR*)ErrLib_ExArgs_GetPointer()));}
492
502#define ERRLIB_INVOKEAPI(func, ...) if(FALSE == func( ##__VA_ARGS__ )){DWORD ErrLibLocal_LastError=GetLastError();\
503ErrLib_ErrorMes((L#func),ErrLibLocal_LastError,(WCHAR*)ErrLib_StrBuf_GetPointer());\
504((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[0]=(ULONG_PTR)ErrLibLocal_LastError;((ULONG_PTR*)ErrLib_ExArgs_GetPointer())[1]=(ULONG_PTR)ErrLib_StrBuf_GetPointer();\
505RaiseException(ERRLIB_WIN32_EXCEPTION,0,2,((ULONG_PTR*)ErrLib_ExArgs_GetPointer()));}
506
510#define ERRLIB_CATCH_ALL __except(ErrLib_CatchAll(GetExceptionInformation()))
511
515#define ERRLIB_CATCH(code) __except(ErrLib_CatchCode(GetExceptionInformation(),code))
516
517#endif
ERRLIB_API BOOL __stdcall ErrLib_SetParameter(UINT param, UINT_PTR value)
ERRLIB_API LPWSTR __stdcall ErrLib_Except_GetStackTrace()
ERRLIB_API void __stdcall ErrLib_SetLogFilePath(LPCWSTR path)
ERRLIB_API ERRLIB_STACK_TRACE __stdcall ErrLib_GetStackTrace(CONTEXT *ctx)
BOOL ERRLIB_INLINE ErrLib_Initialize()
Definition: ErrLib.h:249
ERRLIB_API void __stdcall ErrLib_LogMessage(LPCWSTR lpwsMessage, BOOL visible, DWORD type, BOOL bIncludeStack)
ERRLIB_API BOOL __stdcall ErrLib_RegisterEventSource()
struct structERRLIB_STACK_FRAME ERRLIB_STACK_FRAME
ERRLIB_API const ERRLIB_STACK_FRAME *__stdcall ErrLib_ST_GetFrame(const ERRLIB_STACK_TRACE *pStack, int n)
ERRLIB_API void __stdcall ErrLib_PrintStack(CONTEXT *ctx, WCHAR *dest, size_t cch)
ERRLIB_API void __stdcall ErrLib_FreeStackTrace(ERRLIB_STACK_TRACE *pStack)
ERRLIB_API void __stdcall ErrLib_ErrorMes(LPTSTR lpszFunction, DWORD dw, WCHAR *buf)
ERRLIB_API DWORD __stdcall ErrLib_Except_GetCode()
ERRLIB_API void __stdcall ErrLib_SetLoggingCallback(ERRLIB_LOGGING_CALLBACK pCallback)
struct structERRLIB_STACK_TRACE ERRLIB_STACK_TRACE
ERRLIB_API void __stdcall ErrLib_LogExceptionInfo(DWORD dwExcCode, LPCWSTR lpwsMessage, LPCWSTR lpwsStackTrace, BOOL visible)
ERRLIB_API int __stdcall ErrLib_ST_GetFramesCount(const ERRLIB_STACK_TRACE *pStack)
ERRLIB_API uint64_t __stdcall ErrLib_ST_GetAddress(const ERRLIB_STACK_FRAME *pFrame)
ERRLIB_API DWORD __stdcall ErrLib_ST_GetSymLine(const ERRLIB_STACK_FRAME *pFrame)
ERRLIB_API void __stdcall ErrLib_SetExceptionCallback(ERRLIB_EXCEPTION_CALLBACK pCallback)
ERRLIB_API void __stdcall ErrLib_GetExceptionMessage(struct _EXCEPTION_POINTERS *ExceptionInfo, LPWSTR dest, size_t cch)
ERRLIB_API BOOL __stdcall ErrLib_UnregisterEventSource()
ERRLIB_API LPWSTR __stdcall ErrLib_Except_GetMessage()
ERRLIB_API uint64_t __stdcall ErrLib_ST_GetDisplacement(const ERRLIB_STACK_FRAME *pFrame)
ERRLIB_API ERRLIB_STACK_TRACE __stdcall ErrLib_Except_GetStackTraceData()
ERRLIB_API const WCHAR *__stdcall ErrLib_ST_GetStringProperty(const ERRLIB_STACK_FRAME *pFrame, int propId)
Definition: ErrLib.h:133
Definition: ErrLib.h:147