KiCad PCB EDA Suite
Loading...
Searching...
No Matches
app_monitor.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 The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <app_monitor.h>
25#include <pgm_base.h>
26#include <magic_enum.hpp>
27#include <kiplatform/policy.h>
28#include <policy_keys.h>
29#include <paths.h>
30#include <wx/filename.h>
31#include <wx/ffile.h>
32
33#include <boost/uuid/uuid_io.hpp>
34#include <boost/uuid/uuid_generators.hpp>
35#include <build_version.h>
36
37#ifdef KICAD_USE_SENTRY
38#include <sentry.h>
39#endif
40
41using namespace APP_MONITOR;
42
43
45 m_isOptedIn( false )
46{
47}
48
49
51{
52#ifdef KICAD_USE_SENTRY
53 sentryInit();
54#endif
55}
56
57
59{
60#ifdef KICAD_USE_SENTRY
61 sentry_close();
62#endif
63}
64
65
66void SENTRY::AddTag( const wxString& aKey, const wxString& aValue )
67{
68#ifdef KICAD_USE_SENTRY
69 sentry_set_tag( aKey.c_str(), aValue.c_str() );
70#endif
71}
72
73
74void SENTRY::SetSentryOptIn( bool aOptIn )
75{
76#ifdef KICAD_USE_SENTRY
77 if( aOptIn )
78 {
79 // Opting in (when not opted in before) may require reading the uid off disk
80 // as it was skipped previously.
82
83 if( !m_sentry_optin_fn.Exists() )
84 {
85 wxFFile sentryInitFile( m_sentry_optin_fn.GetFullPath(), "w" );
86 sentryInitFile.Write( "" );
87 sentryInitFile.Close();
88 }
89
90 m_isOptedIn = true;
91 }
92 else
93 {
94 if( m_sentry_optin_fn.Exists() )
95 {
96 wxRemoveFile( m_sentry_optin_fn.GetFullPath() );
97 sentry_close();
98 }
99
100 m_isOptedIn = false;
101 }
102#else
103 m_isOptedIn = false;
104#endif
105}
106
107
109{
110 boost::uuids::uuid uuid = boost::uuids::random_generator()();
111 wxString userGuid = boost::uuids::to_string( uuid );
112
113 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath(), "w" );
114 sentryInitFile.Write( userGuid );
115 sentryInitFile.Close();
116
117 return userGuid;
118}
119
120
125
126
127const wxString& SENTRY::GetSentryId()
128{
129 return m_sentryUid;
130}
131
132
134{
135 if( m_sentry_optin_fn.Exists() )
136 {
137 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath() );
138 sentryInitFile.ReadAll( &m_sentryUid );
139 sentryInitFile.Close();
140 }
141
142 if( m_sentryUid.IsEmpty() || m_sentryUid.length() != 36 )
143 {
145 }
146}
147
148
150{
151#ifdef KICAD_USE_SENTRY
152 m_sentry_optin_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-opt-in" );
153 m_sentry_uid_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-uid" );
154
155 if( isConfiguredOptedIn() )
156 {
157 m_isOptedIn = true;
158
160
161 sentry_options_t* options = sentry_options_new();
162
163#ifndef KICAD_SENTRY_DSN
164#error "Project configuration error, missing KICAD_SENTRY_DSN"
165#endif
166 // only capture 5% of transactions
167 sentry_options_set_traces_sample_rate( options, 0.05 );
168 sentry_options_set_dsn( options, KICAD_SENTRY_DSN );
169
170#ifdef __WXMAC__
171 wxString handlerPath = PATHS::GetExecutablePath() + wxT( "Contents/MacOS/crashpad_handler" );
172
173 if( wxFileExists( handlerPath ) )
174 sentry_options_set_handler_path( options, handlerPath.c_str() );
175#endif
176
177 wxFileName tmp;
178 tmp.AssignDir( PATHS::GetUserCachePath() );
179 tmp.AppendDir( "sentry" );
180
181#ifdef __WINDOWS__
182 sentry_options_set_database_pathw( options, tmp.GetPathWithSep().wc_str() );
183#else
184 sentry_options_set_database_path( options, tmp.GetPathWithSep().c_str() );
185#endif
186 sentry_options_set_symbolize_stacktraces( options, true );
187 sentry_options_set_auto_session_tracking( options, false );
188
189 sentry_options_set_release( options, GetCommitHash().ToStdString().c_str() );
190
191 // This just gives us more filtering within sentry, issues still get grouped across
192 // environments.
193 sentry_options_set_environment( options, GetMajorMinorVersion().c_str() );
194
195 sentry_init( options );
196
197 sentry_value_t user = sentry_value_new_object();
198 sentry_value_set_by_key( user, "id", sentry_value_new_string( m_sentryUid.c_str() ) );
199 sentry_set_user( user );
200
201 sentry_set_tag( "kicad.version", GetBuildVersion().ToStdString().c_str() );
202 }
203#endif
204}
205
206
208{
209 // policy takes precedence
212 {
213 return policyState == KIPLATFORM::POLICY::PBOOL::ENABLED;
214 }
215
216 return m_sentry_optin_fn.Exists();
217}
218
219
221{
222#ifdef KICAD_USE_SENTRY
223 // always override with policy, just in case
226 {
227 return policyState == KIPLATFORM::POLICY::PBOOL::ENABLED;
228 }
229
230 return m_isOptedIn;
231#else
232 return false;
233#endif
234}
235
236
237void SENTRY::LogAssert( const ASSERT_CACHE_KEY& aKey, const wxString& aAssertMsg )
238{
239#ifdef KICAD_USE_SENTRY
241 {
242 return;
243 }
244
245 // We use an assert cache to avoid collecting too many events
246 // Because they can come from functions that are called hundreds of times in loops
247 if( m_assertCache.find( aKey ) == m_assertCache.end() )
248 {
249 sentry_value_t exc = sentry_value_new_exception( "assert", aAssertMsg.c_str() );
250 sentry_value_set_stacktrace( exc, NULL, 0 );
251
252 sentry_value_t sentryEvent = sentry_value_new_event();
253 sentry_event_add_exception( sentryEvent, exc );
254 sentry_capture_event( sentryEvent );
255 m_assertCache.insert( aKey );
256 }
257#endif
258}
259
260
261void SENTRY::LogException( const wxString& aMsg, bool aUnhandled )
262{
263#ifdef KICAD_USE_SENTRY
265 {
266 return;
267 }
268
269 sentry_scope_t* local_scope = sentry_local_scope_new();
270 sentry_scope_set_tag( local_scope, "unhandled", aUnhandled ? "true" : "false" );
271
272 sentry_value_t exc = sentry_value_new_exception( "exception", aMsg.c_str() );
273 sentry_value_set_stacktrace( exc, NULL, 0 );
274
275 sentry_value_t sentryEvent = sentry_value_new_event();
276 sentry_event_add_exception( sentryEvent, exc );
277 sentry_capture_event_with_scope( sentryEvent, local_scope );
278#endif
279}
280
281#ifdef KICAD_USE_SENTRY
282static std::string GetSentryBreadCrumbType( BREADCRUMB_TYPE aType )
283{
284 // the only special case due to collisions with defines
285 if( aType == BREADCRUMB_TYPE::DBG )
286 return "debug";
287
288 if( aType == BREADCRUMB_TYPE::ERR )
289 return "error";
290
291 std::string ret( magic_enum::enum_name( aType ) );
292
293 std::transform( ret.begin(), ret.end(), ret.begin(),
294 []( unsigned char c )
295 {
296 return std::tolower( c );
297 } );
298
299 return ret;
300}
301
302
303static std::string GetSentryBreadCrumbLevel( BREADCRUMB_LEVEL aLevel )
304{
305 // the only special case due to collisions with defines
306 if( aLevel == BREADCRUMB_LEVEL::DBG )
307 return "debug";
308
309 if( aLevel == BREADCRUMB_LEVEL::ERR )
310 return "error";
311
312 std::string ret( magic_enum::enum_name( aLevel ) );
313
314 std::transform( ret.begin(), ret.end(), ret.begin(),
315 []( unsigned char c )
316 {
317 return std::tolower( c );
318 } );
319
320 return ret;
321}
322#endif
323
324namespace APP_MONITOR
325{
326SENTRY* SENTRY::m_instance = nullptr;
327
328
329bool operator<( const ASSERT_CACHE_KEY& aKey1, const ASSERT_CACHE_KEY& aKey2 )
330{
331 if( aKey1.file != aKey2.file )
332 return aKey1.file < aKey2.file;
333
334 if( aKey1.line != aKey2.line )
335 return aKey1.line < aKey2.line;
336
337 if( aKey1.func != aKey2.func )
338 return aKey1.func < aKey2.func;
339
340 return aKey1.cond < aKey2.cond;
341}
342
343
344void AddBreadcrumb( BREADCRUMB_TYPE aType, const wxString& aMsg, const wxString& aCategory, BREADCRUMB_LEVEL aLevel )
345{
346#ifdef KICAD_USE_SENTRY
347 if( !SENTRY::Instance()->IsOptedIn() )
348 {
349 return;
350 }
351
352 std::string type = GetSentryBreadCrumbType( aType );
353 sentry_value_t crumb = sentry_value_new_breadcrumb( type.c_str(), aMsg.c_str() );
354
355 sentry_value_set_by_key( crumb, "category", sentry_value_new_string( aCategory.c_str() ) );
356
357 if( aType == BREADCRUMB_TYPE::ERR )
358 {
359 std::string level = GetSentryBreadCrumbLevel( aLevel );
360 sentry_value_set_by_key( crumb, "level", sentry_value_new_string( level.c_str() ) );
361 }
362
363 sentry_add_breadcrumb( crumb );
364#endif
365}
366
367
368void AddNavigationBreadcrumb( const wxString& aMsg, const wxString& aCategory )
369{
370#ifdef KICAD_USE_SENTRY
372#endif
373}
374
375
376void AddTransactionBreadcrumb( const wxString& aMsg, const wxString& aCategory )
377{
378#ifdef KICAD_USE_SENTRY
380#endif
381}
382
383#ifdef KICAD_USE_SENTRY
384class TRANSACTION_IMPL
385{
386public:
387 TRANSACTION_IMPL( const std::string& aName, const std::string& aOperation )
388 {
389 m_ctx = sentry_transaction_context_new( aName.c_str(), aOperation.c_str() );
390 }
391
392 ~TRANSACTION_IMPL() {
393 Finish();
394
395 // note m_ctx is handled by sentry
396 }
397
398 void Start()
399 {
400 m_tx = sentry_transaction_start( m_ctx, sentry_value_new_null() );
401 }
402
403 void Finish()
404 {
405 FinishSpan();
406
407 if( m_tx )
408 {
409 sentry_transaction_finish( m_tx );
410 m_tx = nullptr;
411 }
412 }
413
414 void StartSpan( const std::string& aOperation, const std::string& aDescription )
415 {
416 if( m_span )
417 return;
418
419 if( !m_tx )
420 return;
421
422 m_span = sentry_transaction_start_child( m_tx, aOperation.c_str(), aDescription.c_str() );
423 }
424
425 void FinishSpan()
426 {
427 if( m_span )
428 {
429 sentry_span_finish( m_span );
430 m_span = nullptr;
431 }
432 }
433
434private:
435 sentry_transaction_context_t* m_ctx = nullptr;
436 sentry_transaction_t* m_tx = nullptr;
437 sentry_span_t* m_span = nullptr;
438};
439#endif
440}
441
442
443TRANSACTION::TRANSACTION( const std::string& aName, const std::string& aOperation )
444{
445#ifdef KICAD_USE_SENTRY
446 if( SENTRY::Instance()->IsOptedIn() )
447 {
448 m_impl = new TRANSACTION_IMPL( aName, aOperation );
449 }
450#endif
451}
452
453
455{
456#ifdef KICAD_USE_SENTRY
457 delete m_impl;
458#endif
459}
460
461
463{
464#ifdef KICAD_USE_SENTRY
465 if( m_impl )
466 {
467 m_impl->Start();
468 }
469#endif
470}
471
472
473void TRANSACTION::StartSpan( const std::string& aOperation, const std::string& aDescription )
474{
475#ifdef KICAD_USE_SENTRY
476 if( m_impl )
477 {
478 m_impl->StartSpan( aOperation, aDescription );
479 }
480#endif
481}
482
483
485{
486#ifdef KICAD_USE_SENTRY
487 if( m_impl )
488 {
489 m_impl->Finish();
490 }
491#endif
492}
493
494
496{
497#ifdef KICAD_USE_SENTRY
498 if( m_impl )
499 {
500 m_impl->FinishSpan();
501 }
502#endif
503}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
wxString GetCommitHash()
Get the commit hash as a string.
wxString GetBuildVersion()
Get the full KiCad version string.
This is a singleton class intended to manage sentry.
std::set< ASSERT_CACHE_KEY > m_assertCache
wxString sentryCreateUid()
wxFileName m_sentry_uid_fn
void LogAssert(const ASSERT_CACHE_KEY &aKey, const wxString &aMsg)
void SetSentryOptIn(bool aOptIn)
void LogException(const wxString &aMsg, bool aUnhandled)
void AddTag(const wxString &aKey, const wxString &aValue)
static SENTRY * m_instance
wxFileName m_sentry_optin_fn
static SENTRY * Instance()
const wxString & GetSentryId()
void StartSpan(const std::string &aOperation, const std::string &aDescription)
TRANSACTION(const std::string &aName, const std::string &aOperation)
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition paths.cpp:460
static const wxString & GetExecutablePath()
Definition paths.cpp:671
void AddTransactionBreadcrumb(const wxString &aMsg, const wxString &aCategory)
Add a transaction breadcrumb.
void AddBreadcrumb(BREADCRUMB_TYPE aType, const wxString &aMsg, const wxString &aCategory, BREADCRUMB_LEVEL aLevel)
Add a sentry breadcrumb.
void AddNavigationBreadcrumb(const wxString &aMsg, const wxString &aCategory)
Add a navigation breadcrumb.
bool operator<(const ASSERT_CACHE_KEY &aKey1, const ASSERT_CACHE_KEY &aKey2)
PBOOL GetPolicyBool(const wxString &aKey)
see class PGM_BASE
#define POLICY_KEY_DATACOLLECTION
Definition policy_keys.h:30
This struct represents a key being used for the std::set that deduplicates asserts during this runnin...
Definition app_monitor.h:90