Skip to content

Commit 3858e47

Browse files
author
fengjian
committed
support log string format, it can avoid release version log string concat for better performance
1 parent 6f33a8a commit 3858e47

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

app/src/main/java/com/jayfeng/lesscode/app/activity/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
6262
}
6363
});
6464

65-
LogLess.$d("network:" + NetworkLess.$online() + ", type:" + NetworkLess.$type());
65+
LogLess.$d("network: %s, type: %s", NetworkLess.$online(), NetworkLess.$type());
6666

6767
Person person = new Person("fengj");
6868
SerializeLess.$se(new File(getCacheDir(), "person").getAbsolutePath(), person);

lesscode-core/src/main/java/com/jayfeng/lesscode/core/LogLess.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public final class LogLess {
2626
*
2727
* @param str
2828
*/
29-
public static void v(String str) {
29+
public static void v(String str, Object... args) {
3030
if ($.sDebug || DEBUG_TAG_LOG) {
31-
Log.v(getTag(), buildLogString(str));
31+
Log.v(getTag(), buildLogString(str, args));
3232
}
3333
}
3434

@@ -37,9 +37,9 @@ public static void v(String str) {
3737
*
3838
* @param str
3939
*/
40-
public static void $d(String str) {
40+
public static void $d(String str, Object... args) {
4141
if ($.sDebug || DEBUG_TAG_LOG) {
42-
Log.d(getTag(), buildLogString(str));
42+
Log.d(getTag(), buildLogString(str, args));
4343
}
4444
}
4545

@@ -48,9 +48,9 @@ public static void v(String str) {
4848
*
4949
* @param str
5050
*/
51-
public static void $i(String str) {
51+
public static void $i(String str, Object... args) {
5252
if ($.sDebug || DEBUG_TAG_LOG) {
53-
Log.i(getTag(), buildLogString(str));
53+
Log.i(getTag(), buildLogString(str, args));
5454
}
5555
}
5656

@@ -59,9 +59,9 @@ public static void v(String str) {
5959
*
6060
* @param str
6161
*/
62-
public static void $w(String str) {
62+
public static void $w(String str, Object... args) {
6363
if ($.sDebug || DEBUG_TAG_LOG) {
64-
Log.w(getTag(), buildLogString(str));
64+
Log.w(getTag(), buildLogString(str, args));
6565
}
6666
}
6767

@@ -70,9 +70,9 @@ public static void v(String str) {
7070
*
7171
* @param str
7272
*/
73-
public static void $e(String str) {
73+
public static void $e(String str, Object... args) {
7474
if ($.sDebug || DEBUG_TAG_LOG) {
75-
Log.e(getTag(), buildLogString(str));
75+
Log.e(getTag(), buildLogString(str, args));
7676
}
7777
}
7878

@@ -116,6 +116,7 @@ public static void v(String str) {
116116

117117
/**
118118
* json
119+
*
119120
* @param str
120121
*/
121122
public static void $json(String str) {
@@ -142,7 +143,13 @@ private static String getTag() {
142143
* @param str
143144
* @return
144145
*/
145-
private static String buildLogString(String str) {
146+
private static String buildLogString(String str, Object... args) {
147+
148+
// format string with args
149+
if (args.length > 0) {
150+
str = String.format(str, args);
151+
}
152+
146153
StackTraceElement caller = new Throwable().fillInStackTrace().getStackTrace()[2];
147154
StringBuilder stringBuilder = new StringBuilder();
148155
if (TextUtils.isEmpty($.sTAG)) {

0 commit comments

Comments
 (0)