Skip to content

Commit

Permalink
exchnld: Add ability to specify the location of the crash report file.
Browse files Browse the repository at this point in the history
Based on Daniel Atallah's patch:

  https://hg.pidgin.im/util/drmingw/rev/088532978452

Fixes #10.
  • Loading branch information
jrfonseca committed Jun 5, 2015
1 parent 7d9a2fd commit 318048b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ You can use ExcHndl by:

* compiling and linking `exchndl2.cpp` source file

* or by explicitly calling `LoadLibrary("exchndl.dll")`
* or by explicitly calling `LoadLibraryA("exchndl.dll")`

* you can also override the report location by invoking the exported `SetLogFileNameA` entry-point.

This comment has been minimized.

Copy link
@dismine

dismine Jun 5, 2015

Is it possible provide some little example how use it entry-point? Can't understand myself.

This comment has been minimized.

Copy link
@jrfonseca

jrfonseca Jun 5, 2015

Author Owner

I thought about that. But I decided that rather than mucking about with LoadLibraryA and GetProcAddress, (which is complex and potentially unsafe), I'm going to start providing a libexchndl.a import library.

In the meanwhile, see the code bloew in the test case -- 318048b#diff-69928642edb2a051cada87f33b079740R90

This comment has been minimized.

Copy link
@renatosilva

renatosilva Jun 7, 2015

Wouldn't it be better that you defined the function as SetLogFileName and used TCHAR and friends instead? Or maybe added a macro that resolved to the appropriate version? SetLogFileNameA may not be anymore available if Unicode is eventually supported and you decide to maintain a single version of the function.

This comment has been minimized.

Copy link
@White-Tiger

White-Tiger Jun 7, 2015

@renatosilva you know that this is actually the reason why SetLogFileNameA was chosen? So that it works for anyone? (ok it will not work on paths that actually require unicode.. such as a Chinese path on a non Chinese localized OS^^)
"TCHAR" can't be used if you're about to export symbols.. "TCHAR" is either char or wchar_t depending on how the application was build.. So it's always char for DrMinGW for example, but your application might use unicode and you'll then try to pass a wchar_t parameter to a char function.... because your header defined it as TCHAR even though it's compiled as char and not wchar_t
So if unicode support is to be added, it would require SetLogFileNameW in addition to A

This comment has been minimized.

Copy link
@renatosilva

renatosilva Jun 9, 2015

See issue #19.


Example
-------
Expand Down
23 changes: 21 additions & 2 deletions src/exchndl/exchndl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "exchndl.h"

#include <assert.h>
#include <windows.h>
#include <tchar.h>
Expand All @@ -26,6 +28,7 @@

#include "symbols.h"
#include "log.h"
#include "outdbg.h"


#define REPORT_FILE 1
Expand Down Expand Up @@ -171,9 +174,25 @@ static void OnExit(void)
SetUnhandledExceptionFilter(g_prevExceptionFilter);
}

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved);

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
BOOL APIENTRY
SetLogFileNameA(const char *szLogFileName)
{
size_t size = _countof(g_szLogFileName);
if (!szLogFileName ||
_tcslen(szLogFileName) > size - 1) {
OutputDebug("EXCHNDL: specified log name is too long or invalid (%s)\n",
szLogFileName);
return FALSE;
}
_tcsncpy(g_szLogFileName, szLogFileName, size - 1);
g_szLogFileName[size - 1] = _T('\0');
return TRUE;
}


BOOL APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
Expand Down
1 change: 1 addition & 0 deletions src/exchndl/exchndl.def
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
EXPORTS
SetLogFileNameA
25 changes: 25 additions & 0 deletions src/exchndl/exchndl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2002-2015 Jose Fonseca
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include <windows.h>


EXTERN_C BOOL APIENTRY SetLogFileNameA(const char *szLogFileName);

EXTERN_C BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved);
21 changes: 20 additions & 1 deletion tests/exchndl_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,24 @@ main(int argc, char **argv)

SetUnhandledExceptionFilter(topLevelExceptionHandler);

ok = LoadLibraryA("exchndl.dll");
HMODULE hModule = LoadLibraryA("exchndl.dll");
ok = hModule != NULL;
test_line(ok, "LoadLibraryA(\"exchndl.dll\")");
if (!ok) {
test_diagnostic_last_error();
test_exit();
}

typedef BOOL (APIENTRY * PFN_SETLOGFILENAMEA)(const char *szLogFileName);
PFN_SETLOGFILENAMEA pfnSetLogFileNameA = (PFN_SETLOGFILENAMEA)GetProcAddress(hModule, "SetLogFileNameA");
ok = pfnSetLogFileNameA != NULL;
test_line(ok, "GetProcAddress(\"SetLogFileNameA\")");
if (!ok) {
test_diagnostic_last_error();
} else {
ok = pfnSetLogFileNameA(szReport);
test_line(ok, "SetLogFileNameA(\"%s\")", szReport);

if (!setjmp(g_JmpBuf) ) {
_snprintf(g_szExceptionPattern, sizeof g_szExceptionPattern, " %s!%s [%s @ %u]", PROG_NAME ".exe", __FUNCTION__, __FILE__, __LINE__); *((int *)0) = 0; test_line(false, "longjmp"); exit(1);
} else {
Expand Down Expand Up @@ -120,5 +133,11 @@ main(int argc, char **argv)
}
}

ok = FreeLibrary(hModule);
test_line(ok, "FreeLibrary()");
if (!ok) {
test_diagnostic_last_error();
}

test_exit();
}

0 comments on commit 318048b

Please sign in to comment.