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