KiCad PCB EDA Suite
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-2021 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" );
58
59
60wxString dump( const wxArrayString& aArray )
61{
62 wxString tmp;
63
64 for( unsigned i = 0; i < aArray.GetCount(); i++ )
65 {
66 if( aArray[i].IsEmpty() )
67 tmp << wxT( "\"\" " );
68 else
69 tmp << aArray[i] << wxT( " " );
70 }
71
72 return tmp;
73}
74
75
76// The following code was shamelessly copied from the wxWidgets keyboard sample
77// at https://github.com/wxWidgets/wxWidgets/blob/master/samples/keyboard/keyboard.cpp.
78
80// Author: Vadim Zeitlin
81// Modified by: Marcin Wojdyr
82// Created: 07.04.02
83// Copyright: (c) 2002 Vadim Zeitlin
84// Licence: wxWindows licence
86
87// helper function that returns textual description of wx virtual keycode
88const char* GetVirtualKeyCodeName(int keycode)
89{
90 switch ( keycode )
91 {
92#define WXK_(x) \
93 case WXK_##x: return #x;
94
95 WXK_(BACK)
96 WXK_(TAB)
97 WXK_(RETURN)
98 WXK_(ESCAPE)
99 WXK_(SPACE)
100 WXK_(DELETE)
101 WXK_(START)
102 WXK_(LBUTTON)
103 WXK_(RBUTTON)
104 WXK_(CANCEL)
105 WXK_(MBUTTON)
106 WXK_(CLEAR)
107 WXK_(SHIFT)
108 WXK_(ALT)
109 WXK_(CONTROL)
110 WXK_(MENU)
111 WXK_(PAUSE)
112 WXK_(CAPITAL)
113 WXK_(END)
114 WXK_(HOME)
115 WXK_(LEFT)
116 WXK_(UP)
117 WXK_(RIGHT)
118 WXK_(DOWN)
119 WXK_(SELECT)
120 WXK_(PRINT)
121 WXK_(EXECUTE)
122 WXK_(SNAPSHOT)
123 WXK_(INSERT)
124 WXK_(HELP)
125 WXK_(NUMPAD0)
126 WXK_(NUMPAD1)
127 WXK_(NUMPAD2)
128 WXK_(NUMPAD3)
129 WXK_(NUMPAD4)
130 WXK_(NUMPAD5)
131 WXK_(NUMPAD6)
132 WXK_(NUMPAD7)
133 WXK_(NUMPAD8)
134 WXK_(NUMPAD9)
135 WXK_(MULTIPLY)
136 WXK_(ADD)
139 WXK_(DECIMAL)
140 WXK_(DIVIDE)
141 WXK_(F1)
142 WXK_(F2)
143 WXK_(F3)
144 WXK_(F4)
145 WXK_(F5)
146 WXK_(F6)
147 WXK_(F7)
148 WXK_(F8)
149 WXK_(F9)
150 WXK_(F10)
151 WXK_(F11)
152 WXK_(F12)
153 WXK_(F13)
154 WXK_(F14)
155 WXK_(F15)
156 WXK_(F16)
157 WXK_(F17)
158 WXK_(F18)
159 WXK_(F19)
160 WXK_(F20)
161 WXK_(F21)
162 WXK_(F22)
163 WXK_(F23)
164 WXK_(F24)
165 WXK_(NUMLOCK)
166 WXK_(SCROLL)
167 WXK_(PAGEUP)
168 WXK_(PAGEDOWN)
169 WXK_(NUMPAD_SPACE)
170 WXK_(NUMPAD_TAB)
171 WXK_(NUMPAD_ENTER)
172 WXK_(NUMPAD_F1)
173 WXK_(NUMPAD_F2)
174 WXK_(NUMPAD_F3)
175 WXK_(NUMPAD_F4)
176 WXK_(NUMPAD_HOME)
177 WXK_(NUMPAD_LEFT)
178 WXK_(NUMPAD_UP)
179 WXK_(NUMPAD_RIGHT)
180 WXK_(NUMPAD_DOWN)
181 WXK_(NUMPAD_PAGEUP)
182 WXK_(NUMPAD_PAGEDOWN)
183 WXK_(NUMPAD_END)
184 WXK_(NUMPAD_BEGIN)
185 WXK_(NUMPAD_INSERT)
186 WXK_(NUMPAD_DELETE)
187 WXK_(NUMPAD_EQUAL)
188 WXK_(NUMPAD_MULTIPLY)
189 WXK_(NUMPAD_ADD)
190 WXK_(NUMPAD_SEPARATOR)
191 WXK_(NUMPAD_SUBTRACT)
192 WXK_(NUMPAD_DECIMAL)
193 WXK_(NUMPAD_DIVIDE)
194
195 WXK_(WINDOWS_LEFT)
196 WXK_(WINDOWS_RIGHT)
197#ifdef __WXOSX__
198 WXK_(RAW_CONTROL)
199#endif
200#undef WXK_
201
202 default:
203 return nullptr;
204 }
205}
206
207
208// helper function that returns textual description of key in the event
209wxString GetKeyName( const wxKeyEvent &aEvent )
210{
211 int keycode = aEvent.GetKeyCode();
212 const char* virt = GetVirtualKeyCodeName( keycode );
213
214 if( virt )
215 return virt;
216
217 if( keycode > 0 && keycode < 32 )
218 return wxString::Format( "Ctrl-%c", (unsigned char)('A' + keycode - 1) );
219
220 if( keycode >= 32 && keycode < 128 )
221 return wxString::Format( "'%c'", (unsigned char)keycode );
222
223#if wxUSE_UNICODE
224 int uc = aEvent.GetUnicodeKey();
225
226 if( uc != WXK_NONE )
227 return wxString::Format( "'%c'", uc );
228#endif
229
230 return "unknown";
231}
232
233
234wxString dump( const wxKeyEvent& aEvent )
235{
236 wxString msg;
237 wxString eventType = wxS( "unknown" );
238
239 if( aEvent.GetEventType() == wxEVT_KEY_DOWN )
240 eventType = wxS( "KeyDown" );
241 else if( aEvent.GetEventType() == wxEVT_KEY_UP )
242 eventType = wxS( "KeyUp" );
243 else if( aEvent.GetEventType() == wxEVT_CHAR )
244 eventType = wxS( "Char" );
245 else if( aEvent.GetEventType() == wxEVT_CHAR_HOOK )
246 eventType = wxS( "Hook" );
247
248 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
249 msg.Printf( "%7s %15s %5d %c%c%c%c"
250#if wxUSE_UNICODE
251 "%5d (U+%04x)"
252#else
253 " none "
254#endif
255#ifdef wxHAS_RAW_KEY_CODES
256 " %7lu 0x%08lx"
257#else
258 " not-set not-set"
259#endif
260 " (%5d,%5d)",
261 eventType,
262 GetKeyName( aEvent ),
263 aEvent.GetKeyCode(),
264 aEvent.ControlDown() ? 'C' : '-',
265 aEvent.AltDown() ? 'A' : '-',
266 aEvent.ShiftDown() ? 'S' : '-',
267 aEvent.MetaDown() ? 'M' : '-'
268#if wxUSE_UNICODE
269 , aEvent.GetUnicodeKey()
270 , aEvent.GetUnicodeKey()
271#endif
272#ifdef wxHAS_RAW_KEY_CODES
273 , (unsigned long) aEvent.GetRawKeyCode()
274 , (unsigned long) aEvent.GetRawKeyFlags()
275#endif
276 , aEvent.GetX()
277 , aEvent.GetY()
278 );
279
280 return msg;
281}
282
283
285{
286 static TRACE_MANAGER* self = nullptr;
287
288 if( !self )
289 {
290 self = new TRACE_MANAGER;
291 self->init();
292 }
293
294 return *self;
295}
296
297
298bool TRACE_MANAGER::IsTraceEnabled( const wxString& aWhat )
299{
300 if( !m_printAllTraces )
301 {
303 return false;
304
305 if( m_enabledTraces.find( aWhat ) == m_enabledTraces.end() )
306 return false;
307 }
308
309 return true;
310}
311
312
313void TRACE_MANAGER::traceV( const wxString& aWhat, const wxString& aFmt, va_list vargs )
314{
315 if( !IsTraceEnabled( aWhat ) )
316 return;
317
318 wxString str;
319 str.PrintfV( aFmt, vargs );
320
321#ifdef __unix__
322 fprintf( stderr, " %-30s | %s", aWhat.c_str().AsChar(), str.c_str().AsChar() );
323#endif
324}
325
326
328{
329 wxString traceVars;
330 m_globalTraceEnabled = wxGetEnv( wxT( "KICAD_TRACE" ), &traceVars );
331 m_printAllTraces = false;
332
334 return;
335
336 wxStringTokenizer tokenizer( traceVars, wxT( "," ) );
337
338 while( tokenizer.HasMoreTokens() )
339 {
340 wxString token = tokenizer.GetNextToken();
341 m_enabledTraces[token] = true;
342
343 if( token.Lower() == wxT( "all" ) )
344 m_printAllTraces = true;
345 }
346}
@ 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 traceScreen
Flag to enable debug output of BASE_SCREEN and it's derivatives.
const wxChar *const traceSchSheetPaths
Flag to enable debug output of schematic symbol sheet path manipulation code.
const wxChar *const tracePrinting
Flag to enable print controller debug output.
const wxChar *const traceFindItem
Flag to enable find debug tracing.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
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 kicadTraceCoords
Flag to enable draw panel coordinate debug tracing.
const wxChar *const kicadTraceCoroutineStack
Flag to enable tracing of the coroutine call stack.
const wxChar *const traceSchPlugin
Flag to enable legacy schematic plugin debug output.
const wxChar *const traceFindReplace
Flag to enable find and replace debug tracing.
const wxChar *const traceSchLibMem
Flag to enable schematic library memory deletion debug output.
const wxChar *const traceFonts
Flag to enable locale debug output.
const wxChar *const traceSymbolResolver
Flag to enable debug output of symbol library resolver results.
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
const wxChar *const traceKiCad2Step
Flag to enable KiCad2Step debug tracing.
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
const wxChar *const traceLocale
Flag to enable locale debug output.
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
const wxChar *const traceDisplayLocation
Flag to enable debug output of display positioning logic.
const wxChar *const traceGedaPcbPlugin
Flag to enable GEDA PCB plugin debug output.
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
const wxChar *const traceZoomScroll
Flag to enable debug output of zoom-scrolling calculations in KIGFX::ZOOM_CONTROLLER and derivatives.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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