-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathODWLogManager.mm
More file actions
342 lines (296 loc) · 9.67 KB
/
ODWLogManager.mm
File metadata and controls
342 lines (296 loc) · 9.67 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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#import <Foundation/Foundation.h>
#import "ODWLogConfiguration.h"
#import "ODWLogConfiguration_private.h"
#import "ODWLogManager.h"
#import "ODWLogger_private.h"
#include <stdexcept>
#include "LogManager.hpp"
#include "LogManagerProvider.hpp"
using namespace MAT;
using namespace Microsoft::Applications::Events;
LOGMANAGER_INSTANCE
@implementation ODWLogManager
static BOOL _initialized = false;
+(nullable ODWLogger *)loggerWithTenant:(nonnull NSString *)tenantToken
{
return [ODWLogManager loggerWithTenant:tenantToken source:@""];
}
+(nullable ODWLogger *)loggerWithTenant:(nonnull NSString *)tenantToken
source:(nonnull NSString *)source
{
// If log manager is not initialized, try initializing it. If that fails, return nil else return the logger.
if (!_initialized && ![ODWLogManager initializeLogManager:tenantToken withConfig:nil])
{
return nil;
}
std::string strToken = std::string([tenantToken UTF8String]);
std::string strSource = std::string([source UTF8String]);
ILogger* logger = nullptr;
try
{
logger = LogManager::GetLogger(strToken, strSource);
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
}
if(!logger) return nil;
return [[ODWLogger alloc] initWithILogger: logger];
}
+(nullable ODWLogger *)loggerWithTenant:(nonnull NSString *)tenantToken
source:(nonnull NSString *)source
withConfig:(nonnull ODWLogConfiguration *)config
{
// We expect that the static LogManager has already been initialized before this function is called
// If not, return nil
if (!_initialized)
{
return nil;
}
status_t status = status_t::STATUS_SUCCESS;
ILogConfiguration* wrappedConfig = [config getWrappedConfiguration];
if (wrappedConfig == nil)
{
return nil;
}
ILogManager* manager = LogManagerProvider::CreateLogManager(
*wrappedConfig,
status);
if (status == status_t::STATUS_SUCCESS && manager != nil)
{
std::string strToken = std::string([tenantToken UTF8String]);
std::string strSource = std::string([source UTF8String]);
ILogger* logger = nullptr;
try
{
logger = manager->GetLogger(strToken, strSource);
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
}
if(!logger) return nil;
return [[ODWLogger alloc] initWithILogger: logger];
}
return nil;
}
+(nullable ODWLogger *)initForTenant:(nonnull NSString *)tenantToken withConfig:(nullable NSDictionary *)config
{
ILogger *logger = [ODWLogManager initializeLogManager:tenantToken withConfig:config];
if (!logger) return nil;
return [[ODWLogger alloc] initWithILogger: logger];
}
+(nullable ODWLogger *)initForTenant:(nonnull NSString *)tenantToken
{
return [ODWLogManager initForTenant:tenantToken withConfig:nil];
}
+(nullable ILogger *)initializeLogManager:(nonnull NSString *)tenantToken withConfig:(nullable NSDictionary *)config
{
ILogger* logger = nullptr;
try
{
// Get reference to the default configuration
auto& logManagerConfig = LogManager::GetLogConfiguration();
// Update logManager config when custom configuration is provided.
if (config != nil && config.count > 0)
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:config
options:0
error:&error];
if (jsonData)
{
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
MAT::MergeFromJSON(logManagerConfig, [jsonString UTF8String]);
}
else
{
[ODWLogger raiseException: [error.localizedDescription UTF8String]];
}
}
// Turn off statistics
logManagerConfig[CFG_MAP_METASTATS_CONFIG][CFG_INT_METASTATS_INTERVAL] = 0;
// Initialize SDK Log Manager
std::string strToken = std::string([tenantToken UTF8String]);
logger = LogManager::Initialize(strToken, logManagerConfig);
// Obtain semantics values
NSBundle* bundle = [NSBundle mainBundle];
NSLocale* locale = [NSLocale currentLocale];
std::string strUserLocale;
NSString* countryCode = nil;
if (@available(iOS 10.0, macOS 10.12, *))
{
const char *localeCode = [[locale languageCode] UTF8String];
if (localeCode != nullptr)
{
strUserLocale = std::string(localeCode);
}
else
{
strUserLocale = "und";
}
countryCode = [locale countryCode];
}
if ([countryCode length] != 0)
{
strUserLocale += [[NSString stringWithFormat:@"-%@", countryCode] UTF8String];
}
NSString* bundleVersion = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
std::string strBundleVersion = bundleVersion == nil ? std::string {} : std::string([bundleVersion UTF8String]);
NSArray<NSString *> *localizations = [bundle preferredLocalizations];
NSString* appLanguage;
if ((localizations != nil) && ([localizations count] > 0))
{
appLanguage = [localizations firstObject];
}
else
{
appLanguage = [[NSLocale preferredLanguages] firstObject];
}
NSString* appCountry = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
std::string strBundleLocale = std::string([[NSString stringWithFormat:@"%@-%@", appLanguage, appCountry] UTF8String]);
// Set semantics
ISemanticContext* semanticContext = LogManager::GetSemanticContext();
semanticContext->SetAppVersion(strBundleVersion);
semanticContext->SetAppLanguage(strBundleLocale);
semanticContext->SetUserLanguage(strUserLocale);
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
}
_initialized = logger != NULL;
return logger;
}
+(nullable ODWLogger *)loggerForSource:(nonnull NSString *)source
{
std::string strSource = std::string([source UTF8String]);
ILogger* logger = nullptr;
try
{
logger = LogManager::GetLogger(strSource);
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
}
if(!logger) return nil;
return [[ODWLogger alloc] initWithILogger: logger];
}
+(void)uploadNow
{
PerformActionWithCppExceptionsCatch(^(void) {
LogManager::UploadNow();
});
}
+(ODWStatus)flush
{
try
{
return ((ODWStatus)LogManager::Flush());
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
return ODWEfail;
}
}
+(ODWStatus)flushAndTeardown
{
try
{
return ((ODWStatus)LogManager::FlushAndTeardown());
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
return ODWEfail;
}
}
+(void)setTransmissionProfile:(ODWTransmissionProfile)profile
{
PerformActionWithCppExceptionsCatch(^(void) {
LogManager::SetTransmitProfile((TransmitProfile)profile);
});
}
+(void)pauseTransmission
{
try
{
LogManager::PauseTransmission();
}
catch (const std::exception &e)
{
if ([ODWLogConfiguration surfaceCppExceptions])
{
[ODWLogger raiseException: e.what()];
}
[ODWLogger traceException: e.what()];
}
}
+(void)resumeTransmission
{
PerformActionWithCppExceptionsCatch(^(void) {
LogManager::ResumeTransmission();
});
}
+(void)resetTransmitProfiles
{
PerformActionWithCppExceptionsCatch(^(void) {
LogManager::ResetTransmitProfiles();
});
}
+(void)setContextWithName:(nonnull NSString*)name
stringValue:(nonnull NSString*)value
{
PerformActionWithCppExceptionsCatch(^(void) {
std::string strKey = std::string([name UTF8String]);
std::string strValue = std::string([value UTF8String]);
LogManager::SetContext(strKey, strValue);
});
}
+(void)setContextWithName:(nonnull NSString*)name
stringValue:(nonnull NSString*)value
piiKind:(enum ODWPiiKind)piiKind
{
PerformActionWithCppExceptionsCatch(^(void) {
std::string strKey = std::string([name UTF8String]);
std::string strValue = std::string([value UTF8String]);
PiiKind piiValue = PiiKind(piiKind);
LogManager::SetContext(strKey, strValue, piiValue);
});
}
+(void)applicationWillTerminate {
canUseSDK = false;
[ODWLogManager pauseTransmission];
[ODWLogManager flushAndTeardown];
}
@end