Skip to content

Commit b3a1e07

Browse files
Pezzahkattrali
authored andcommitted
[ENG-857] Support programmatically setting API key (#41)
* updated bugsnag.cs to have a static way of setting the API key * Changed to log an error message if the API key could not be found * Add factory method to create Bugsnag instance with API key already set
1 parent 84e1170 commit b3a1e07

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

example/Assets/Standard Assets/Bugsnag/Bugsnag.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ public enum Severity {
278278
public static string[] SeverityValues = new string[]{"info", "error", "warning"};
279279

280280
public string BugsnagApiKey = "";
281+
public static string BugsnagApiKeyStatic = "";
281282
public bool AutoNotify = true;
282283

283284
// Rate limiting section
@@ -368,6 +369,11 @@ public static void SetMaximumCount(LogType unityLogType, int maxCount) {
368369
maxCounts[unityLogType] = maxCount;
369370
}
370371

372+
// Used to set the API key when Bugsnag is being initialized
373+
public static void SetApiKey(String apiKey) {
374+
BugsnagApiKeyStatic = apiKey;
375+
}
376+
371377
string GetLevelName() {
372378
#if UNITY_5_OR_NEWER
373379
return SceneManager.GetActiveScene().name;
@@ -378,11 +384,34 @@ string GetLevelName() {
378384

379385
void Awake() {
380386
DontDestroyOnLoad(this);
381-
Init(BugsnagApiKey);
387+
Init(null);
382388
}
383389

384-
public void Init(string apiKey)
385-
{
390+
public static Bugsnag createBugsnagInstance(GameObject gameObject, String ApiKey) {
391+
Bugsnag.SetApiKey(ApiKey);
392+
Bugsnag bugsnagInstance = gameObject.AddComponent<Bugsnag>();
393+
bugsnagInstance.Init();
394+
return bugsnagInstance;
395+
}
396+
397+
public void Init() {
398+
Init(null);
399+
}
400+
401+
public void Init(string apiKey) {
402+
// Try to work out which API key to use
403+
if (!String.IsNullOrEmpty (apiKey)) {
404+
InitInternal (apiKey);
405+
} else if (!String.IsNullOrEmpty(BugsnagApiKey)) {
406+
InitInternal(BugsnagApiKey);
407+
} else if (!String.IsNullOrEmpty(BugsnagApiKeyStatic)) {
408+
InitInternal(BugsnagApiKeyStatic);
409+
} else {
410+
Debug.LogError("BUGSNAG: ERROR: unable to initialize Bugsnag, API key must be specified");
411+
}
412+
}
413+
414+
private void InitInternal(string apiKey) {
386415
BugsnagApiKey = apiKey;
387416
NativeBugsnag.Register(BugsnagApiKey);
388417

src/Assets/Standard Assets/Bugsnag/Bugsnag.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ public enum Severity {
278278
public static string[] SeverityValues = new string[]{"info", "error", "warning"};
279279

280280
public string BugsnagApiKey = "";
281+
public static string BugsnagApiKeyStatic = "";
281282
public bool AutoNotify = true;
282283

283284
// Rate limiting section
@@ -368,6 +369,11 @@ public static void SetMaximumCount(LogType unityLogType, int maxCount) {
368369
maxCounts[unityLogType] = maxCount;
369370
}
370371

372+
// Used to set the API key when Bugsnag is being initialized
373+
public static void SetApiKey(String apiKey) {
374+
BugsnagApiKeyStatic = apiKey;
375+
}
376+
371377
string GetLevelName() {
372378
#if UNITY_5_OR_NEWER
373379
return SceneManager.GetActiveScene().name;
@@ -378,11 +384,34 @@ string GetLevelName() {
378384

379385
void Awake() {
380386
DontDestroyOnLoad(this);
381-
Init(BugsnagApiKey);
387+
Init(null);
382388
}
383389

384-
public void Init(string apiKey)
385-
{
390+
public static Bugsnag createBugsnagInstance(GameObject gameObject, String ApiKey) {
391+
Bugsnag.SetApiKey(ApiKey);
392+
Bugsnag bugsnagInstance = gameObject.AddComponent<Bugsnag>();
393+
bugsnagInstance.Init();
394+
return bugsnagInstance;
395+
}
396+
397+
public void Init() {
398+
Init(null);
399+
}
400+
401+
public void Init(string apiKey) {
402+
// Try to work out which API key to use
403+
if (!String.IsNullOrEmpty (apiKey)) {
404+
InitInternal (apiKey);
405+
} else if (!String.IsNullOrEmpty(BugsnagApiKey)) {
406+
InitInternal(BugsnagApiKey);
407+
} else if (!String.IsNullOrEmpty(BugsnagApiKeyStatic)) {
408+
InitInternal(BugsnagApiKeyStatic);
409+
} else {
410+
Debug.LogError("BUGSNAG: ERROR: unable to initialize Bugsnag, API key must be specified");
411+
}
412+
}
413+
414+
private void InitInternal(string apiKey) {
386415
BugsnagApiKey = apiKey;
387416
NativeBugsnag.Register(BugsnagApiKey);
388417

0 commit comments

Comments
 (0)