|
| 1 | +package com.deploygate.sdk; |
| 2 | + |
| 3 | +import com.deploygate.sdk.internal.Logger; |
| 4 | + |
| 5 | +public class CustomLogConfiguration { |
| 6 | + public enum Backpressure { |
| 7 | + /** |
| 8 | + * SDK rejects new logs if buffer size is exceeded |
| 9 | + */ |
| 10 | + PRESERVE_BUFFER, |
| 11 | + |
| 12 | + /** |
| 13 | + * SDK drops logs from the oldest if buffer size is exceeded |
| 14 | + */ |
| 15 | + DROP_BUFFER_BY_OLDEST |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * the log buffer is required until DeployGate client app receives BOOT_COMPLETED broadcast. |
| 20 | + * <p> |
| 21 | + * This is an experimental value. |
| 22 | + * <p> |
| 23 | + * - 10 seconds until boot-completed |
| 24 | + * - 10 logs per 1 seconds |
| 25 | + * - plus some buffer |
| 26 | + */ |
| 27 | + private static final int DEFAULT_BUFFER_SIZE = 150; |
| 28 | + private static final int MAX_BUFFER_SIZE = DEFAULT_BUFFER_SIZE; |
| 29 | + |
| 30 | + public final Backpressure backpressure; |
| 31 | + public final int bufferSize; |
| 32 | + |
| 33 | + /** |
| 34 | + * Do not bypass {@link Builder} to instantiate this class. |
| 35 | + * |
| 36 | + * @see Builder |
| 37 | + */ |
| 38 | + private CustomLogConfiguration( |
| 39 | + Backpressure backpressure, |
| 40 | + int bufferSize |
| 41 | + ) { |
| 42 | + this.backpressure = backpressure; |
| 43 | + this.bufferSize = bufferSize; |
| 44 | + } |
| 45 | + |
| 46 | + public static class Builder { |
| 47 | + private Backpressure backpressure = Backpressure.DROP_BUFFER_BY_OLDEST; |
| 48 | + private int bufferSize = DEFAULT_BUFFER_SIZE; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param backpressure |
| 52 | + * the strategy of the backpressure in the log buffer |
| 53 | + * |
| 54 | + * @return self |
| 55 | + * |
| 56 | + * @see Backpressure |
| 57 | + */ |
| 58 | + public Builder setBackpressure(Backpressure backpressure) { |
| 59 | + if (backpressure == null) { |
| 60 | + throw new IllegalArgumentException("backpressure must be non-null"); |
| 61 | + } |
| 62 | + |
| 63 | + this.backpressure = backpressure; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param bufferSize |
| 69 | + * the max size of the log buffer |
| 70 | + * |
| 71 | + * @return self |
| 72 | + */ |
| 73 | + public Builder setBufferSize(int bufferSize) { |
| 74 | + if (bufferSize <= 0) { |
| 75 | + throw new IllegalArgumentException("buffer size must be greater than 0"); |
| 76 | + } |
| 77 | + |
| 78 | + if (bufferSize > MAX_BUFFER_SIZE) { |
| 79 | + Logger.w("buffer size is exceeded %d so it's rounded to %d", bufferSize, MAX_BUFFER_SIZE); |
| 80 | + bufferSize = MAX_BUFFER_SIZE; |
| 81 | + } |
| 82 | + |
| 83 | + this.bufferSize = bufferSize; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public CustomLogConfiguration build() { |
| 88 | + return new CustomLogConfiguration(backpressure, bufferSize); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments