Skip to content

Commit ddff4c5

Browse files
authored
Merge pull request #94 from JauneQ/main
Restores the unusable logs method and adds a new data type to the onConversationUserInputStatusChanged
2 parents 41129d2 + 1d3f323 commit ddff4c5

File tree

5 files changed

+115
-11
lines changed

5 files changed

+115
-11
lines changed

OpenIM-SDK/src/main/java/io/openim/android/sdk/OpenIMClient.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@
88
import android.net.NetworkCapabilities;
99
import android.net.NetworkRequest;
1010
import android.os.Build;
11+
import android.text.TextUtils;
1112
import android.util.ArrayMap;
1213

1314

1415
import androidx.annotation.NonNull;
16+
import androidx.annotation.Nullable;
1517
import androidx.lifecycle.DefaultLifecycleObserver;
1618
import androidx.lifecycle.LifecycleOwner;
1719
import androidx.lifecycle.ProcessLifecycleOwner;
1820

1921
import org.jetbrains.annotations.NotNull;
2022

23+
import java.util.ArrayList;
24+
import java.util.Arrays;
2125
import java.util.HashMap;
2226
import java.util.List;
2327
import java.util.Map;
2428

29+
import io.openim.android.sdk.enums.LogLevel;
2530
import io.openim.android.sdk.internal.log.LogcatHelper;
2631
import io.openim.android.sdk.listener.BaseImpl;
2732
import io.openim.android.sdk.listener.OnBase;
@@ -228,17 +233,57 @@ public void uploadLogs(OnBase<String> base, List<String> params, int line, Stri
228233
}
229234

230235
/**
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.
239252
*/
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+
}
241281

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);
242287
}
243288

244289

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.openim.android.sdk.enums;
2+
3+
import androidx.annotation.IntDef;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@IntDef(value = {
11+
LogLevel.Fatal,
12+
LogLevel.Panic,
13+
LogLevel.Error,
14+
LogLevel.Warn,
15+
LogLevel.Info,
16+
LogLevel.Debug,
17+
LogLevel.DebugWithSQL
18+
})
19+
@Retention(RetentionPolicy.SOURCE)
20+
@Target({ElementType.PARAMETER, ElementType.FIELD})
21+
public @interface LogLevel {
22+
int Fatal = 0;
23+
int Panic = 1;
24+
int Error = 2;
25+
int Warn = 3;
26+
int Info = 4;
27+
int Debug = 5;
28+
int DebugWithSQL = 6;
29+
}

OpenIM-SDK/src/main/java/io/openim/android/sdk/internal/log/LogcatHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ public static void logDInDebug(String message) {
2222
Log.d(TAG, message);
2323
}
2424
}
25+
26+
public static void logEInError(String message) {
27+
if (TextUtils.isEmpty(message)) {
28+
return;
29+
}
30+
if (BuildConfig.DEBUG) {
31+
Log.e(TAG, message);
32+
}
33+
}
2534
}

OpenIM-SDK/src/main/java/io/openim/android/sdk/models/InitConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.openim.android.sdk.models;
22

3+
import io.openim.android.sdk.enums.LogLevel;
4+
import io.openim.android.sdk.enums.Platform;
5+
36
/**
47
* apiUrl SDK的API接口地址。如:http:xxx:10000
58
* wsUrl SDK的web socket地址。如: ws:xxx:17778
@@ -15,11 +18,12 @@ public InitConfig(String apiUrl, String wsUrl, String storageDir) {
1518
this.dataDir = storageDir;
1619
}
1720

18-
public final int platformID = 2;
21+
public final int platformID = Platform.ANDROID;
1922
public String apiAddr;
2023
public String wsAddr;
2124
public String dataDir;
22-
public int logLevel = 6;
25+
@LogLevel
26+
public int logLevel = LogLevel.Debug;
2327
public boolean isLogStandardOutput;
2428
public String logFilePath;
2529
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.openim.android.sdk.models;
2+
import java.util.List;
3+
4+
public class InputStatesChangedData {
5+
private String conversationID;
6+
private List<Long> platformIDs;
7+
private String userID;
8+
9+
public String getConversationID() { return conversationID; }
10+
public void setConversationID(String value) { this.conversationID = value; }
11+
12+
public List<Long> getPlatformIDs() { return platformIDs; }
13+
public void setPlatformIDs(List<Long> value) { this.platformIDs = value; }
14+
15+
public String getUserID() { return userID; }
16+
public void setUserID(String value) { this.userID = value; }
17+
}

0 commit comments

Comments
 (0)