Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer custom logs until a service connection is established #49

Merged
merged 18 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ build/
#idea
*.iml
.idea/

.DS_Store
5 changes: 5 additions & 0 deletions sample/proguard-project.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
# public *;
#}
-keep class * extends android.os.IInterface

# to check the R8 results except obfuscation
-keepnames class com.deploygate.sdk.** {
*;
}
10 changes: 9 additions & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
apply from: rootProject.file('sdk.build.gradle')

android.defaultConfig.consumerProguardFiles 'deploygate-sdk-proguard-rules.txt'
android {
defaultConfig {
consumerProguardFiles 'deploygate-sdk-proguard-rules.txt'
}
}

dependencies {
testImplementation 'org.mockito:mockito-core:4.4.0'
}

ext {
displayName = "DeployGate SDK"
Expand Down
9 changes: 9 additions & 0 deletions sdk/deploygate-sdk-proguard-rules.txt
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
-keep class * extends android.os.IInterface

# To allow removing our logger
-assumenosideeffects class com.deploygate.sdk.internal.Logger {
public static void v(...);
public static void i(...);
public static void w(...);
public static void d(...);
public static void e(...);
}
5 changes: 5 additions & 0 deletions sdk/src/debug/java/com/deploygate/sdk/internal/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.deploygate.sdk.internal;

public class Config {
public static final boolean DEBUG = true;
}
37 changes: 37 additions & 0 deletions sdk/src/main/java/com/deploygate/sdk/CustomLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.deploygate.sdk;

import android.os.Bundle;

import com.deploygate.service.DeployGateEvent;

class CustomLog {
public final String type;
public final String body;
private int retryCount;

CustomLog(
String type,
String body
) {
this.type = type;
this.body = body;
this.retryCount = 0;
}

/**
* @return the number of current attempts
*/
int getAndIncrementRetryCount() {
return retryCount++;
}

/**
* @return a bundle to send to the client service
*/
Bundle toExtras() {
Bundle extras = new Bundle();
extras.putSerializable(DeployGateEvent.EXTRA_LOG, body);
extras.putSerializable(DeployGateEvent.EXTRA_LOG_TYPE, type);
return extras;
}
}
91 changes: 91 additions & 0 deletions sdk/src/main/java/com/deploygate/sdk/CustomLogConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.deploygate.sdk;

import com.deploygate.sdk.internal.Logger;

public class CustomLogConfiguration {
public enum Backpressure {
/**
* SDK rejects new logs if buffer size is exceeded
*/
PRESERVE_BUFFER,

/**
* SDK drops logs from the oldest if buffer size is exceeded
*/
DROP_BUFFER_BY_OLDEST
}

/**
* the log buffer is required until DeployGate client app receives BOOT_COMPLETED broadcast.
* <p>
* This is an experimental value.
* <p>
* - 10 seconds until boot-completed
* - 10 logs per 1 seconds
* - plus some buffer
*/
private static final int DEFAULT_BUFFER_SIZE = 150;
private static final int MAX_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;

public final Backpressure backpressure;
public final int bufferSize;

/**
* Do not bypass {@link Builder} to instantiate this class.
*
* @see Builder
*/
private CustomLogConfiguration(
Backpressure backpressure,
int bufferSize
) {
this.backpressure = backpressure;
this.bufferSize = bufferSize;
}

public static class Builder {
private Backpressure backpressure = Backpressure.DROP_BUFFER_BY_OLDEST;
private int bufferSize = DEFAULT_BUFFER_SIZE;

/**
* @param backpressure
* the strategy of the backpressure in the log buffer
*
* @return self
*
* @see Backpressure
*/
public Builder setBackpressure(Backpressure backpressure) {
if (backpressure == null) {
throw new IllegalArgumentException("backpressure must be non-null");
}

this.backpressure = backpressure;
return this;
}

/**
* @param bufferSize
* the max size of the log buffer
*
* @return self
*/
public Builder setBufferSize(int bufferSize) {
if (bufferSize <= 0) {
throw new IllegalArgumentException("buffer size must be greater than 0");
}

if (bufferSize > MAX_BUFFER_SIZE) {
Logger.w("buffer size is exceeded %d so it's rounded to %d", bufferSize, MAX_BUFFER_SIZE);
bufferSize = MAX_BUFFER_SIZE;
}

this.bufferSize = bufferSize;
return this;
}

public CustomLogConfiguration build() {
return new CustomLogConfiguration(backpressure, bufferSize);
}
}
}
Loading