xref: /aoo42x/main/sal/osl/w32/diagnose.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "system.h"
29 
30 #define NO_DEBUG_CRT
31 
32 #include <osl/diagnose.h>
33 #include <osl/thread.h>
34 
35 #include "printtrace.h"
36 
37 #define NO_DEBUG_CRT
38 
39 static pfunc_osl_printDebugMessage	_pPrintDebugMessage = NULL;
40 static pfunc_osl_printDetailedDebugMessage	_pPrintDetailedDebugMessage = NULL;
41 
42 pfunc_osl_printDebugMessage SAL_CALL osl_setDebugMessageFunc( pfunc_osl_printDebugMessage pNewFunc )
43 {
44 	pfunc_osl_printDebugMessage	pOldFunc = _pPrintDebugMessage;
45 	_pPrintDebugMessage = pNewFunc;
46 
47 	return pOldFunc;
48 }
49 
50 pfunc_osl_printDetailedDebugMessage SAL_CALL osl_setDetailedDebugMessageFunc( pfunc_osl_printDetailedDebugMessage pNewFunc )
51 {
52 	pfunc_osl_printDetailedDebugMessage	pOldFunc = _pPrintDetailedDebugMessage;
53 	_pPrintDetailedDebugMessage = pNewFunc;
54 	return pOldFunc;
55 }
56 
57 /*
58  Trace output
59 */
60 
61 void SAL_CALL osl_breakDebug(void)
62 {
63 	DebugBreak();
64 }
65 
66 void osl_trace(char const * pszFormat, ...) {
67     va_list args;
68     va_start(args, pszFormat);
69     if ( IsDebuggerPresent() )
70     {
71         sal_Char	szMessage[512];
72         int written = _vsnprintf(
73             szMessage, sizeof(szMessage) - 2, pszFormat, args );
74         if ( written == -1 )
75             written = sizeof(szMessage) - 2;
76         szMessage[ written++ ] = '\n';
77         szMessage[ written ] = 0;
78         OutputDebugString( szMessage );
79     }
80     printTrace((unsigned long) _getpid(), pszFormat, args);
81     va_end(args);
82 }
83 
84 sal_Bool SAL_CALL osl_assertFailedLine(const sal_Char* pszFileName, sal_Int32 nLine, const sal_Char* pszMessage)
85 {
86 #ifndef NO_DEBUG_CRT
87 	return (_CrtDbgReport(_CRT_ASSERT, pszFileName, nLine, NULL, pszMessage));
88 #else
89 	HWND hWndParent;
90 	UINT nFlags;
91 	int  nCode;
92 
93 	/* get app name or NULL if unknown (don't call assert) */
94 	LPCSTR lpszAppName = "Error";
95 	sal_Char   szMessage[512];
96     char const * env = getenv( "SAL_DIAGNOSE_ABORT" );
97 
98 	/* format message into buffer */
99 	szMessage[sizeof(szMessage)-1] = '\0';	/* zero terminate always */
100 	_snprintf(szMessage, sizeof(szMessage)-1, "%s: File %hs, Line %d\n:%s\n",
101 	 	      lpszAppName, pszFileName, nLine,	pszMessage);
102 
103 	OutputDebugString(szMessage);
104 
105     if ( _pPrintDetailedDebugMessage )
106         _pPrintDetailedDebugMessage( pszFileName, nLine, pszMessage );
107     else if ( _pPrintDebugMessage )
108 		_pPrintDebugMessage( szMessage );
109 	else
110     {
111         if ( !getenv( "DISABLE_SAL_DBGBOX" ) )
112 	    {
113 		    TCHAR	szBoxMessage[1024];
114 
115 		    /* active popup window for the current thread */
116 		    hWndParent = GetActiveWindow();
117 		    if (hWndParent != NULL)
118 			    hWndParent = GetLastActivePopup(hWndParent);
119 
120 		    /* set message box flags */
121 		    nFlags = MB_TASKMODAL | MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND;
122 		    if (hWndParent == NULL)
123 			    nFlags |= MB_SERVICE_NOTIFICATION;
124 
125 		    /* display the assert */
126 
127 		    szBoxMessage[sizeof(szBoxMessage)-1] = 0;
128 		    _snprintf(szBoxMessage, sizeof(szBoxMessage)-1, "%s\n( Yes=Abort / No=Ignore / Cancel=Debugger )",
129 	 			      szMessage);
130 
131 		    nCode = MessageBox(hWndParent, szBoxMessage, "Assertion Failed!", nFlags);
132 
133 		    if (nCode == IDYES)
134 			    FatalExit(-1);
135 
136 		    if (nCode == IDNO)
137 			    return sal_False;   /* ignore */
138 
139 		    if (nCode == IDCANCEL)
140 			    return sal_True;    /* will cause oslDebugBreak */
141 	    }
142         return ( ( env != NULL ) && ( *env != '\0' ) );
143     }
144 
145     return sal_False;
146 #endif /* NO_DEBUG_CRT */
147 }
148 
149 sal_Int32 SAL_CALL osl_reportError(sal_uInt32 nType, const sal_Char* pszMessage)
150 {
151 	UINT nFlags;
152 	int nDisposition;
153 
154 	// active popup window for the current thread
155 	HWND hWndParent = GetActiveWindow();
156 	if (hWndParent != NULL)
157 		hWndParent = GetLastActivePopup(hWndParent);
158 
159     nType = nType; /* avoid warnings */
160 
161 	/* set message box flags */
162 	nFlags = MB_TASKMODAL | MB_ICONERROR | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND;
163 	if (hWndParent == NULL)
164 		nFlags |= MB_SERVICE_NOTIFICATION;
165 
166 	// display the assert
167 	nDisposition = MessageBox(hWndParent, pszMessage, "Exception!", nFlags);
168 
169 	return nDisposition;
170 }
171 
172