KiCad PCB EDA Suite
Loading...
Searching...
No Matches
trace_helpers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018 Wayne Stambaugh <[email protected]>
5 * Copyright (C) 2018-2024 KiCad Developers, see change_log.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
30#include <trace_helpers.h>
31
32#include <wx/tokenzr.h>
33
34const wxChar* const traceFindReplace = wxT( "KICAD_FIND_REPLACE" );
35const wxChar* const kicadTraceCoords = wxT( "KICAD_COORDS" );
36const wxChar* const kicadTraceKeyEvent = wxT( "KICAD_KEY_EVENTS" );
37const wxChar* const kicadTraceToolStack = wxT( "KICAD_TOOL_STACK" );
38const wxChar* const kicadTraceCoroutineStack = wxT( "KICAD_COROUTINE_STACK" );
39const wxChar* const traceSchLibMem = wxT( "KICAD_SCH_LIB_MEM" );
40const wxChar* const traceFindItem = wxT( "KICAD_FIND_ITEM" );
41const wxChar* const traceSchLegacyPlugin = wxT( "KICAD_SCH_LEGACY_PLUGIN" );
42const wxChar* const traceSchPlugin = wxT( "KICAD_SCH_PLUGIN" );
43const wxChar* const traceGedaPcbPlugin = wxT( "KICAD_GEDA_PLUGIN" );
44const wxChar* const traceKicadPcbPlugin = wxT( "KICAD_PCB_PLUGIN" );
45const wxChar* const tracePrinting = wxT( "KICAD_PRINT" );
46const wxChar* const traceAutoSave = wxT( "KICAD_AUTOSAVE" );
47const wxChar* const tracePathsAndFiles = wxT( "KICAD_PATHS_AND_FILES" );
48const wxChar* const traceLocale = wxT( "KICAD_LOCALE" );
49const wxChar* const traceFonts = wxT( "KICAD_FONTS" );
50const wxChar* const traceScreen = wxT( "KICAD_SCREEN" );
51const wxChar* const traceZoomScroll = wxT( "KICAD_ZOOM_SCROLL" );
52const wxChar* const traceSymbolResolver = wxT( "KICAD_SYM_RESOLVE" );
53const wxChar* const traceDisplayLocation = wxT( "KICAD_DISPLAY_LOCATION" );
54const wxChar* const traceSchSheetPaths = wxT( "KICAD_SCH_SHEET_PATHS" );
55const wxChar* const traceEnvVars = wxT( "KICAD_ENV_VARS" );
56const wxChar* const traceGalProfile = wxT( "KICAD_GAL_PROFILE" );
57const wxChar* const traceKiCad2Step = wxT( "KICAD2STEP" );
58const wxChar* const traceUiProfile = wxT( "KICAD_UI_PROFILE" );
59const wxChar* const traceGit = wxT( "KICAD_GIT" );
60const wxChar* const traceEagleIo = wxT( "KICAD_EAGLE_IO" );
61const wxChar* const traceDesignBlocks = wxT( "KICAD_DESIGN_BLOCK" );
62
63
64wxString dump( const wxArrayString& aArray )
65{
66 wxString tmp;
67
68 for( unsigned i = 0; i < aArray.GetCount(); i++ )
69 {
70 if( aArray[i].IsEmpty() )
71 tmp << wxT( "\"\" " );
72 else
73 tmp << aArray[i] << wxT( " " );
74 }
75
76 return tmp;
77}
78
79
80// The following code was shamelessly copied from the wxWidgets keyboard sample
81// at https://github.com/wxWidgets/wxWidgets/blob/master/samples/keyboard/keyboard.cpp.
82
84// Author: Vadim Zeitlin
85// Modified by: Marcin Wojdyr
86// Created: 07.04.02
87// Copyright: (c) 2002 Vadim Zeitlin
88// Licence: wxWindows licence
90
91// helper function that returns textual description of wx virtual keycode
92const char* GetVirtualKeyCodeName(int keycode)
93{
94 switch ( keycode )
95 {
96#define WXK_(x) \
97 case WXK_##x: return #x;
98
99 WXK_(BACK)
100 WXK_(TAB)
101 WXK_(RETURN)
102 WXK_(ESCAPE)
103 WXK_(SPACE)
104 WXK_(DELETE)
105 WXK_(START)
106 WXK_(LBUTTON)
107 WXK_(RBUTTON)
108 WXK_(CANCEL)
109 WXK_(MBUTTON)
110 WXK_(CLEAR)
111 WXK_(SHIFT)
112 WXK_(ALT)
113 WXK_(CONTROL)
114 WXK_(MENU)
115 WXK_(PAUSE)
116 WXK_(CAPITAL)
117 WXK_(END)
118 WXK_(HOME)
119 WXK_(LEFT)
120 WXK_(UP)
121 WXK_(RIGHT)
122 WXK_(DOWN)
123 WXK_(SELECT)
124 WXK_(PRINT)
125 WXK_(EXECUTE)
126 WXK_(SNAPSHOT)
127 WXK_(INSERT)
128 WXK_(HELP)
129 WXK_(NUMPAD0)
130 WXK_(NUMPAD1)
131 WXK_(NUMPAD2)
132 WXK_(NUMPAD3)
133 WXK_(NUMPAD4)
134 WXK_(NUMPAD5)
135 WXK_(NUMPAD6)
136 WXK_(NUMPAD7)
137 WXK_(NUMPAD8)
138 WXK_(NUMPAD9)
139 WXK_(MULTIPLY)
140 WXK_(ADD)
143 WXK_(DECIMAL)
144 WXK_(DIVIDE)
145 WXK_(F1)
146 WXK_(F2)
147 WXK_(F3)
148 WXK_(F4)
149 WXK_(F5)
150 WXK_(F6)
151 WXK_(F7)
152 WXK_(F8)
153 WXK_(F9)
154 WXK_(F10)
155 WXK_(F11)
156 WXK_(F12)
157 WXK_(F13)
158 WXK_(F14)
159 WXK_(F15)
160 WXK_(F16)
161 WXK_(F17)
162 WXK_(F18)
163 WXK_(F19)
164 WXK_(F20)
165 WXK_(F21)
166 WXK_(F22)
167 WXK_(F23)
168 WXK_(F24)
169 WXK_(NUMLOCK)
170 WXK_(SCROLL)
171 WXK_(PAGEUP)
172 WXK_(PAGEDOWN)
173 WXK_(NUMPAD_SPACE)
174 WXK_(NUMPAD_TAB)
175 WXK_(NUMPAD_ENTER)
176 WXK_(NUMPAD_F1)
177 WXK_(NUMPAD_F2)
178 WXK_(NUMPAD_F3)
179 WXK_(NUMPAD_F4)
180 WXK_(NUMPAD_HOME)
181 WXK_(NUMPAD_LEFT)
182 WXK_(NUMPAD_UP)
183 WXK_(NUMPAD_RIGHT)
184 WXK_(NUMPAD_DOWN)
185 WXK_(NUMPAD_PAGEUP)
186 WXK_(NUMPAD_PAGEDOWN)
187 WXK_(NUMPAD_END)
188 WXK_(NUMPAD_BEGIN)
189 WXK_(NUMPAD_INSERT)
190 WXK_(NUMPAD_DELETE)
191 WXK_(NUMPAD_EQUAL)
192 WXK_(NUMPAD_MULTIPLY)
193 WXK_(NUMPAD_ADD)
194 WXK_(NUMPAD_SEPARATOR)
195 WXK_(NUMPAD_SUBTRACT)
196 WXK_(NUMPAD_DECIMAL)
197 WXK_(NUMPAD_DIVIDE)
198
199 WXK_(WINDOWS_LEFT)
200 WXK_(WINDOWS_RIGHT)
201#ifdef __WXOSX__
202 WXK_(RAW_CONTROL)
203#endif
204#undef WXK_
205
206 default:
207 return nullptr;
208 }
209}
210
211
212// helper function that returns textual description of key in the event
213wxString GetKeyName( const wxKeyEvent &aEvent )
214{
215 int keycode = aEvent.GetKeyCode();
216 const char* virt = GetVirtualKeyCodeName( keycode );
217
218 if( virt )
219 return virt;
220
221 if( keycode > 0 && keycode < 32 )
222 return wxString::Format( "Ctrl-%c", (unsigned char)('A' + keycode - 1) );
223
224 if( keycode >= 32 && keycode < 128 )
225 return wxString::Format( "'%c'", (unsigned char)keycode );
226
227#if wxUSE_UNICODE
228 int uc = aEvent.GetUnicodeKey();
229
230 if( uc != WXK_NONE )
231 return wxString::Format( "'%c'", uc );
232#endif
233
234 return "unknown";
235}
236
237
238wxString dump( const wxKeyEvent& aEvent )
239{
240 wxString msg;
241 wxString eventType = wxS( "unknown" );
242
243 if( aEvent.GetEventType() == wxEVT_KEY_DOWN )
244 eventType = wxS( "KeyDown" );
245 else if( aEvent.GetEventType() == wxEVT_KEY_UP )
246 eventType = wxS( "KeyUp" );
247 else if( aEvent.GetEventType() == wxEVT_CHAR )
248 eventType = wxS( "Char" );
249 else if( aEvent.GetEventType() == wxEVT_CHAR_HOOK )
250 eventType = wxS( "Hook" );
251
252 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
253 msg.Printf( "%7s %15s %5d %c%c%c%c"
254#if wxUSE_UNICODE
255 "%5d (U+%04x)"
256#else
257 " none "
258#endif
259#ifdef wxHAS_RAW_KEY_CODES
260 " %7lu 0x%08lx"
261#else
262 " not-set not-set"
263#endif
264 " (%5d,%5d)",
265 eventType,
266 GetKeyName( aEvent ),
267 aEvent.GetKeyCode(),
268 aEvent.ControlDown() ? 'C' : '-',
269 aEvent.AltDown() ? 'A' : '-',
270 aEvent.ShiftDown() ? 'S' : '-',
271 aEvent.MetaDown() ? 'M' : '-'
272#if wxUSE_UNICODE
273 , aEvent.GetUnicodeKey()
274 , aEvent.GetUnicodeKey()
275#endif
276#ifdef wxHAS_RAW_KEY_CODES
277 , (unsigned long) aEvent.GetRawKeyCode()
278 , (unsigned long) aEvent.GetRawKeyFlags()
279#endif
280 , aEvent.GetX()
281 , aEvent.GetY()
282 );
283
284 return msg;
285}
286
287
289{
290 static TRACE_MANAGER* self = nullptr;
291
292 if( !self )
293 {
294 self = new TRACE_MANAGER;
295 self->init();
296 }
297
298 return *self;
299}
300
301
302bool TRACE_MANAGER::IsTraceEnabled( const wxString& aWhat )
303{
304 if( !m_printAllTraces )
305 {
307 return false;
308
309 if( m_enabledTraces.find( aWhat ) == m_enabledTraces.end() )
310 return false;
311 }
312
313 return true;
314}
315
316
317void TRACE_MANAGER::traceV( const wxString& aWhat, const wxString& aFmt, va_list vargs )
318{
319 if( !IsTraceEnabled( aWhat ) )
320 return;
321
322 wxString str;
323 str.PrintfV( aFmt, vargs );
324
325#if defined( __UNIX__ ) || defined( _WIN32 )
326 fprintf( stderr, " %-30s | %s", aWhat.c_str().AsChar(), str.c_str().AsChar() );
327#endif
328}
329
330
332{
333 wxString traceVars;
334 m_globalTraceEnabled = wxGetEnv( wxT( "KICAD_TRACE" ), &traceVars );
335 m_printAllTraces = false;
336
338 return;
339
340 wxStringTokenizer tokenizer( traceVars, wxT( "," ) );
341
342 while( tokenizer.HasMoreTokens() )
343 {
344 wxString token = tokenizer.GetNextToken();
345 m_enabledTraces[token] = true;
346
347 if( token.Lower() == wxT( "all" ) )
348 m_printAllTraces = true;
349 }
350}
@ ADD
Definition: am_param.h:150
static TRACE_MANAGER & Instance()
const wxChar * aFmt
traceV(aWhat, aFmt, argptr)
bool IsTraceEnabled(const wxString &aWhat)
bool m_globalTraceEnabled
std::map< wxString, bool > m_enabledTraces
const wxChar *const traceSymbolResolver
Flag to enable debug output of symbol library resolver results.
const wxChar *const traceLocale
Flag to enable locale debug output.
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
const wxChar *const traceAutoSave
Flag to enable auto save feature debug tracing.
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
const wxChar *const traceUiProfile
Flag to enable user interface profile tracing.
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
const wxChar *const traceFindReplace
Flag to enable find and replace debug tracing.
const wxChar *const traceFindItem
Flag to enable find debug tracing.
const wxChar *const kicadTraceCoroutineStack
Flag to enable tracing of the coroutine call stack.
const wxChar *const traceSchLibMem
Flag to enable schematic library memory deletion debug output.
const wxChar *const traceEagleIo
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
const wxChar *const traceKiCad2Step
Flag to enable KiCad2Step debug tracing.
const wxChar *const traceSchPlugin
Flag to enable legacy schematic plugin debug output.
const wxChar *const traceSchSheetPaths
Flag to enable debug output of schematic symbol sheet path manipulation code.
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
const wxChar *const tracePrinting
Flag to enable print controller debug output.
const wxChar *const kicadTraceCoords
Flag to enable draw panel coordinate debug tracing.
const wxChar *const traceZoomScroll
Flag to enable debug output of zoom-scrolling calculations in KIGFX::ZOOM_CONTROLLER and derivatives.
const wxChar *const traceGedaPcbPlugin
Flag to enable GEDA PCB plugin debug output.
const wxChar *const traceScreen
Flag to enable debug output of BASE_SCREEN and it's derivatives.
const wxChar *const traceDesignBlocks
const wxChar *const traceFonts
Flag to enable locale debug output.
const wxChar *const traceGit
Flag to enable Git debugging output.
const wxChar *const traceDisplayLocation
Flag to enable debug output of display positioning logic.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
static VECTOR2D CLEAR
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
#define WXK_(x)
wxString GetKeyName(const wxKeyEvent &aEvent)
const char * GetVirtualKeyCodeName(int keycode)
wxLogTrace helper definitions.
#define SEPARATOR
Definition: wx_combobox.cpp:27