From 1da22077e101cfcc539bd9c6ea44f896e6a28e26 Mon Sep 17 00:00:00 2001 From: hyj Date: Fri, 16 Sep 2022 23:29:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=97=A5=E5=BF=97=E6=8D=A2?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xlogsample/XLogSampleApplication.java | 128 +++++++++--------- .../border/DefaultBorderFormatter.java | 120 ++++++++-------- 2 files changed, 127 insertions(+), 121 deletions(-) diff --git a/xlog-sample/src/main/java/com/elvishew/xlogsample/XLogSampleApplication.java b/xlog-sample/src/main/java/com/elvishew/xlogsample/XLogSampleApplication.java index 6d26988..993c7d3 100644 --- a/xlog-sample/src/main/java/com/elvishew/xlogsample/XLogSampleApplication.java +++ b/xlog-sample/src/main/java/com/elvishew/xlogsample/XLogSampleApplication.java @@ -35,76 +35,76 @@ public class XLogSampleApplication extends Application { - public static Printer globalFilePrinter; + public static Printer globalFilePrinter; - private static final long MAX_TIME = 1000 * 60 * 60 * 24 * 2; // two days + private static final long MAX_TIME = 1000 * 60 * 60 * 24 * 2; // two days - @Override - public void onCreate() { - super.onCreate(); + @Override + public void onCreate() { + super.onCreate(); - initXlog(); - } + initXlog(); + } - /** - * Initialize XLog. - */ - private void initXlog() { - LogConfiguration config = new LogConfiguration.Builder() - .logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL - : LogLevel.NONE) - .tag(getString(R.string.global_tag)) // Specify TAG, default: "X-LOG" - // .enableThreadInfo() // Enable thread info, disabled by default - // .enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default - // .enableBorder() // Enable border, disabled by default - // .jsonFormatter(new MyJsonFormatter()) // Default: DefaultJsonFormatter - // .xmlFormatter(new MyXmlFormatter()) // Default: DefaultXmlFormatter - // .throwableFormatter(new MyThrowableFormatter()) // Default: DefaultThrowableFormatter - // .threadFormatter(new MyThreadFormatter()) // Default: DefaultThreadFormatter - // .stackTraceFormatter(new MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter - // .borderFormatter(new MyBoardFormatter()) // Default: DefaultBorderFormatter - // .addObjectFormatter(AnyClass.class, // Add formatter for specific class of object - // new AnyClassObjectFormatter()) // Use Object.toString() by default - .addInterceptor(new BlacklistTagsFilterInterceptor( // Add blacklist tags filter - "blacklist1", "blacklist2", "blacklist3")) - // .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter - // "whitelist1", "whitelist2", "whitelist3")) - // .addInterceptor(new MyInterceptor()) // Add a log interceptor - .build(); + /** + * Initialize XLog. + */ + private void initXlog() { + LogConfiguration config = new LogConfiguration.Builder() + .logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL + : LogLevel.NONE) + .tag(getString(R.string.global_tag)) // Specify TAG, default: "X-LOG" + // .enableThreadInfo() // Enable thread info, disabled by default + // .enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default + // .enableBorder() // Enable border, disabled by default + // .jsonFormatter(new MyJsonFormatter()) // Default: DefaultJsonFormatter + // .xmlFormatter(new MyXmlFormatter()) // Default: DefaultXmlFormatter + // .throwableFormatter(new MyThrowableFormatter()) // Default: DefaultThrowableFormatter + // .threadFormatter(new MyThreadFormatter()) // Default: DefaultThreadFormatter + // .stackTraceFormatter(new MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter + // .borderFormatter(new MyBoardFormatter()) // Default: DefaultBorderFormatter + // .addObjectFormatter(AnyClass.class, // Add formatter for specific class of object + // new AnyClassObjectFormatter()) // Use Object.toString() by default + .addInterceptor(new BlacklistTagsFilterInterceptor( // Add blacklist tags filter + "blacklist1", "blacklist2", "blacklist3")) + // .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter + // "whitelist1", "whitelist2", "whitelist3")) + // .addInterceptor(new MyInterceptor()) // Add a log interceptor + .build(); - Printer androidPrinter = new AndroidPrinter(); // Printer that print the log using android.util.Log - Printer filePrinter = new FilePrinter // Printer that print the log to the file system - .Builder(new File(getExternalCacheDir().getAbsolutePath(), "log").getPath()) // Specify the path to save log file - .fileNameGenerator(new DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log") - // .backupStrategy(new MyBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024) - // .cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Default: NeverCleanStrategy() - .flattener(new ClassicFlattener()) // Default: DefaultFlattener - .writer(new SimpleWriter() { // Default: SimpleWriter - @Override - public void onNewFileCreated(File file) { - super.onNewFileCreated(file); - final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" + - "\nDevice Manufacturer: " + Build.MANUFACTURER + - "\nDevice Model : " + Build.MODEL + - "\nAndroid Version : " + Build.VERSION.RELEASE + - "\nAndroid SDK : " + Build.VERSION.SDK_INT + - "\nApp VersionName : " + BuildConfig.VERSION_NAME + - "\nApp VersionCode : " + BuildConfig.VERSION_CODE + - "\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n"; - appendLog(header); - } - }) - .build(); + Printer androidPrinter = new AndroidPrinter(); // Printer that print the log using android.util.Log + Printer filePrinter = new FilePrinter // Printer that print the log to the file system + .Builder(new File(getExternalCacheDir().getAbsolutePath(), "log").getPath()) // Specify the path to save log file + .fileNameGenerator(new DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log") + // .backupStrategy(new MyBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024) + // .cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Default: NeverCleanStrategy() + .flattener(new ClassicFlattener()) // Default: DefaultFlattener + .writer(new SimpleWriter() { // Default: SimpleWriter + @Override + public void onNewFileCreated(File file) { + super.onNewFileCreated(file); + final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" + + "\nDevice Manufacturer: " + Build.MANUFACTURER + + "\nDevice Model : " + Build.MODEL + + "\nAndroid Version : " + Build.VERSION.RELEASE + + "\nAndroid SDK : " + Build.VERSION.SDK_INT + + "\nApp VersionName : " + BuildConfig.VERSION_NAME + + "\nApp VersionCode : " + BuildConfig.VERSION_CODE + + "\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n"; + appendLog(header); + } + }) + .build(); - XLog.init( // Initialize XLog - config, // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build() - androidPrinter, // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used. - filePrinter); + XLog.init( // Initialize XLog + config, // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build() + androidPrinter, // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used. + filePrinter); - // For future usage: partial usage in MainActivity. - globalFilePrinter = filePrinter; + // For future usage: partial usage in MainActivity. + globalFilePrinter = filePrinter; - // Intercept all logs(including logs logged by third party modules/libraries) and print them to file. - LibCat.config(true, filePrinter); - } + // Intercept all logs(including logs logged by third party modules/libraries) and print them to file. + LibCat.config(true, filePrinter); + } } diff --git a/xlog/src/main/java/com/elvishew/xlog/formatter/border/DefaultBorderFormatter.java b/xlog/src/main/java/com/elvishew/xlog/formatter/border/DefaultBorderFormatter.java index 0b06cdc..6f5b8c3 100644 --- a/xlog/src/main/java/com/elvishew/xlog/formatter/border/DefaultBorderFormatter.java +++ b/xlog/src/main/java/com/elvishew/xlog/formatter/border/DefaultBorderFormatter.java @@ -30,70 +30,76 @@ */ public class DefaultBorderFormatter implements BorderFormatter { - private static final char VERTICAL_BORDER_CHAR = '║'; + private static final char VERTICAL_BORDER_CHAR = '║'; - // Length: 100. - private static final String TOP_HORIZONTAL_BORDER = - "╔═════════════════════════════════════════════════" + - "══════════════════════════════════════════════════"; + // Length: 100. + private static final String TOP_HORIZONTAL_BORDER = + "╔═════════════════════════════════════════════════" + + "══════════════════════════════════════════════════"; - // Length: 99. - private static final String DIVIDER_HORIZONTAL_BORDER = - "╟─────────────────────────────────────────────────" + - "──────────────────────────────────────────────────"; + // Length: 99. + private static final String DIVIDER_HORIZONTAL_BORDER = + "╟─────────────────────────────────────────────────" + + "──────────────────────────────────────────────────"; - // Length: 100. - private static final String BOTTOM_HORIZONTAL_BORDER = - "╚═════════════════════════════════════════════════" + - "══════════════════════════════════════════════════"; + // Length: 100. + private static final String BOTTOM_HORIZONTAL_BORDER = + "╚═════════════════════════════════════════════════" + + "══════════════════════════════════════════════════"; - @Override - public String format(String[] segments) { - if (segments == null || segments.length == 0) { - return ""; - } + @Override + public String format(String[] segments) { + if (segments == null || segments.length == 0) { + return ""; + } - String[] nonNullSegments = new String[segments.length]; - int nonNullCount = 0; - for (String segment : segments) { - if (segment != null) { - nonNullSegments[nonNullCount++] = segment; - } - } - if (nonNullCount == 0) { - return ""; - } + String[] nonNullSegments = new String[segments.length]; + int nonNullCount = 0; + for (String segment : segments) { + if (segment != null) { + nonNullSegments[nonNullCount++] = segment; + } + } + if (nonNullCount == 0) { + return ""; + } + + StringBuilder msgBuilder = new StringBuilder(" "); + msgBuilder + .append(SystemCompat.lineSeparator) + .append(TOP_HORIZONTAL_BORDER) + .append(SystemCompat.lineSeparator); + for (int i = 0; i < nonNullCount; i++) { + msgBuilder.append(appendVerticalBorder(nonNullSegments[i])) + .append(SystemCompat.lineSeparator); + + if (i != nonNullCount - 1) { + msgBuilder.append(DIVIDER_HORIZONTAL_BORDER); + } else { + msgBuilder.append(BOTTOM_HORIZONTAL_BORDER); + } - StringBuilder msgBuilder = new StringBuilder(); - msgBuilder.append(TOP_HORIZONTAL_BORDER).append(SystemCompat.lineSeparator); - for (int i = 0; i < nonNullCount; i++) { - msgBuilder.append(appendVerticalBorder(nonNullSegments[i])); - if (i != nonNullCount - 1) { - msgBuilder.append(SystemCompat.lineSeparator).append(DIVIDER_HORIZONTAL_BORDER) - .append(SystemCompat.lineSeparator); - } else { - msgBuilder.append(SystemCompat.lineSeparator).append(BOTTOM_HORIZONTAL_BORDER); - } + msgBuilder.append(SystemCompat.lineSeparator); + } + return msgBuilder.toString(); } - return msgBuilder.toString(); - } - /** - * Add {@value #VERTICAL_BORDER_CHAR} to each line of msg. - * - * @param msg the message to add border - * @return the message with {@value #VERTICAL_BORDER_CHAR} in the start of each line - */ - private static String appendVerticalBorder(String msg) { - StringBuilder borderedMsgBuilder = new StringBuilder(msg.length() + 10); - String[] lines = msg.split(SystemCompat.lineSeparator); - for (int i = 0, N = lines.length; i < N; i++) { - if (i != 0) { - borderedMsgBuilder.append(SystemCompat.lineSeparator); - } - String line = lines[i]; - borderedMsgBuilder.append(VERTICAL_BORDER_CHAR).append(line); + /** + * Add {@value #VERTICAL_BORDER_CHAR} to each line of msg. + * + * @param msg the message to add border + * @return the message with {@value #VERTICAL_BORDER_CHAR} in the start of each line + */ + private static String appendVerticalBorder(String msg) { + StringBuilder borderedMsgBuilder = new StringBuilder(msg.length() + 10); + String[] lines = msg.split(SystemCompat.lineSeparator); + for (int i = 0, N = lines.length; i < N; i++) { + if (i != 0) { + borderedMsgBuilder.append(SystemCompat.lineSeparator); + } + String line = lines[i]; + borderedMsgBuilder.append(VERTICAL_BORDER_CHAR).append(line); + } + return borderedMsgBuilder.toString(); } - return borderedMsgBuilder.toString(); - } }