|
8 | 8 | import android.net.NetworkCapabilities; |
9 | 9 | import android.net.NetworkRequest; |
10 | 10 | import android.os.Build; |
| 11 | +import android.text.TextUtils; |
11 | 12 | import android.util.ArrayMap; |
12 | 13 |
|
13 | 14 |
|
14 | 15 | import androidx.annotation.NonNull; |
| 16 | +import androidx.annotation.Nullable; |
15 | 17 | import androidx.lifecycle.DefaultLifecycleObserver; |
16 | 18 | import androidx.lifecycle.LifecycleOwner; |
17 | 19 | import androidx.lifecycle.ProcessLifecycleOwner; |
18 | 20 |
|
19 | 21 | import org.jetbrains.annotations.NotNull; |
20 | 22 |
|
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
21 | 25 | import java.util.HashMap; |
22 | 26 | import java.util.List; |
23 | 27 | import java.util.Map; |
24 | 28 |
|
| 29 | +import io.openim.android.sdk.enums.LogLevel; |
25 | 30 | import io.openim.android.sdk.internal.log.LogcatHelper; |
26 | 31 | import io.openim.android.sdk.listener.BaseImpl; |
27 | 32 | import io.openim.android.sdk.listener.OnBase; |
@@ -228,17 +233,57 @@ public void uploadLogs(OnBase<String> base, List<String> params, int line, Stri |
228 | 233 | } |
229 | 234 |
|
230 | 235 | /** |
231 | | - * 日志 |
232 | | - * @param base |
233 | | - * @param logLevel |
234 | | - * @param file |
235 | | - * @param line |
236 | | - * @param msg |
237 | | - * @param err |
238 | | - * @param keyAndValues |
| 236 | + * Sends a log entry to the SDK with the specified level, caller information, and optional key-value data. |
| 237 | + * |
| 238 | + * <p>This method mirrors the behavior of the Go SDKLog function by: |
| 239 | + * <ul> |
| 240 | + * <li>Embedding caller information ({@code clazz} and {@code currentLineNum}) similar to Go's {@code [file:line]}.</li> |
| 241 | + * <li>Routing logs according to the specified {@code logLevel}.</li> |
| 242 | + * <li>Appending extra key-value pairs, requiring an even number of elements.</li> |
| 243 | + * </ul> |
| 244 | + * </p> |
| 245 | + * |
| 246 | + * @param logLevel Log level defined in {@link LogLevel}. Levels Warn~Fatal require a non-empty {@code errStr}. |
| 247 | + * @param currentClassName The class' name where the log is generated, used to recreate Go's native caller info. |
| 248 | + * @param currentLineNum The line number associated with the log. |
| 249 | + * @param msgStr The main log message. |
| 250 | + * @param errStr Optional error detail; required for Warn, Error, and Fatal levels. |
| 251 | + * @param extra Optional key-value pairs. Must be an even number of elements; otherwise a warning entry is appended. |
239 | 252 | */ |
240 | | - public void logs(OnBase<String> base, long logLevel, String file, long line, String msg, String err, String[] keyAndValues) { |
| 253 | + public void logs(@LogLevel int logLevel, String currentClassName , int currentLineNum, @NonNull String msgStr, @Nullable String errStr, @Nullable String... extra) { |
| 254 | + if (logLevel <= LogLevel.Warn && logLevel >= LogLevel.Fatal && TextUtils.isEmpty(errStr)) { |
| 255 | + LogcatHelper.logEInError("Must have an error message."); |
| 256 | + return; |
| 257 | + } |
| 258 | + if (logLevel > LogLevel.DebugWithSQL || logLevel < LogLevel.Fatal) { |
| 259 | + LogcatHelper.logEInError("Unknown log level."); |
| 260 | + return; |
| 261 | + } |
| 262 | + // The Core SDK requires a value of array type, so the default string here is "[]". |
| 263 | + String kvJson = "[]"; |
| 264 | + if (extra != null) { |
| 265 | + if (extra.length % 2 == 0) { |
| 266 | + kvJson = JsonUtil.toString(extra); |
| 267 | + } else { |
| 268 | + List<String> stringList = new ArrayList<>(Arrays.asList(extra)); |
| 269 | + stringList.add("The extra argument must contain an even number of elements."); |
| 270 | + kvJson = JsonUtil.toString(stringList); |
| 271 | + } |
| 272 | + if (TextUtils.isEmpty(kvJson)) |
| 273 | + kvJson = "[]"; |
| 274 | + } |
| 275 | + |
| 276 | + Open_im_sdk.logs(BaseImpl.stringBase(new OnBase<String>() { |
| 277 | + @Override |
| 278 | + public void onSuccess(String data) { |
| 279 | + LogcatHelper.logDInDebug("Insert log success"); |
| 280 | + } |
241 | 281 |
|
| 282 | + @Override |
| 283 | + public void onError(int code, String error) { |
| 284 | + LogcatHelper.logEInError("Insert log failed, code is " + code + " " + error); |
| 285 | + } |
| 286 | + }), ParamsUtil.buildOperationID(), logLevel, currentClassName, currentLineNum, msgStr, errStr, kvJson); |
242 | 287 | } |
243 | 288 |
|
244 | 289 |
|
|
0 commit comments