Browse Source

修改log打印

yanyi 3 years ago
parent
commit
b9f61b9ddd

+ 1 - 1
.gitignore

@@ -102,4 +102,4 @@ lint/tmp/
 
 # Android Profiling
 *.hprof
-
+.idea

+ 0 - 3
.idea/.gitignore

@@ -1,3 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml

+ 0 - 6
.idea/compiler.xml

@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="CompilerConfiguration">
-    <bytecodeTargetLevel target="1.8" />
-  </component>
-</project>

+ 0 - 30
.idea/jarRepositories.xml

@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="RemoteRepositoriesConfiguration">
-    <remote-repository>
-      <option name="id" value="central" />
-      <option name="name" value="Maven Central repository" />
-      <option name="url" value="https://repo1.maven.org/maven2" />
-    </remote-repository>
-    <remote-repository>
-      <option name="id" value="jboss.community" />
-      <option name="name" value="JBoss Community repository" />
-      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
-    </remote-repository>
-    <remote-repository>
-      <option name="id" value="maven2" />
-      <option name="name" value="maven2" />
-      <option name="url" value="https://developer.huawei.com/repo/" />
-    </remote-repository>
-    <remote-repository>
-      <option name="id" value="BintrayJCenter" />
-      <option name="name" value="BintrayJCenter" />
-      <option name="url" value="https://jcenter.bintray.com/" />
-    </remote-repository>
-    <remote-repository>
-      <option name="id" value="maven" />
-      <option name="name" value="maven" />
-      <option name="url" value="https://repo.huaweicloud.com/repository/maven/" />
-    </remote-repository>
-  </component>
-</project>

+ 0 - 4
.idea/misc.xml

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
-</project>

+ 19 - 3
build.gradle

@@ -8,7 +8,7 @@ ohos {
         compatibleSdkVersion 4
     }
 }
-	
+
 buildscript {
     repositories {
         maven {
@@ -17,11 +17,13 @@ buildscript {
         maven {
             url 'https://developer.huawei.com/repo/'
         }
-		jcenter()
+        jcenter()
+        maven { url 'http://maven.benyanyi.com/nexus/content/repositories/mylove_hm/' }
     }
     dependencies {
         classpath 'com.huawei.ohos:hap:2.4.4.2'
         classpath 'com.huawei.ohos:decctest:1.2.4.0'
+        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
     }
 }
 
@@ -33,6 +35,20 @@ allprojects {
         maven {
             url 'https://developer.huawei.com/repo/'
         }
-		jcenter()
+        jcenter()
+        //解决中文乱码问题
+        tasks.withType(Javadoc) { //兼容中文
+            options.addStringOption('Xdoclint:none', '-quiet')
+            options.addStringOption('encoding', 'UTF-8')
+            options {
+                encoding "UTF-8"
+                charSet 'UTF-8'
+                links "http://docs.oracle.com/javase/7/docs/api"
+            }
+        }
     }
 }
+
+//task clean(type: Delete) {
+//    delete rootProject.buildDir
+//}

+ 28 - 0
loglib/bintrayUpload.gradle

@@ -0,0 +1,28 @@
+apply plugin: 'maven-publish'
+apply plugin: 'maven'
+
+Properties properties = new Properties()
+properties.load(project.rootProject.file('local.properties').newDataInputStream())
+uploadArchives {
+    repositories {
+        mavenDeployer {
+            repository(url: properties.getProperty("POM_URL")) {
+                authentication(userName: properties.getProperty("nexus.user"), password: properties.getProperty("nexus.password"))
+            }
+
+            pom.groupId = properties.getProperty("POM_GROUP_ID")
+            pom.artifactId = properties.getProperty("POM_ATRIFACT_ID")
+            pom.version = properties.getProperty("POM_VERSION")
+
+            pom.project {
+                licenses {
+                    license {
+                        name 'The Apache Software License, Version 2.0'
+                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
+                    }
+                }
+            }
+        }
+    }
+}
+//gradlew bintrayUpload    terminal中输入

+ 3 - 1
loglib/build.gradle

@@ -1,4 +1,5 @@
 apply plugin: 'com.huawei.ohos.library'
+apply from: 'bintrayUpload.gradle'
 //For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#ZH-CN_TOPIC_0000001154985555__section1112183053510
 ohos {
     compileSdkVersion 5
@@ -13,10 +14,11 @@ ohos {
             }
         }
     }
-    
+
 }
 
 dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     testImplementation 'junit:junit:4.13'
+    api files('lib/json.jar')
 }

BIN
loglib/libs/json.jar


+ 52 - 0
loglib/src/main/java/com/benyanyi/loglib/FileLog.java

@@ -0,0 +1,52 @@
+package com.benyanyi.loglib;
+
+import ohos.hiviewdfx.HiLog;
+import ohos.hiviewdfx.HiLogLabel;
+
+import java.io.*;
+import java.util.Random;
+
+/**
+ * @author myLove
+ * @time 2017/10/31 10:40
+ * @e-mail mylove.520.y@gmail.com
+ * @overview
+ */
+
+class FileLog {
+    private static final String FILE_PREFIX = "JLog_";
+    private static final String FILE_FORMAT = ".log";
+
+    static void printFile(String tag, File targetDirectory, String fileName, String headString, String msg) {
+
+        fileName = (fileName == null) ? getFileName() : fileName;
+        if (save(targetDirectory, fileName, msg)) {
+            HiLog.debug(new HiLogLabel(HiLog.LOG_APP, 0x100, tag), headString + " save log success ! location is >>>" + targetDirectory.getAbsolutePath() + "/" + fileName);
+        } else {
+            HiLog.debug(new HiLogLabel(HiLog.LOG_APP, 0x100, tag), headString + "save log fails !");
+        }
+    }
+
+    private static boolean save(File dic, String fileName, String msg) {
+
+        File file = new File(dic, fileName);
+
+        try {
+            OutputStream outputStream = new FileOutputStream(file);
+            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
+            outputStreamWriter.write(msg);
+            outputStreamWriter.flush();
+            outputStream.close();
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+
+    }
+
+    private static String getFileName() {
+        Random random = new Random();
+        return FILE_PREFIX + Long.toString(System.currentTimeMillis() + random.nextInt(10000)).substring(4) + FILE_FORMAT;
+    }
+}

+ 20 - 12
loglib/src/main/java/com/benyanyi/loglib/Jlog.java

@@ -154,9 +154,12 @@ public final class Jlog {
         if (!IS_SHOW_LOG) {
             return;
         }
-        if (!IS_INIT && isApkInDebug()) {
+        if (!IS_INIT) {
             return;
         }
+//        if (!IS_INIT && isApkInDebug()) {
+//            return;
+//        }
 
         Throwable tr = new Throwable();
         StringWriter sw = new StringWriter();
@@ -186,9 +189,12 @@ public final class Jlog {
         if (!IS_SHOW_LOG) {
             return;
         }
-        if (!IS_INIT && isApkInDebug()) {
+        if (!IS_INIT) {
             return;
         }
+//        if (!IS_INIT && isApkInDebug()) {
+//            return;
+//        }
         String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
         String tag = contents[0];
         String msg = contents[1];
@@ -207,7 +213,7 @@ public final class Jlog {
                 JsonLog.printJson(tag, msg, headString);
                 break;
             case XML:
-//                XmlLog.printXml(tag, msg, headString);
+                XmlLog.printXml(tag, msg, headString);
                 break;
         }
 
@@ -227,15 +233,18 @@ public final class Jlog {
         if (!IS_SHOW_LOG) {
             return;
         }
-        if (!IS_INIT && isApkInDebug()) {
+        if (!IS_INIT) {
             return;
         }
+//        if (!IS_INIT && isApkInDebug()) {
+//            return;
+//        }
         String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objectMsg);
         String tag = contents[0];
         String msg = contents[1];
         String headString = contents[2];
 
-//        FileLog.printFile(tag, targetDirectory, fileName, headString, msg);
+        FileLog.printFile(tag, targetDirectory, fileName, headString, msg);
     }
 
     private static String[] wrapperContent(int stackTraceIndex, Object tagStr, Object... objects) {
@@ -296,14 +305,13 @@ public final class Jlog {
         }
     }
 
-    private static boolean isApkInDebug() {
+//    private static boolean isApkInDebug() {
 //        try {
-//            @SuppressLint("PrivateApi")
-//            Application application = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
-//            ApplicationInfo info = application.getApplicationInfo();
-//            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0;
+////            Application application = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
+////            ApplicationInfo info = application.getApplicationInfo();
+////            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0;
 //        } catch (Exception e) {
-        return false;
+//            return false;
 //        }
-    }
+//    }
 }

+ 16 - 13
loglib/src/main/java/com/benyanyi/loglib/JsonLog.java

@@ -2,6 +2,9 @@ package com.benyanyi.loglib;
 
 import ohos.hiviewdfx.HiLog;
 import ohos.hiviewdfx.HiLogLabel;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 /**
  * @author myLove
@@ -16,19 +19,19 @@ class JsonLog {
         String message = "";
         String str1 = "{";
         String str2 = "[";
-//        try {
-//            if (msg.startsWith(str1)) {
-//                JSONObject jsonObject = new JSONObject(msg);
-//                message = jsonObject.toString(Jlog.JSON_INDENT);
-//            } else if (msg.startsWith(str2)) {
-//                JSONArray jsonArray = new JSONArray(msg);
-//                message = jsonArray.toString(Jlog.JSON_INDENT);
-//            } else {
-//                message = msg;
-//            }
-//        } catch (JSONException e) {
-//            message = msg;
-//        }
+        try {
+            if (msg.startsWith(str1)) {
+                JSONObject jsonObject = new JSONObject(msg);
+                message = jsonObject.toString(Jlog.JSON_INDENT);
+            } else if (msg.startsWith(str2)) {
+                JSONArray jsonArray = new JSONArray(msg);
+                message = jsonArray.toString(Jlog.JSON_INDENT);
+            } else {
+                message = msg;
+            }
+        } catch (JSONException e) {
+            message = msg;
+        }
 
         LogUtil.printLine(tag, true);
         message = headString + Jlog.LINE_SEPARATOR + message;

+ 48 - 0
loglib/src/main/java/com/benyanyi/loglib/XmlLog.java

@@ -0,0 +1,48 @@
+package com.benyanyi.loglib;
+
+import ohos.hiviewdfx.HiLog;
+import ohos.hiviewdfx.HiLogLabel;
+
+/**
+ * @author myLove
+ * @time 2017/10/31 10:44
+ * @e-mail mylove.520.y@gmail.com
+ * @overview
+ */
+
+class XmlLog {
+    static void printXml(String tag, String xml, String headString) {
+
+        if (xml != null) {
+            xml = XmlLog.formatXML(xml);
+            xml = headString + "\n" + xml;
+        } else {
+            xml = headString + Jlog.NULL_TIPS;
+        }
+
+        LogUtil.printLine(tag, true);
+        String[] lines = xml.split(Jlog.LINE_SEPARATOR);
+        for (String line : lines) {
+            if (!LogUtil.isEmpty(line)) {
+                HiLog.debug(new HiLogLabel(HiLog.LOG_APP, 0x100, tag), "║ " + line);
+            }
+        }
+        LogUtil.printLine(tag, false);
+    }
+
+    private static String formatXML(String inputXML) {
+//        try {
+//            Source xmlInput = new StreamSource(new StringReader(inputXML));
+//            StreamResult xmlOutput = new StreamResult(new StringWriter());
+//            Transformer transformer = TransformerFactory.newInstance().newTransformer();
+//            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+//            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
+//            transformer.transform(xmlInput, xmlOutput);
+//            return xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//            return inputXML;
+//        }
+        return inputXML;
+    }
+}

+ 1 - 0
settings.gradle

@@ -1 +1,2 @@
+rootProject.name = 'logger'
 include ':entry', ':loglib'