-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathLogManagerImpl.cpp
988 lines (875 loc) · 30.9 KB
/
LogManagerImpl.cpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#ifdef _MSC_VER
// evntprov.h(838) : warning C4459 : declaration of 'Version' hides global declaration
#pragma warning(disable : 4459)
#endif
#include "LogManagerImpl.hpp"
#include "mat/config.h"
#include "offline/LogSessionDataProvider.hpp"
#include "offline/OfflineStorageHandler.hpp"
#include "system/TelemetrySystem.hpp"
#include "EventProperty.hpp"
#include "TransmitProfiles.hpp"
#include "http/HttpClientFactory.hpp"
#include "pal/TaskDispatcher.hpp"
#include "utils/Utils.hpp"
#ifdef HAVE_MAT_UTC
#if defined __has_include
#if __has_include("modules/utc/UtcTelemetrySystem.hpp")
#include "modules/utc/UtcTelemetrySystem.hpp"
#else
/* Compiling without UTC support because UTC private header is unavailable */
#undef HAVE_MAT_UTC
#endif
#else
#include "modules/utc/UtcTelemetrySystem.hpp"
#endif
#endif
#ifdef HAVE_MAT_AI
#if defined __has_include
#if __has_include("modules/azmon/AITelemetrySystem.hpp")
#include "modules/azmon/AITelemetrySystem.hpp"
#else
/* Compiling without Azure Monitor support because Azure Monitor private header is unavailable */
#undef HAVE_MAT_AI
#endif
#else
#include "modules/azmon/AITelemetrySystem.hpp"
#endif
#endif // HAVE_MAT_AI
#ifdef HAVE_MAT_DEFAULT_FILTER
#if defined __has_include
#if __has_include("modules/filter/CompliantByDefaultEventFilterModule.hpp")
#include "modules/filter/CompliantByDefaultEventFilterModule.hpp"
#else
/* Compiling without Filtering support because Filtering private header is unavailable */
#undef HAVE_MAT_DEFAULT_FILTER
#endif
#else
#include "modules/filter/LevelChececkingEventFilter.hpp"
#endif
#endif // HAVE_MAT_DEFAULT_FILTER
#ifdef HAVE_MAT_PRIVACYGUARD
#if defined __has_include
#if __has_include("modules/privacyguard/PrivacyGuard.hpp")
#include "modules/privacyguard/PrivacyGuard.hpp"
#else
/* Compiling without Privacy Guard support because Privacy Guard private header is unavailable */
#undef HAVE_MAT_PRIVACYGUARD
#endif
#else
#include "modules/privacyguard/PrivacyGuard.hpp"
#endif
#endif
#ifdef HAVE_MAT_SIGNALS
#if defined __has_include
#if __has_include("modules/signals/Signals.hpp")
#include "modules/signals/Signals.hpp"
#else
/* Compiling without Signals support because Signals private header is unavailable */
#undef HAVE_MAT_SIGNALS
#endif
#else
#include "modules/signals/Signals.hpp"
#endif
#endif
#ifdef HAVE_MAT_SANITIZER
#if defined __has_include
#if __has_include("modules/sanitizer/Sanitizer.hpp")
#include "modules/sanitizer/Sanitizer.hpp"
#else
/* Compiling without Sanitizer support because Santizer private header is unavailable */
#undef HAVE_MAT_SANITIZER
#endif
#else
#include "modules/sanitizer/Sanitizer.hpp"
#endif
#endif
namespace MAT_NS_BEGIN
{
void DeadLoggers::AddMap(LoggerMap&& source)
{
std::lock_guard<std::mutex> lock(m_deadLoggersMutex);
m_deadLoggers.reserve(m_deadLoggers.size() + source.size());
for (auto& kv : source)
{
m_deadLoggers.emplace_back(std::move(kv.second));
assert(!kv.second);
}
source.clear(); // source is dead
}
size_t DeadLoggers::GetDeadLoggerCount() const noexcept
{
std::lock_guard<std::mutex> lock(m_deadLoggersMutex);
return m_deadLoggers.size();
}
bool ILogManager::DispatchEventBroadcast(DebugEvent evt)
{
// LOCKGUARD(ILogManagerInternal::managers_lock);
for (auto instance : ILogManagerInternal::managers)
{
instance->DispatchEvent(evt);
}
return true;
}
MATSDK_LOG_INST_COMPONENT_CLASS(LogManagerImpl, "EventsSDK.LogManager", "Microsoft Telemetry Client - LogManager class");
#if 1
// TODO: integrate Tracing API from v1
// Meanwhile we'd set the g_logLevel using ILogConfiguration settings
static void setLogLevel(ILogConfiguration& configuration)
{
uint32_t minTraceLevel = configuration[CFG_INT_TRACE_LEVEL_MIN];
switch (minTraceLevel)
{
case ACTTraceLevel_Debug:
PAL::detail::g_logLevel = PAL::LogLevel::Detail;
break;
case ACTTraceLevel_Trace:
PAL::detail::g_logLevel = PAL::LogLevel::Detail;
break;
case ACTTraceLevel_Info:
PAL::detail::g_logLevel = PAL::LogLevel::Info;
break;
case ACTTraceLevel_Warn:
PAL::detail::g_logLevel = PAL::LogLevel::Warning;
break;
case ACTTraceLevel_Error:
PAL::detail::g_logLevel = PAL::LogLevel::Error;
break;
case ACTTraceLevel_Fatal:
PAL::detail::g_logLevel = PAL::LogLevel::Error;
break;
default:
PAL::detail::g_logLevel = PAL::LogLevel::Warning;
break;
}
}
#endif
DeadLoggers LogManagerImpl::s_deadLoggers;
LogManagerImpl::LogManagerImpl(ILogConfiguration& configuration) :
LogManagerImpl(configuration, false /*deferSystemStart*/)
{
}
LogManagerImpl::LogManagerImpl(ILogConfiguration& configuration, bool deferSystemStart) :
m_logConfiguration(configuration),
m_bandwidthController(nullptr),
m_offlineStorage(nullptr)
{
m_httpClient = std::static_pointer_cast<IHttpClient>(configuration.GetModule(CFG_MODULE_HTTP_CLIENT));
m_taskDispatcher = std::static_pointer_cast<ITaskDispatcher>(configuration.GetModule(CFG_MODULE_TASK_DISPATCHER));
m_dataViewer = std::static_pointer_cast<IDataViewer>(configuration.GetModule(CFG_MODULE_DATA_VIEWER));
m_customDecorator = std::static_pointer_cast<IDecoratorModule>(configuration.GetModule(CFG_MODULE_DECORATOR));
m_config = std::unique_ptr<IRuntimeConfig>(new RuntimeConfig_Default(m_logConfiguration));
setLogLevel(configuration);
LOG_TRACE("New LogManager instance");
PAL::initialize(*m_config);
PAL::registerSemanticContext(&m_context);
std::string cacheFilePath = MAT::GetAppLocalTempDirectory();
if (!m_logConfiguration.HasConfig(CFG_STR_CACHE_FILE_PATH) ||
(const char*)(m_logConfiguration[CFG_STR_CACHE_FILE_PATH]) == nullptr)
{
if (m_logConfiguration.HasConfig(CFG_STR_PRIMARY_TOKEN))
{
std::string tenantId = (const char*)m_logConfiguration[CFG_STR_PRIMARY_TOKEN];
tenantId = MAT::tenantTokenToId(tenantId);
if ((!cacheFilePath.empty()) && (cacheFilePath.back() != PATH_SEPARATOR_CHAR))
{
// append path separator if required
cacheFilePath += PATH_SEPARATOR_CHAR;
}
cacheFilePath += tenantId;
cacheFilePath += ".db";
}
else
{
cacheFilePath = ":memory:";
}
m_logConfiguration[CFG_STR_CACHE_FILE_PATH] = cacheFilePath;
}
else
{
std::string filename = (const char*)m_logConfiguration[CFG_STR_CACHE_FILE_PATH];
if (filename.find(PATH_SEPARATOR_CHAR) == std::string::npos)
{
cacheFilePath += filename;
m_logConfiguration[CFG_STR_CACHE_FILE_PATH] = cacheFilePath;
}
}
if (m_logConfiguration.HasConfig(CFG_STR_TRANSMIT_PROFILES))
{
std::string transmitProfiles = m_logConfiguration[CFG_STR_TRANSMIT_PROFILES];
if (!transmitProfiles.empty())
{
LOG_INFO("Loading custom transmit profiles...");
LoadTransmitProfiles(transmitProfiles);
}
}
if (m_logConfiguration.HasConfig(CFG_STR_START_PROFILE_NAME))
{
std::string transmitProfile = m_logConfiguration[CFG_STR_START_PROFILE_NAME];
if (!transmitProfile.empty())
{
LOG_INFO("Setting custom transmit profile %s", transmitProfile.c_str());
SetTransmitProfile(transmitProfile);
}
}
m_context.SetCommonField(SESSION_ID_LEGACY, PAL::generateUuidString());
if (m_dataViewer != nullptr)
{
m_dataViewerCollection.RegisterViewer(m_dataViewer);
}
if (m_taskDispatcher == nullptr)
{
m_taskDispatcher = PAL::getDefaultTaskDispatcher();
}
else
{
LOG_TRACE("TaskDispatcher: External %p", m_taskDispatcher.get());
}
int32_t sdkMode = configuration[CFG_INT_SDK_MODE];
(void)sdkMode; // variable may be unused when SDK is compiled without private modules
#ifdef HAVE_MAT_UTC
// UTC is not active
configuration[CFG_STR_UTC][CFG_BOOL_UTC_ACTIVE] = false;
// UTC functionality is only available on Windows 10 RS2+
bool isWindowsUtcClientRegistrationEnable = PAL::IsUtcRegistrationEnabledinWindows();
configuration[CFG_STR_UTC][CFG_BOOL_UTC_ENABLED] = isWindowsUtcClientRegistrationEnable;
if (
((sdkMode == SdkModeTypes::SdkModeTypes_UTCBackCompat) || (sdkMode == SdkModeTypes::SdkModeTypes_UTCCommonSchema)) &&
isWindowsUtcClientRegistrationEnable)
{
// UTC is active
configuration[CFG_STR_UTC][CFG_BOOL_UTC_ACTIVE] = true;
LOG_TRACE("Initializing UTC physical layer...");
m_system.reset(new UtcTelemetrySystem(*this, *m_config, *m_taskDispatcher));
if (!deferSystemStart)
{
m_system->start();
m_isSystemStarted = true;
}
m_alive = true;
LOG_INFO("Started up and running in UTC mode");
return;
}
#endif
#ifdef HAVE_MAT_DEFAULT_HTTP_CLIENT
if (m_httpClient == nullptr)
{
m_httpClient = HttpClientFactory::Create();
#ifdef HAVE_MAT_WININET_HTTP_CLIENT
HttpClient_WinInet* client = static_cast<HttpClient_WinInet*>(m_httpClient.get());
if (client != nullptr)
{
client->SetMsRootCheck(m_logConfiguration[CFG_MAP_HTTP][CFG_BOOL_HTTP_MS_ROOT_CHECK]);
}
#endif
}
else
{
LOG_TRACE("HttpClient: External %p", m_httpClient.get());
}
#else
if (m_httpClient == nullptr)
{
LOG_ERROR("No valid IHTTPClient passed to LogManagerImpl's ILogConfiguration.");
MATSDK_THROW(std::invalid_argument("configuration"));
}
#endif
if (m_bandwidthController == nullptr)
{
m_bandwidthController = m_ownBandwidthController.get();
}
else
{
LOG_TRACE("BandwidthController: External %p", m_bandwidthController);
}
if (m_bandwidthController == nullptr)
{
LOG_TRACE("BandwidthController: None");
}
m_offlineStorage.reset(new OfflineStorageHandler(*this, *m_config, *m_taskDispatcher));
#if defined(STORE_SESSION_DB) && defined(HAVE_MAT_STORAGE)
m_logSessionDataProvider.reset(new LogSessionDataProvider(m_offlineStorage.get()));
#else
m_logSessionDataProvider.reset(new LogSessionDataProvider(cacheFilePath));
#endif
#ifdef HAVE_MAT_AI
if (sdkMode == SdkModeTypes::SdkModeTypes_AI)
{
m_system.reset(new AITelemetrySystem(*this, *m_config, *m_offlineStorage, *m_httpClient,
*m_taskDispatcher, m_bandwidthController, *m_logSessionDataProvider));
}
else
#endif
{
// Default mode is Common Schema - direct
m_system.reset(new TelemetrySystem(*this, *m_config, *m_offlineStorage, *m_httpClient,
*m_taskDispatcher, m_bandwidthController, *m_logSessionDataProvider));
}
LOG_TRACE("Telemetry system created, starting up...");
if (m_system && !deferSystemStart)
{
m_system->start();
m_isSystemStarted = true;
}
#ifdef HAVE_MAT_DEFAULT_FILTER
m_modules.push_back(std::unique_ptr<CompliantByDefaultEventFilterModule>(new CompliantByDefaultEventFilterModule()));
#endif // HAVE_MAT_DEFAULT_FILTER
LOG_INFO("Initializing Modules");
InitializeModules();
LOG_INFO("Started up and running");
m_alive = true;
}
/// <summary>
/// Reconfigure this ILogManager instance using current snapshot of ILogConfiguration
/// </summary>
void LogManagerImpl::Configure()
{
// TODO: [maxgolov] - add other config params.
#ifdef HAVE_MAT_WININET_HTTP_CLIENT
HttpClient_WinInet* client = static_cast<HttpClient_WinInet*>(m_httpClient.get());
if (client != nullptr)
{
client->SetMsRootCheck(m_logConfiguration[CFG_MAP_HTTP][CFG_BOOL_HTTP_MS_ROOT_CHECK]);
}
#endif
};
LogManagerImpl::~LogManagerImpl() noexcept
{
FlushAndTeardown();
LOCKGUARD(ILogManagerInternal::managers_lock);
ILogManagerInternal::managers.erase(this);
}
size_t LogManagerImpl::GetDeadLoggerCount()
{
return s_deadLoggers.GetDeadLoggerCount();
}
void LogManagerImpl::FlushAndTeardown()
{
PauseActivity();
WaitPause();
LOG_INFO("Shutting down...");
LOCKGUARD(m_lock);
if (m_alive)
{
if (m_logConfiguration[CFG_BOOL_DISABLE_ZOMBIE_LOGGERS])
{
m_loggers.clear();
}
else
{
// before we do anything else, move our Logger instances
// to the shut-down state and the s_deadLoggers graveyard.
// Calls to these loggers will be benign: before we complete
// their RecordShutdown() call, this LogManagerImpl is alive
// and well and ILogger methods should work. After that
// RecordShutdown(), those ILogger methods do nothing and in
// particular do not touch this now-defunct LogManagerImpl.
for (auto& kv : m_loggers)
{
// this waits until no active calls on this logger
kv.second->RecordShutdown();
}
s_deadLoggers.AddMap(std::move(m_loggers));
// Ensure that AddMap clears m_loggers (it does, it should continue to).
assert(m_loggers.empty());
}
LOG_INFO("Tearing down modules");
TeardownModules();
if (m_isSystemStarted && m_system)
{
m_system->stop();
LOG_TRACE("Telemetry system stopped");
}
m_system = nullptr;
m_offlineStorage.reset();
m_ownBandwidthController.reset();
m_bandwidthController = nullptr;
m_httpClient = nullptr;
m_taskDispatcher = nullptr;
m_dataViewer = nullptr;
ClearDataInspectors();
m_filters.UnregisterAllFilters();
auto shutTime = GetUptimeMs();
PAL::shutdown();
shutTime = GetUptimeMs() - shutTime;
LOG_INFO("Shutdown complete in %lld ms", shutTime);
}
m_alive = false;
}
status_t LogManagerImpl::Flush()
{
LOG_INFO("Flush()");
if (m_offlineStorage)
m_offlineStorage->Flush();
return STATUS_SUCCESS;
}
status_t LogManagerImpl::UploadNow()
{
LOCKGUARD(m_lock);
if (GetSystem())
{
GetSystem()->upload();
}
return STATUS_SUCCESS;
}
status_t LogManagerImpl::PauseTransmission()
{
LOG_INFO("Pausing transmission, cancelling any outstanding uploads...");
LOCKGUARD(m_lock);
if (GetSystem())
{
GetSystem()->pause();
}
return STATUS_SUCCESS;
}
status_t LogManagerImpl::ResumeTransmission()
{
LOG_INFO("Resuming transmission...");
LOCKGUARD(m_lock);
if (GetSystem())
{
GetSystem()->resume();
}
return STATUS_SUCCESS;
}
/// <summary>Sets the transmit profile by enum</summary>
/// <param name="profile">Profile enum</param>
status_t LogManagerImpl::SetTransmitProfile(TransmitProfile profile)
{
bool result = TransmitProfiles::setDefaultProfile(profile);
return (result) ? STATUS_SUCCESS : STATUS_EFAIL;
}
/// <summary>
/// Select one of several predefined transmission profiles.
/// </summary>
/// <param name="profile"></param>
status_t LogManagerImpl::SetTransmitProfile(const std::string& profile)
{
LOG_INFO("SetTransmitProfile: profile=%s", profile.c_str());
bool result = TransmitProfiles::setProfile(profile);
return (result) ? STATUS_SUCCESS : STATUS_EFAIL;
}
/// <summary>
/// Load transmit profiles from JSON config
/// </summary>
/// <param name="profiles_json">JSON config (see example above)</param>
/// <returns>true on successful profiles load, false if config is invalid</returns>
status_t LogManagerImpl::LoadTransmitProfiles(const std::string& profiles_json)
{
LOG_INFO("LoadTransmitProfiles");
bool result = TransmitProfiles::load(profiles_json);
return (result) ? STATUS_SUCCESS : STATUS_EFAIL;
}
status_t LogManagerImpl::LoadTransmitProfiles(const std::vector<TransmitProfileRules>& profiles) noexcept
{
LOG_INFO("LoadTransmitProfiles");
bool result = TransmitProfiles::load(profiles);
return (result) ? STATUS_SUCCESS : STATUS_EFAIL;
}
/// <summary>
/// Reset transmission profiles to default settings
/// </summary>
status_t LogManagerImpl::ResetTransmitProfiles()
{
LOG_INFO("ResetTransmitProfiles");
TransmitProfiles::reset();
return STATUS_SUCCESS;
}
const std::string& LogManagerImpl::GetTransmitProfileName()
{
return TransmitProfiles::getProfile();
};
ISemanticContext& LogManagerImpl::GetSemanticContext()
{
return m_context;
}
/// <summary>
/// Set global context field - string
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(std::string const& name, std::string const& value, PiiKind piiKind)
{
LOG_TRACE("SetContext(\"%s\", ..., %u)", name.c_str(), piiKind);
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
{
LOCKGUARD(m_dataInspectorGuard);
for(const auto& dataInspector : m_dataInspectors)
{
dataInspector->InspectSemanticContext(name, value, /*isGlobalContext: */ true, std::string{});
}
}
return STATUS_SUCCESS;
}
/// <summary>
/// Set global context field - double
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(const std::string& name, double value, PiiKind piiKind)
{
LOG_INFO("SetContext");
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
return STATUS_SUCCESS;
}
/// <summary>
/// Set global context field - int64
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(const std::string& name, int64_t value, PiiKind piiKind)
{
LOG_INFO("SetContext");
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
return STATUS_SUCCESS;
}
/// <summary>
/// Set global context field - boolean
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(const std::string& name, bool value, PiiKind piiKind)
{
LOG_INFO("SetContext");
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
return STATUS_SUCCESS;
}
/// <summary>
/// Set global context field - date/time in .NET ticks
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(const std::string& name, time_ticks_t value, PiiKind piiKind)
{
LOG_INFO("SetContext");
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
return STATUS_SUCCESS;
}
/// <summary>
/// Set global context field - GUID
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="piiKind"></param>
status_t LogManagerImpl::SetContext(const std::string& name, GUID_t value, PiiKind piiKind)
{
LOG_INFO("SetContext");
EventProperty prop(value, piiKind);
m_context.SetCustomField(name, prop);
m_context.SetCustomField(name, prop);
{
LOCKGUARD(m_dataInspectorGuard);
for(const auto& dataInspector : m_dataInspectors)
{
dataInspector->InspectSemanticContext(name, value, /*isGlobalContext: */ true, std::string{});
}
}
return STATUS_SUCCESS;
}
/// <summary>
/// Obtain current ILogConfiguration instance associated with the LogManager
/// </summary>
ILogConfiguration& LogManagerImpl::GetLogConfiguration()
{
return m_logConfiguration;
}
ILogger* LogManagerImpl::GetLogger(const std::string& tenantToken, const std::string& source, const std::string& scope)
{
{
LOCKGUARD(m_lock);
if (!m_alive)
{
return nullptr;
}
}
//
LOG_TRACE("GetLogger(tenantId=\"%s\", source=\"%s\")", tenantTokenToId(tenantToken).c_str(), source.c_str());
std::string normalizedTenantToken = toLower(tenantToken);
std::string normalizedSource = toLower(source);
std::string hash = normalizedTenantToken + "/" + normalizedSource;
LOCKGUARD(m_lock);
if (!m_alive)
{
return nullptr;
}
auto it = m_loggers.find(hash);
if (it == std::end(m_loggers))
{
m_loggers[hash] = std::make_unique<Logger>(
normalizedTenantToken, normalizedSource, scope,
*this, m_context, *m_config);
}
uint8_t level = m_diagLevelFilter.GetDefaultLevel();
if (level != DIAG_LEVEL_DEFAULT)
{
m_loggers[hash]->SetLevel(level);
}
return m_loggers[hash].get();
}
/// <summary>
/// Adds the event listener.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="listener">The listener.</param>
void LogManagerImpl::AddEventListener(DebugEventType type, DebugEventListener& listener)
{
m_debugEventSource.AddEventListener(type, listener);
};
/// <summary>
/// Removes the event listener.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="listener">The listener.</param>
void LogManagerImpl::RemoveEventListener(DebugEventType type, DebugEventListener& listener)
{
m_debugEventSource.RemoveEventListener(type, listener);
};
/// <summary>
/// Dispatches the event.
/// </summary>
/// <param name="evt">The evt.</param>
/// <returns></returns>
bool LogManagerImpl::DispatchEvent(DebugEvent evt)
{
return m_debugEventSource.DispatchEvent(std::move(evt));
};
/// <summary>Attach cascaded DebugEventSource to forward all events to</summary>
bool LogManagerImpl::AttachEventSource(DebugEventSource& other)
{
return m_debugEventSource.AttachEventSource(other);
}
/// <summary>Detach cascaded DebugEventSource to forward all events to</summary>
bool LogManagerImpl::DetachEventSource(DebugEventSource& other)
{
return m_debugEventSource.DetachEventSource(other);
}
void LogManagerImpl::sendEvent(IncomingEventContextPtr const& event)
{
LOCKGUARD(m_lock);
if (GetSystem())
{
if (m_customDecorator)
{
m_customDecorator->decorate(*(event->source));
}
{
LOCKGUARD(m_dataInspectorGuard);
for (const auto& dataInspector : m_dataInspectors)
{
dataInspector->InspectRecord(*(event->source));
}
}
GetSystem()->sendEvent(event);
}
}
ILogController* LogManagerImpl::GetLogController()
{
return this;
}
IAuthTokensController* LogManagerImpl::GetAuthTokensController()
{
return &m_authTokensController;
}
IEventFilterCollection& LogManagerImpl::GetEventFilters() noexcept
{
return m_filters;
}
const IEventFilterCollection& LogManagerImpl::GetEventFilters() const noexcept
{
return m_filters;
}
LogSessionData* LogManagerImpl::GetLogSessionData()
{
return (m_logSessionDataProvider) ? m_logSessionDataProvider->GetLogSessionData() : nullptr;
}
void LogManagerImpl::ResetLogSessionData()
{
if (m_logSessionDataProvider)
{
m_logSessionDataProvider->ResetLogSessionData();
}
}
void LogManagerImpl::SetLevelFilter(uint8_t defaultLevel, uint8_t levelMin, uint8_t levelMax)
{
m_diagLevelFilter.SetFilter(defaultLevel, levelMin, levelMax);
}
void LogManagerImpl::SetLevelFilter(uint8_t defaultLevel, const std::set<uint8_t>& allowedLevels)
{
m_diagLevelFilter.SetFilter(defaultLevel, allowedLevels);
}
const DiagLevelFilter& LogManagerImpl::GetLevelFilter()
{
return m_diagLevelFilter;
}
std::unique_ptr<ITelemetrySystem>& LogManagerImpl::GetSystem()
{
if (m_system == nullptr || m_isSystemStarted)
return m_system;
m_system->start();
m_isSystemStarted = true;
return m_system;
}
void LogManagerImpl::InitializeModules() noexcept
{
for (const auto& module : m_modules)
{
module->Initialize(this);
}
}
void LogManagerImpl::TeardownModules() noexcept
{
for (const auto& module : m_modules)
{
module->Teardown();
}
std::vector<std::unique_ptr<IModule>>{}.swap(m_modules);
}
const IDataViewerCollection& LogManagerImpl::GetDataViewerCollection() const
{
return m_dataViewerCollection;
}
IDataViewerCollection& LogManagerImpl::GetDataViewerCollection()
{
return m_dataViewerCollection;
}
void LogManagerImpl::SetDataInspector(const std::shared_ptr<IDataInspector>& dataInspector)
{
LOCKGUARD(m_dataInspectorGuard);
if(dataInspector == nullptr)
{
LOG_WARN("Attempting to set nullptr as DataInspector");
return;
}
auto itDataInspector = std::find_if(m_dataInspectors.begin(), m_dataInspectors.end(), [&dataInspector](const std::shared_ptr<IDataInspector>& currentInspector)
{
return strcmp(dataInspector->GetName(), currentInspector->GetName()) == 0;
});
if (itDataInspector != m_dataInspectors.end())
{
LOG_WARN("Replacing specified IDataInspector with passed in inspector");
m_dataInspectors.erase(itDataInspector);
}
m_dataInspectors.push_back(dataInspector);
}
void LogManagerImpl::ClearDataInspectors()
{
LOCKGUARD(m_dataInspectorGuard);
std::vector<std::shared_ptr<IDataInspector>>{}.swap(m_dataInspectors);
}
void LogManagerImpl::RemoveDataInspector(const std::string& name)
{
LOCKGUARD(m_dataInspectorGuard);
auto itDataInspector = std::find_if(m_dataInspectors.begin(), m_dataInspectors.end(), [&name](const std::shared_ptr<IDataInspector>& inspector){
return strcmp(inspector->GetName(), name.c_str()) == 0;
});
if (itDataInspector != m_dataInspectors.end())
{
m_dataInspectors.erase(itDataInspector);
}
}
std::shared_ptr<IDataInspector> LogManagerImpl::GetDataInspector(const std::string& name) noexcept
{
LOCKGUARD(m_dataInspectorGuard);
auto it = std::find_if(m_dataInspectors.begin(), m_dataInspectors.end(), [&name](const std::shared_ptr<IDataInspector>& inspector){
return strcmp(inspector->GetName(), name.c_str()) == 0;
});
return it != m_dataInspectors.end() ? *it : nullptr;
}
status_t LogManagerImpl::DeleteData()
{
LOCKGUARD(m_lock);
if (GetSystem())
{
// cleanup pending http requests
GetSystem()->cleanup();
// cleanup log session ( UUID)
if (m_logSessionDataProvider)
{
m_logSessionDataProvider->DeleteLogSessionData();
}
// cleanup offline storage ( this will also cleanup retry queue for http requests
if (m_offlineStorage)
{
m_offlineStorage->DeleteAllRecords();
}
}
return STATUS_SUCCESS;
}
// Pause/Resume interface
void LogManagerImpl::PauseActivity()
{
std::unique_lock<std::mutex> lock(m_pause_mutex);
if (m_pause_state != PauseState::Active) {
return;
}
if (m_pause_active_count == 0)
{
m_pause_state = PauseState::Paused;
// notify under lock because a waiting thread
// may destroy objects (including this object)
m_pause_cv.notify_all();
}
else
{
m_pause_state = PauseState::Pausing;
}
}
void LogManagerImpl::ResumeActivity()
{
std::unique_lock<std::mutex> lock(m_pause_mutex);
if (m_pause_state == PauseState::Active) {
return;
}
m_pause_state = PauseState::Active;
m_pause_cv.notify_all();
}
void LogManagerImpl::WaitPause()
{
std::unique_lock<std::mutex> lock(m_pause_mutex);
if (m_pause_state != PauseState::Pausing) {
return;
}
m_pause_cv.wait(lock, [this]() -> bool {
return m_pause_state != PauseState::Pausing;
});
}
bool LogManagerImpl::StartActivity()
{
std::unique_lock<std::mutex> lock(m_pause_mutex);
if (m_pause_state != PauseState::Active) {
return false;
}
m_pause_active_count += 1;
return true;
}
void LogManagerImpl::EndActivity()
{
std::unique_lock<std::mutex> lock(m_pause_mutex);
if (m_pause_active_count == 0) {
return;
}
m_pause_active_count -= 1;
if (m_pause_active_count > 0) {
return;
}
if (m_pause_state == PauseState::Pausing) {
m_pause_state = PauseState::Paused;
m_pause_cv.notify_all();
}
}
}
MAT_NS_END