From fb6228ba763abee2e40a1b9e2339135e0db67ec3 Mon Sep 17 00:00:00 2001 From: michaellee123 Date: Wed, 16 Jun 2021 20:53:21 +0800 Subject: [PATCH 1/3] 1.fix over 4000 words break, should break in line, do not break before split lines. --- .../logger/PrettyFormatStrategy.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java b/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java index d6870d04..fc4150c2 100644 --- a/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java +++ b/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java @@ -89,25 +89,10 @@ private PrettyFormatStrategy(@NonNull Builder builder) { logTopBorder(priority, tag); logHeaderContent(priority, tag, methodCount); - //get bytes of message with system's default charset (which is UTF-8 for Android) - byte[] bytes = message.getBytes(); - int length = bytes.length; - if (length <= CHUNK_SIZE) { - if (methodCount > 0) { - logDivider(priority, tag); - } - logContent(priority, tag, message); - logBottomBorder(priority, tag); - return; - } if (methodCount > 0) { logDivider(priority, tag); } - for (int i = 0; i < length; i += CHUNK_SIZE) { - int count = Math.min(length - i, CHUNK_SIZE); - //create a new String with system's default charset (which is UTF-8 for Android) - logContent(priority, tag, new String(bytes, i, count)); - } + logContent(priority, tag, message); logBottomBorder(priority, tag); } @@ -167,7 +152,18 @@ private void logContent(int logType, @Nullable String tag, @NonNull String chunk String[] lines = chunk.split(System.getProperty("line.separator")); for (String line : lines) { - logChunk(logType, tag, HORIZONTAL_LINE + " " + line); + //get bytes of message with system's default charset (which is UTF-8 for Android) + byte[] bytes = line.getBytes(); + int length = bytes.length; + if (length > CHUNK_SIZE) { + for (int i = 0; i < length; i += CHUNK_SIZE) { + int count = Math.min(length - i, CHUNK_SIZE); + //create a new String with system's default charset (which is UTF-8 for Android) + logChunk(logType, tag, HORIZONTAL_LINE + " " + new String(bytes, i, count)); + } + } else { + logChunk(logType, tag, HORIZONTAL_LINE + " " + line); + } } } From 862013281d79fe96a500c0a52e9292d85c0a8799 Mon Sep 17 00:00:00 2001 From: michaellee123 Date: Thu, 17 Jun 2021 14:59:57 +0800 Subject: [PATCH 2/3] 1.Fix the bug that over 4000 words break. Check string completed and calc offset. Support Chinese words and Emoji or more UTF-8 charset. 2.Add unit test in sample, about Zhuge Kongming's and much emoji. --- .../orhanobut/logger/PrettyFormatStrategy.java | 15 ++++++++++++++- .../java/com/orhanobut/sample/MainActivity.java | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java b/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java index fc4150c2..896faaa3 100644 --- a/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java +++ b/logger/src/main/java/com/orhanobut/logger/PrettyFormatStrategy.java @@ -155,11 +155,24 @@ private void logContent(int logType, @Nullable String tag, @NonNull String chunk //get bytes of message with system's default charset (which is UTF-8 for Android) byte[] bytes = line.getBytes(); int length = bytes.length; + int offset; + String check; if (length > CHUNK_SIZE) { for (int i = 0; i < length; i += CHUNK_SIZE) { int count = Math.min(length - i, CHUNK_SIZE); //create a new String with system's default charset (which is UTF-8 for Android) - logChunk(logType, tag, HORIZONTAL_LINE + " " + new String(bytes, i, count)); + check = new String(bytes, i, count); + if (check.getBytes().length == count) { + logChunk(logType, tag, HORIZONTAL_LINE + " " + check); + } else { + /* + If length of new string not equals count, it's means the string not completed, so calc a offset for it. + Offset always between 0 and 3, CHUNK_SIZE is 4000 and Android's max limit is 4076, offset won't exceed the limit; + */ + offset = check.getBytes().length - count; + logChunk(logType, tag, HORIZONTAL_LINE + " " + new String(bytes, i, count + offset)); + i += offset; + } } } else { logChunk(logType, tag, HORIZONTAL_LINE + " " + line); diff --git a/sample/src/main/java/com/orhanobut/sample/MainActivity.java b/sample/src/main/java/com/orhanobut/sample/MainActivity.java index c2bf2f2d..4edd0bfa 100644 --- a/sample/src/main/java/com/orhanobut/sample/MainActivity.java +++ b/sample/src/main/java/com/orhanobut/sample/MainActivity.java @@ -13,9 +13,14 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; public class MainActivity extends Activity { + String csb = "先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。  宫中府中,俱为一体;陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。  侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。  将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。  亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。  臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。  先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明;故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。  愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏。臣不胜受恩感激。今当远离,临表涕零,不知所言。"; + + String emoji = "🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -65,6 +70,9 @@ protected void onCreate(Bundle savedInstanceState) { Logger.json("{ \"key\": 3, \"value\": something}"); + Logger.i(csb + csb); + Logger.v(emoji + emoji + emoji); + Logger.d(Arrays.asList("foo", "bar")); Map map = new HashMap<>(); From f48c0cecdb0ddd76f27a842039465e6f32184b40 Mon Sep 17 00:00:00 2001 From: michaellee123 Date: Tue, 17 May 2022 16:49:57 +0800 Subject: [PATCH 3/3] 1.fix over 4000 words break, should break in line, do not break before split lines. --- .idea/codeStyles/Project.xml | 29 ++++-------------------- .idea/compiler.xml | 6 +++++ README.md | 4 ++++ gradle/wrapper/gradle-wrapper.properties | 2 +- 4 files changed, 15 insertions(+), 26 deletions(-) create mode 100644 .idea/compiler.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 3550d645..33583988 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,30 +1,8 @@ - - - - - - - - - - + + +