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