Skip to content

Commit 29bff6f

Browse files
committed
see 06/06 log
1 parent c020b5a commit 29bff6f

File tree

4 files changed

+53
-50
lines changed

4 files changed

+53
-50
lines changed

lib/base/src/main/java/com/blankj/lib/base/rv/BaseAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
6363
public int getItemCount() {
6464
return mData.size();
6565
}
66+
67+
@Override
68+
public void onViewRecycled(@NonNull BaseViewHolder holder) {
69+
super.onViewRecycled(holder);
70+
int position = holder.getAdapterPosition();
71+
mData.get(position).onViewRecycled(holder, position);
72+
}
6673
}

lib/base/src/main/java/com/blankj/lib/base/rv/BaseCell.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ static View getViewByType(int type) {
2828

2929
public abstract void bind(@NonNull final BaseViewHolder holder, final int position);
3030

31+
public void onViewRecycled(@NonNull final BaseViewHolder holder, final int position) {
32+
}
33+
3134
protected int viewType;
3235

3336
public BaseCell(int layoutId) {

utilcode/lib/src/main/java/com/blankj/utilcode/util/LogUtils.java

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public final class LogUtils {
102102
private static final String PLACEHOLDER = " ";
103103
private static final Config CONFIG = new Config();
104104

105-
private static final ThreadLocal<SimpleDateFormat> SDF_THREAD_LOCAL = new ThreadLocal<>();
105+
private static SimpleDateFormat simpleDateFormat;
106106

107107
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
108108

@@ -214,7 +214,7 @@ public static void xml(@TYPE final int type, final String tag, final String cont
214214

215215
public static void log(final int type, final String tag, final Object... contents) {
216216
if (!CONFIG.isLogSwitch()) return;
217-
int type_low = type & 0x0f, type_high = type & 0xf0;
217+
final int type_low = type & 0x0f, type_high = type & 0xf0;
218218
if (CONFIG.isLog2ConsoleSwitch() || CONFIG.isLog2FileSwitch() || type_high == FILE) {
219219
if (type_low < CONFIG.mConsoleFilter && type_low < CONFIG.mFileFilter) return;
220220
final TagHead tagHead = processTagAndHead(tag);
@@ -223,7 +223,12 @@ public static void log(final int type, final String tag, final Object... content
223223
print2Console(type_low, tagHead.tag, tagHead.consoleHead, body);
224224
}
225225
if ((CONFIG.isLog2FileSwitch() || type_high == FILE) && type_low >= CONFIG.mFileFilter) {
226-
print2File(type_low, tagHead.tag, tagHead.fileHead + body);
226+
EXECUTOR.execute(new Runnable() {
227+
@Override
228+
public void run() {
229+
print2File(type_low, tagHead.tag, tagHead.fileHead + body);
230+
}
231+
});
227232
}
228233
}
229234
}
@@ -471,45 +476,40 @@ private static void printSingleTagMsg(final int type, final String tag, final St
471476
}
472477

473478
private static void print2File(final int type, final String tag, final String msg) {
474-
Date now = new Date(System.currentTimeMillis());
475-
String format = getSdf().format(now);
479+
String format = getSdf().format(new Date());
476480
String date = format.substring(0, 10);
477481
String time = format.substring(11);
478482
final String fullPath =
479-
CONFIG.getDir() + CONFIG.getFilePrefix() + "-" + date + "-" + CONFIG.getProcessName() + ".txt";
480-
if (!createOrExistsFile(fullPath)) {
483+
CONFIG.getDir() + CONFIG.getFilePrefix() + "_" + date + "_" + CONFIG.getProcessName() + ".txt";
484+
if (!createOrExistsFile(fullPath, date)) {
481485
Log.e("LogUtils", "create " + fullPath + " failed!");
482486
return;
483487
}
484-
StringBuilder sb = new StringBuilder();
485-
sb.append(time)
486-
.append(T[type - V])
487-
.append("/")
488-
.append(tag)
489-
.append(msg)
490-
.append(LINE_SEP);
491-
final String content = sb.toString();
488+
final String content = time +
489+
T[type - V] +
490+
"/" +
491+
tag +
492+
msg +
493+
LINE_SEP;
492494
input2File(content, fullPath);
493495
}
494496

495497
private static SimpleDateFormat getSdf() {
496-
SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL.get();
497498
if (simpleDateFormat == null) {
498-
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
499-
SDF_THREAD_LOCAL.set(simpleDateFormat);
499+
simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd HH:mm:ss", Locale.getDefault());
500500
}
501501
return simpleDateFormat;
502502
}
503503

504-
private static boolean createOrExistsFile(final String filePath) {
504+
private static boolean createOrExistsFile(final String filePath, final String date) {
505505
File file = new File(filePath);
506506
if (file.exists()) return file.isFile();
507507
if (!createOrExistsDir(file.getParentFile())) return false;
508508
try {
509-
deleteDueLogs(filePath);
509+
deleteDueLogs(filePath, date);
510510
boolean isCreate = file.createNewFile();
511511
if (isCreate) {
512-
printDeviceInfo(filePath);
512+
printDeviceInfo(filePath, date);
513513
}
514514
return isCreate;
515515
} catch (IOException e) {
@@ -518,22 +518,20 @@ private static boolean createOrExistsFile(final String filePath) {
518518
}
519519
}
520520

521-
private static void deleteDueLogs(String filePath) {
521+
private static void deleteDueLogs(final String filePath, final String date) {
522522
if (CONFIG.getSaveDays() <= 0) return;
523523
File file = new File(filePath);
524524
File parentFile = file.getParentFile();
525525
File[] files = parentFile.listFiles(new FilenameFilter() {
526526
@Override
527527
public boolean accept(File dir, String name) {
528-
return name.matches("^" + CONFIG.getFilePrefix() + "-[0-9]{4}-[0-9]{2}-[0-9]{2}-.*\\.txt$");
528+
return name.matches("^" + CONFIG.getFilePrefix() + "_[0-9]{4}_[0-9]{2}_[0-9]{2}_.*$");
529529
}
530530
});
531531
if (files == null || files.length <= 0) return;
532-
final int length = filePath.length();
533-
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
532+
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault());
534533
try {
535-
String curDay = findDate(filePath);
536-
long dueMillis = sdf.parse(curDay).getTime() - CONFIG.getSaveDays() * 86400000L;
534+
long dueMillis = sdf.parse(date).getTime() - CONFIG.getSaveDays() * 86400000L;
537535
for (final File aFile : files) {
538536
String name = aFile.getName();
539537
int l = name.length();
@@ -556,15 +554,15 @@ public void run() {
556554
}
557555

558556
private static String findDate(String str) {
559-
Pattern pattern = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
557+
Pattern pattern = Pattern.compile("[0-9]{4}_[0-9]{2}_[0-9]{2}");
560558
Matcher matcher = pattern.matcher(str);
561559
if (matcher.find()) {
562560
return matcher.group();
563561
}
564562
return "";
565563
}
566564

567-
private static void printDeviceInfo(final String filePath) {
565+
private static void printDeviceInfo(final String filePath, final String date) {
568566
String versionName = "";
569567
int versionCode = 0;
570568
try {
@@ -578,9 +576,8 @@ private static void printDeviceInfo(final String filePath) {
578576
} catch (PackageManager.NameNotFoundException e) {
579577
e.printStackTrace();
580578
}
581-
String time = filePath.substring(filePath.length() - 14, filePath.length() - 4);
582579
final String head = "************* Log Head ****************" +
583-
"\nDate of Log : " + time +
580+
"\nDate of Log : " + date +
584581
"\nDevice Manufacturer: " + Build.MANUFACTURER +
585582
"\nDevice Model : " + Build.MODEL +
586583
"\nAndroid Version : " + Build.VERSION.RELEASE +
@@ -607,27 +604,22 @@ private static boolean isSpace(final String s) {
607604

608605
private static void input2File(final String input, final String filePath) {
609606
if (CONFIG.mFileWriter == null) {
610-
EXECUTOR.execute(new Runnable() {
611-
@Override
612-
public void run() {
613-
BufferedWriter bw = null;
614-
try {
615-
bw = new BufferedWriter(new FileWriter(filePath, true));
616-
bw.write(input);
617-
} catch (IOException e) {
618-
e.printStackTrace();
619-
Log.e("LogUtils", "log to " + filePath + " failed!");
620-
} finally {
621-
try {
622-
if (bw != null) {
623-
bw.close();
624-
}
625-
} catch (IOException e) {
626-
e.printStackTrace();
627-
}
607+
BufferedWriter bw = null;
608+
try {
609+
bw = new BufferedWriter(new FileWriter(filePath, true));
610+
bw.write(input);
611+
} catch (IOException e) {
612+
e.printStackTrace();
613+
Log.e("LogUtils", "log to " + filePath + " failed!");
614+
} finally {
615+
try {
616+
if (bw != null) {
617+
bw.close();
628618
}
619+
} catch (IOException e) {
620+
e.printStackTrace();
629621
}
630-
});
622+
}
631623
} else {
632624
CONFIG.mFileWriter.write(filePath, input);
633625
}

utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/image/ImageActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.blankj.utilcode.pkg.R
2121
import com.blankj.utilcode.util.ImageUtils
2222
import com.blankj.utilcode.util.StringUtils
2323
import com.blankj.utilcode.util.ToastUtils
24+
import kotlinx.android.synthetic.main.activity_image.*
2425

2526
/**
2627
* ```

0 commit comments

Comments
 (0)