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