xref: /AOO41X/main/sal/osl/w32/diagnose.c (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "system.h"
25 
26 #define NO_DEBUG_CRT
27 
28 #include <osl/diagnose.h>
29 #include <osl/thread.h>
30 
31 #include "printtrace.h"
32 
33 #define NO_DEBUG_CRT
34 
35 static pfunc_osl_printDebugMessage  _pPrintDebugMessage = NULL;
36 static pfunc_osl_printDetailedDebugMessage  _pPrintDetailedDebugMessage = NULL;
37 
osl_setDebugMessageFunc(pfunc_osl_printDebugMessage pNewFunc)38 pfunc_osl_printDebugMessage SAL_CALL osl_setDebugMessageFunc( pfunc_osl_printDebugMessage pNewFunc )
39 {
40     pfunc_osl_printDebugMessage pOldFunc = _pPrintDebugMessage;
41     _pPrintDebugMessage = pNewFunc;
42 
43     return pOldFunc;
44 }
45 
osl_setDetailedDebugMessageFunc(pfunc_osl_printDetailedDebugMessage pNewFunc)46 pfunc_osl_printDetailedDebugMessage SAL_CALL osl_setDetailedDebugMessageFunc( pfunc_osl_printDetailedDebugMessage pNewFunc )
47 {
48     pfunc_osl_printDetailedDebugMessage pOldFunc = _pPrintDetailedDebugMessage;
49     _pPrintDetailedDebugMessage = pNewFunc;
50     return pOldFunc;
51 }
52 
53 /*
54  Trace output
55 */
56 
osl_breakDebug(void)57 void SAL_CALL osl_breakDebug(void)
58 {
59     DebugBreak();
60 }
61 
osl_trace(char const * pszFormat,...)62 void osl_trace(char const * pszFormat, ...) {
63     va_list args;
64     va_start(args, pszFormat);
65     if ( IsDebuggerPresent() )
66     {
67         sal_Char    szMessage[512];
68         int written = _vsnprintf(
69             szMessage, sizeof(szMessage) - 2, pszFormat, args );
70         if ( written == -1 )
71             written = sizeof(szMessage) - 2;
72         szMessage[ written++ ] = '\n';
73         szMessage[ written ] = 0;
74         OutputDebugString( szMessage );
75     }
76     printTrace((unsigned long) _getpid(), pszFormat, args);
77     va_end(args);
78 }
79 
osl_assertFailedLine(const sal_Char * pszFileName,sal_Int32 nLine,const sal_Char * pszMessage)80 sal_Bool SAL_CALL osl_assertFailedLine(const sal_Char* pszFileName, sal_Int32 nLine, const sal_Char* pszMessage)
81 {
82 #ifndef NO_DEBUG_CRT
83     return (_CrtDbgReport(_CRT_ASSERT, pszFileName, nLine, NULL, pszMessage));
84 #else
85     HWND hWndParent;
86     UINT nFlags;
87     int  nCode;
88 
89     /* get app name or NULL if unknown (don't call assert) */
90     LPCSTR lpszAppName = "Error";
91     sal_Char   szMessage[512];
92     char const * env = getenv( "SAL_DIAGNOSE_ABORT" );
93 
94     /* format message into buffer */
95     szMessage[sizeof(szMessage)-1] = '\0';  /* zero terminate always */
96     _snprintf(szMessage, sizeof(szMessage)-1, "%s: File %hs, Line %d\n:%s\n",
97               lpszAppName, pszFileName, nLine,  pszMessage);
98 
99     OutputDebugString(szMessage);
100 
101     if ( _pPrintDetailedDebugMessage )
102         _pPrintDetailedDebugMessage( pszFileName, nLine, pszMessage );
103     else if ( _pPrintDebugMessage )
104         _pPrintDebugMessage( szMessage );
105     else
106     {
107         if ( !getenv( "DISABLE_SAL_DBGBOX" ) )
108         {
109             TCHAR   szBoxMessage[1024];
110 
111             /* active popup window for the current thread */
112             hWndParent = GetActiveWindow();
113             if (hWndParent != NULL)
114                 hWndParent = GetLastActivePopup(hWndParent);
115 
116             /* set message box flags */
117             nFlags = MB_TASKMODAL | MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND;
118             if (hWndParent == NULL)
119                 nFlags |= MB_SERVICE_NOTIFICATION;
120 
121             /* display the assert */
122 
123             szBoxMessage[sizeof(szBoxMessage)-1] = 0;
124             _snprintf(szBoxMessage, sizeof(szBoxMessage)-1, "%s\n( Yes=Abort / No=Ignore / Cancel=Debugger )",
125                       szMessage);
126 
127             nCode = MessageBox(hWndParent, szBoxMessage, "Assertion Failed!", nFlags);
128 
129             if (nCode == IDYES)
130                 FatalExit(-1);
131 
132             if (nCode == IDNO)
133                 return sal_False;   /* ignore */
134 
135             if (nCode == IDCANCEL)
136                 return sal_True;    /* will cause oslDebugBreak */
137         }
138         return ( ( env != NULL ) && ( *env != '\0' ) );
139     }
140 
141     return sal_False;
142 #endif /* NO_DEBUG_CRT */
143 }
144 
osl_reportError(sal_uInt32 nType,const sal_Char * pszMessage)145 sal_Int32 SAL_CALL osl_reportError(sal_uInt32 nType, const sal_Char* pszMessage)
146 {
147     UINT nFlags;
148     int nDisposition;
149 
150     // active popup window for the current thread
151     HWND hWndParent = GetActiveWindow();
152     if (hWndParent != NULL)
153         hWndParent = GetLastActivePopup(hWndParent);
154 
155     nType = nType; /* avoid warnings */
156 
157     /* set message box flags */
158     nFlags = MB_TASKMODAL | MB_ICONERROR | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND;
159     if (hWndParent == NULL)
160         nFlags |= MB_SERVICE_NOTIFICATION;
161 
162     // display the assert
163     nDisposition = MessageBox(hWndParent, pszMessage, "Exception!", nFlags);
164 
165     return nDisposition;
166 }
167 
168