├── .gitignore ├── LICENSE ├── Log ├── AndroidManifest.xml ├── ic_launcher-web.png ├── libs │ └── android-support-v4.jar ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── main.xml │ ├── values-sw600dp │ │ └── dimens.xml │ ├── values-sw720dp-land │ │ └── dimens.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── forlong401 │ └── log │ ├── LogApp.java │ ├── transaction │ ├── log │ │ ├── manager │ │ │ ├── ILogManager.java │ │ │ ├── LogManager.java │ │ │ └── LogManagerImpl.java │ │ └── task │ │ │ └── LogTask.java │ └── utils │ │ ├── FileUtils.java │ │ ├── LogUtils.java │ │ └── Utils.java │ └── ui │ └── MainActivity.java ├── README.md └── output └── log.jar /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Intellij project files 26 | *.iml 27 | *.ipr 28 | *.iws 29 | .idea/ 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /Log/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Log/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/ic_launcher-web.png -------------------------------------------------------------------------------- /Log/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/libs/android-support-v4.jar -------------------------------------------------------------------------------- /Log/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /Log/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /Log/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Log/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Log/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Log/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/Log/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Log/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Log/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Log/res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Log/res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /Log/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Log/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Log/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /Log/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Log 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /Log/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/LogApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log; 17 | 18 | import android.app.Application; 19 | 20 | import com.forlong401.log.transaction.log.manager.LogManager; 21 | 22 | /** 23 | * Log application. 24 | * 25 | * @author Li Cong 26 | * @date 2014-3-23 27 | */ 28 | public class LogApp extends Application { 29 | 30 | @Override 31 | public void onCreate() { 32 | super.onCreate(); 33 | LogManager.getManager(getApplicationContext()).registerCrashHandler(); 34 | } 35 | 36 | @Override 37 | public void onTerminate() { 38 | super.onTerminate(); 39 | LogManager.getManager(getApplicationContext()).unregisterCrashHandler(); 40 | } 41 | } -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/log/manager/ILogManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.log.manager; 17 | 18 | import android.app.Activity; 19 | 20 | /** 21 | * Log manager interface. 22 | * 23 | * @author Li Cong 24 | * @date 2014-3-23 25 | */ 26 | public interface ILogManager { 27 | /** 28 | * Log 29 | * 30 | * @param tag 31 | * @param msg 32 | * @param logType 33 | * @return 34 | */ 35 | public boolean log(String tag, String msg, int logType); 36 | 37 | /** 38 | * Register crash handler to handle exception. 39 | * 40 | * @return 41 | */ 42 | public boolean registerCrashHandler(); 43 | 44 | /** 45 | * Unregister crash handler to handle exception. 46 | * 47 | * @return 48 | */ 49 | public boolean unregisterCrashHandler(); 50 | 51 | /** 52 | * Register activity into the stack. 53 | * 54 | * @param activity 55 | * @return 56 | */ 57 | public boolean registerActivity(final Activity activity); 58 | 59 | /** 60 | * Unregister activity into the stack. 61 | * 62 | * @param activity 63 | * @return 64 | */ 65 | public boolean unregisterActivity(final Activity activity); 66 | } 67 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/log/manager/LogManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.log.manager; 17 | 18 | import android.content.Context; 19 | 20 | /** 21 | * Log manager. 22 | * 23 | * @author Li Cong 24 | * @date 2014-3-23 25 | */ 26 | public class LogManager { 27 | private static ILogManager sManager; 28 | 29 | /** 30 | * Get singleton manager. DO NOT GET MANAGER BY THE CONTEXT OF ACTIVITY. 31 | * 32 | * @param context 33 | * The context of application. 34 | * @return 35 | */ 36 | public synchronized static ILogManager getManager(Context context) { 37 | if (context == null) { 38 | return null; 39 | } 40 | 41 | if (sManager == null) { 42 | sManager = new LogManagerImpl(context); 43 | } 44 | 45 | return sManager; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/log/manager/LogManagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.log.manager; 17 | 18 | import java.io.File; 19 | import java.io.FileOutputStream; 20 | import java.io.OutputStreamWriter; 21 | import java.io.PrintWriter; 22 | import java.io.StringWriter; 23 | import java.io.Writer; 24 | import java.lang.Thread.UncaughtExceptionHandler; 25 | import java.lang.ref.WeakReference; 26 | import java.util.ArrayList; 27 | import java.util.Date; 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | import java.util.concurrent.ThreadFactory; 31 | 32 | import android.app.Activity; 33 | import android.content.Context; 34 | import android.util.Log; 35 | 36 | import com.forlong401.log.transaction.log.task.LogTask; 37 | import com.forlong401.log.transaction.utils.LogUtils; 38 | import com.forlong401.log.transaction.utils.Utils; 39 | 40 | /** 41 | * Log manager implement. 42 | * 43 | * @author Li Cong 44 | * @date 2014-3-23 45 | */ 46 | public class LogManagerImpl implements ILogManager, UncaughtExceptionHandler { 47 | private static final String TAG = "LogManagerImpl"; 48 | private final static int EXECUTOR_HANDLE_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1; 49 | 50 | private Context mContext; 51 | private ExecutorService mExecutorService = null; 52 | // The available numbers of threads in executor service. 53 | private int mAvailableProcessors = 1; 54 | private static Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler; 55 | private ArrayList> mActivityList = new ArrayList>(); 56 | 57 | static { 58 | mDefaultUncaughtExceptionHandler = Thread 59 | .getDefaultUncaughtExceptionHandler(); 60 | } 61 | 62 | LogManagerImpl(Context context) { 63 | mContext = context; 64 | mAvailableProcessors = Runtime.getRuntime().availableProcessors(); 65 | } 66 | 67 | private void checkExecutor() { 68 | if (mExecutorService == null || mExecutorService.isShutdown()) { 69 | if (mAvailableProcessors < 0) { 70 | mAvailableProcessors = 1; 71 | } 72 | mExecutorService = Executors.newFixedThreadPool( 73 | mAvailableProcessors, new ThreadFactory() { 74 | @Override 75 | public Thread newThread(Runnable r) { 76 | Thread t = new Thread(r); 77 | t.setPriority(EXECUTOR_HANDLE_THREAD_PRIORITY); 78 | t.setName(TAG); 79 | return t; 80 | } 81 | }); 82 | } 83 | } 84 | 85 | private void submitTask(final Runnable runnable) { 86 | checkExecutor(); 87 | mExecutorService.submit(runnable); 88 | } 89 | 90 | @Override 91 | public boolean registerCrashHandler() { 92 | Thread.setDefaultUncaughtExceptionHandler(this); 93 | return true; 94 | } 95 | 96 | @Override 97 | public boolean unregisterCrashHandler() { 98 | Thread.setDefaultUncaughtExceptionHandler(mDefaultUncaughtExceptionHandler); 99 | return true; 100 | } 101 | 102 | @Override 103 | public boolean registerActivity(Activity activity) { 104 | if (activity == null) { 105 | return false; 106 | } 107 | 108 | WeakReference ref = new WeakReference(activity); 109 | if (!mActivityList.contains(ref)) { 110 | mActivityList.add(ref); 111 | } 112 | return true; 113 | } 114 | 115 | @Override 116 | public boolean unregisterActivity(Activity activity) { 117 | if (activity == null) { 118 | return false; 119 | } 120 | 121 | WeakReference ref = new WeakReference(activity); 122 | return mActivityList.remove(ref); 123 | } 124 | 125 | @Override 126 | public void uncaughtException(Thread thread, Throwable ex) { 127 | try { 128 | final Writer result = new StringWriter(); 129 | final PrintWriter printWriter = new PrintWriter(result); 130 | printWriter.append(ex.getMessage()); 131 | ex.printStackTrace(printWriter); 132 | Log.getStackTraceString(ex); 133 | // If the exception was thrown in a background thread inside 134 | // AsyncTask, then the actual exception can be found with getCause. 135 | Throwable cause = ex.getCause(); 136 | while (cause != null) { 137 | cause.printStackTrace(printWriter); 138 | cause = cause.getCause(); 139 | } 140 | String msg = buildCrashLog(result.toString()); 141 | printWriter.close(); 142 | 143 | if (LogUtils.CRASH_SAVE_2_FILE) { 144 | // Save to file. 145 | saveCrashLog2File(msg); 146 | } 147 | 148 | if (LogUtils.CRASH_UPLOAD_2_NETWORK) { 149 | // Upload crash log to server. 150 | submitTask(new LogTask(mContext, TAG, msg, 151 | LogUtils.LOG_TYPE_2_NETWORK)); 152 | } 153 | } catch (Exception e) { 154 | } 155 | 156 | mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex); 157 | } 158 | 159 | private String saveCrashLog2File(String result) { 160 | if (Utils.sdAvailible()) { 161 | try { 162 | String fileName = LogUtils.getCrashFileName(mContext); 163 | File file = new File(fileName); 164 | FileOutputStream trace = new FileOutputStream(file, true); 165 | 166 | String lineSeparator = System.getProperty("line.separator"); 167 | if (lineSeparator == null) { 168 | lineSeparator = "\n"; 169 | } 170 | 171 | // Encode and encrypt the message. 172 | OutputStreamWriter writer = new OutputStreamWriter(trace, 173 | "utf-8"); 174 | writer.write(Utils.encrypt(result)); 175 | writer.flush(); 176 | 177 | trace.flush(); 178 | trace.close(); 179 | return fileName; 180 | } catch (Exception e) { 181 | } 182 | } 183 | return null; 184 | } 185 | 186 | private String buildCrashLog(String msg) { 187 | StringBuilder sb = new StringBuilder(); 188 | sb.append("#"); 189 | sb.append(new Date().toString()); 190 | sb.append("\n"); 191 | // Add system and device info. 192 | sb.append(Utils.buildSystemInfo(mContext)); 193 | sb.append("\n"); 194 | sb.append("#-------AndroidRuntime-------"); 195 | sb.append(msg); 196 | sb.append("\n"); 197 | sb.append("#-------activity_stack-------"); 198 | sb.append("\n"); 199 | sb.append(buildActivityStack()); 200 | sb.append("#end"); 201 | 202 | return sb.toString(); 203 | } 204 | 205 | private String buildActivityStack() { 206 | StringBuilder buffer = new StringBuilder(); 207 | int size = mActivityList.size(); 208 | for (int i = 0; i < size; i++) { 209 | buffer.append(i + ":"); 210 | WeakReference ref = mActivityList.get(i); 211 | if (ref != null && ref.get() != null) { 212 | buffer.append(ref.get().toString()); 213 | } 214 | buffer.append("\n"); 215 | } 216 | return buffer.toString(); 217 | } 218 | 219 | @Override 220 | public boolean log(String tag, String msg, int logType) { 221 | if (LogUtils.DEBUG) { 222 | submitTask(new LogTask(mContext, tag, msg, logType)); 223 | return true; 224 | } else { 225 | return false; 226 | } 227 | } 228 | 229 | } 230 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/log/task/LogTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.log.task; 17 | 18 | import java.io.File; 19 | import java.io.FileOutputStream; 20 | import java.io.OutputStreamWriter; 21 | import java.util.Date; 22 | 23 | import android.content.Context; 24 | import android.text.TextUtils; 25 | import android.util.Log; 26 | 27 | import com.forlong401.log.transaction.utils.FileUtils; 28 | import com.forlong401.log.transaction.utils.LogUtils; 29 | import com.forlong401.log.transaction.utils.Utils; 30 | 31 | /** 32 | * Handle log task. 33 | * 34 | * @author Li Cong 35 | * @date 2014-3-23 36 | */ 37 | public class LogTask implements Runnable { 38 | private Context mContext; 39 | private String mTag; 40 | private String mMsg; 41 | private int mLogType; 42 | 43 | public LogTask(Context context, String tag, String msg, int logType) { 44 | mContext = context; 45 | mMsg = msg; 46 | mTag = tag; 47 | mLogType = logType; 48 | } 49 | 50 | @Override 51 | public void run() { 52 | if (mContext == null || TextUtils.isEmpty(mMsg) 53 | || TextUtils.isEmpty(mTag)) { 54 | return; 55 | } 56 | 57 | if ((mLogType & LogUtils.LOG_TYPE_2_LOGCAT) > 0) { 58 | Log.d(mTag, mMsg); 59 | } 60 | 61 | if ((mLogType & LogUtils.LOG_TYPE_2_FILE) > 0) { 62 | log2File("ms:" + System.currentTimeMillis() + " [" + mTag + "]" 63 | + mMsg); 64 | } 65 | 66 | if ((mLogType & LogUtils.LOG_TYPE_2_NETWORK) > 0) { 67 | log2Network(mTag, mMsg); 68 | } 69 | } 70 | 71 | private void log2File(String msg) { 72 | if (Utils.sdAvailible()) { 73 | try { 74 | String systemInfo = "\n"; 75 | File file = new File(LogUtils.getLogFileName(mContext)); 76 | if (!file.exists() || file.isDirectory()) { 77 | FileUtils.delete(file); 78 | file.createNewFile(); 79 | systemInfo = Utils.buildSystemInfo(mContext); 80 | } 81 | 82 | String lineSeparator = System.getProperty("line.separator"); 83 | if (lineSeparator == null) { 84 | lineSeparator = "\n"; 85 | } 86 | 87 | // Encode and encrypt the message. 88 | FileOutputStream trace = new FileOutputStream(file, true); 89 | OutputStreamWriter writer = new OutputStreamWriter(trace, 90 | "utf-8"); 91 | writer.write(Utils.encrypt(buildLog(msg, systemInfo))); 92 | writer.flush(); 93 | 94 | trace.flush(); 95 | trace.close(); 96 | } catch (Exception e) { 97 | } 98 | } 99 | } 100 | 101 | private String buildLog(String msg, String systemInfo) { 102 | StringBuilder sb = new StringBuilder(); 103 | sb.append(systemInfo); 104 | sb.append("\n"); 105 | sb.append(new Date().toString()); 106 | sb.append(msg); 107 | sb.append("\n"); 108 | 109 | return sb.toString(); 110 | } 111 | 112 | private void log2Network(String tag, String msg) { 113 | // TODO: Server API for upload message. 114 | // TODO: Encode and encrypt the message. 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/utils/FileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.utils; 17 | 18 | import java.io.File; 19 | 20 | /** 21 | * File utility. 22 | * 23 | * @author Li Cong 24 | * @date 2014-3-23 25 | */ 26 | public final class FileUtils { 27 | 28 | /** 29 | * Delete file(file or folder). 30 | * 31 | * @param file 32 | */ 33 | public static void delete(File file) { 34 | if (file == null) { 35 | return; 36 | } 37 | if (file.isFile()) { 38 | file.delete(); 39 | return; 40 | } 41 | 42 | File[] files = file.listFiles(); 43 | if (files == null) { 44 | return; 45 | } 46 | for (File f : files) { 47 | if (f.isDirectory()) { 48 | delete(f); 49 | } else { 50 | f.delete(); 51 | } 52 | } 53 | file.delete(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/utils/LogUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.utils; 17 | 18 | import java.io.File; 19 | 20 | import android.content.Context; 21 | import android.text.TextUtils; 22 | 23 | /** 24 | * Log utility. 25 | * 26 | * @author Li Cong 27 | * @date 2014-3-23 28 | */ 29 | public class LogUtils { 30 | public static boolean DEBUG = true; 31 | public static boolean CRASH_SAVE_2_FILE = true; 32 | public static boolean CRASH_UPLOAD_2_NETWORK = false; 33 | 34 | public static final int LOG_TYPE_2_LOGCAT = 0x01; 35 | public static final int LOG_TYPE_2_FILE = 0x02; 36 | public static final int LOG_TYPE_2_FILE_AND_LOGCAT = 0x03; 37 | public static final int LOG_TYPE_2_NETWORK = 0x04; 38 | 39 | public final static String LOG_CACHE_DIRECTORY_NAME = "log"; 40 | public final static String CRASH_CACHE_DIRECTORY_NAME = "crash"; 41 | private static String sLogFileName = null; 42 | private static String sCrashFileName = null; 43 | 44 | public static File getLogDir(Context context) { 45 | return Utils.getAppCacheDir(context, LOG_CACHE_DIRECTORY_NAME); 46 | } 47 | 48 | public static File getCrashDir(Context context) { 49 | return Utils.getAppCacheDir(context, CRASH_CACHE_DIRECTORY_NAME); 50 | } 51 | 52 | public static String getLogFileName(Context context) { 53 | if (TextUtils.isEmpty(sLogFileName)) { 54 | File sub = new File(getLogDir(context), "log_" 55 | + System.currentTimeMillis() + ".txt"); 56 | sLogFileName = sub.getAbsolutePath(); 57 | } 58 | 59 | return sLogFileName; 60 | } 61 | 62 | public static String getCrashFileName(Context context) { 63 | if (TextUtils.isEmpty(sCrashFileName)) { 64 | File sub = new File(getCrashDir(context), "crash_" 65 | + System.currentTimeMillis() + ".txt"); 66 | sCrashFileName = sub.getAbsolutePath(); 67 | } 68 | 69 | return sCrashFileName; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/transaction/utils/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.transaction.utils; 17 | 18 | import java.io.File; 19 | 20 | import android.content.Context; 21 | import android.content.pm.PackageInfo; 22 | import android.content.pm.PackageManager; 23 | import android.content.pm.PackageManager.NameNotFoundException; 24 | import android.net.ConnectivityManager; 25 | import android.net.NetworkInfo; 26 | import android.net.wifi.WifiInfo; 27 | import android.net.wifi.WifiManager; 28 | import android.os.Environment; 29 | import android.telephony.TelephonyManager; 30 | import android.text.TextUtils; 31 | import android.util.DisplayMetrics; 32 | 33 | /** 34 | * Utility. 35 | * 36 | * @author Li Cong 37 | * @date 2014-3-23 38 | */ 39 | public final class Utils { 40 | private static String sAppName = ""; 41 | 42 | public static File getAppCacheDir(Context context, String subName) { 43 | if (!sdAvailible()) { 44 | return null; 45 | } 46 | File sd = Environment.getExternalStorageDirectory(); 47 | File dir = new File(sd, getAppName(context)); 48 | File sub = new File(dir, subName); 49 | sub.mkdirs(); 50 | return sub; 51 | } 52 | 53 | public static boolean sdAvailible() { 54 | String state = Environment.getExternalStorageState(); 55 | if (Environment.MEDIA_MOUNTED.equals(state)) { 56 | return true; 57 | } else { 58 | return false; 59 | } 60 | } 61 | 62 | public static String encrypt(String str) { 63 | // TODO: encrypt data. 64 | return str; 65 | } 66 | 67 | public static String buildSystemInfo(Context context) { 68 | StringBuilder buffer = new StringBuilder(); 69 | buffer.append("\n"); 70 | buffer.append("#-------system info-------"); 71 | buffer.append("\n"); 72 | buffer.append("version-name:"); 73 | buffer.append(Utils.getVersionName(context)); 74 | buffer.append("\n"); 75 | buffer.append("version-code:"); 76 | buffer.append(Utils.getVersionCode(context)); 77 | buffer.append("\n"); 78 | buffer.append("system-version:"); 79 | buffer.append(Utils.getSystemVersion(context)); 80 | buffer.append("\n"); 81 | buffer.append("model:"); 82 | buffer.append(Utils.getModel(context)); 83 | buffer.append("\n"); 84 | buffer.append("density:"); 85 | buffer.append(Utils.getDensity(context)); 86 | buffer.append("\n"); 87 | buffer.append("imei:"); 88 | buffer.append(Utils.getIMEI(context)); 89 | buffer.append("\n"); 90 | buffer.append("screen-height:"); 91 | buffer.append(Utils.getScreenHeight(context)); 92 | buffer.append("\n"); 93 | buffer.append("screen-width:"); 94 | buffer.append(Utils.getScreenWidth(context)); 95 | buffer.append("\n"); 96 | buffer.append("unique-code:"); 97 | buffer.append(Utils.getUniqueCode(context)); 98 | buffer.append("\n"); 99 | buffer.append("mobile:"); 100 | buffer.append(Utils.getMobile(context)); 101 | buffer.append("\n"); 102 | buffer.append("imsi:"); 103 | buffer.append(Utils.getProvider(context)); 104 | buffer.append("\n"); 105 | buffer.append("isWifi:"); 106 | buffer.append(Utils.isWifi(context)); 107 | buffer.append("\n"); 108 | return buffer.toString(); 109 | } 110 | 111 | public static String getUniqueCode(Context context) { 112 | if (context == null) 113 | return null; 114 | String imei = getIMEI(context); 115 | WifiManager wifi = (WifiManager) context 116 | .getSystemService(Context.WIFI_SERVICE); 117 | WifiInfo info = wifi.getConnectionInfo(); 118 | String mUniqueCode = imei + "_" + info.getMacAddress(); 119 | return mUniqueCode; 120 | } 121 | 122 | public static boolean isWifi(Context context) { 123 | if (context == null) { 124 | return false; 125 | } 126 | ConnectivityManager connectivityManager = (ConnectivityManager) context 127 | .getSystemService(Context.CONNECTIVITY_SERVICE); 128 | NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 129 | if (activeNetInfo != null 130 | && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 131 | return true; 132 | } 133 | return false; 134 | } 135 | 136 | public static String getMobile(Context context) { 137 | TelephonyManager telephonyManager = (TelephonyManager) context 138 | .getSystemService(Context.TELEPHONY_SERVICE); 139 | return telephonyManager.getLine1Number(); 140 | } 141 | 142 | public static String getProvider(Context context) { 143 | TelephonyManager telephonyManager = (TelephonyManager) context 144 | .getSystemService(Context.TELEPHONY_SERVICE); 145 | return telephonyManager.getSubscriberId(); 146 | } 147 | 148 | public static final String getIMEI(final Context context) { 149 | TelephonyManager manager = (TelephonyManager) context 150 | .getSystemService(Context.TELEPHONY_SERVICE); 151 | return manager.getDeviceId(); 152 | } 153 | 154 | public static int getScreenWidth(Context context) { 155 | DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 156 | return metrics.widthPixels; 157 | } 158 | 159 | public static int getScreenHeight(Context context) { 160 | DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 161 | return metrics.heightPixels; 162 | } 163 | 164 | public static String getSystemVersion(Context context) { 165 | return android.os.Build.VERSION.RELEASE; 166 | } 167 | 168 | public static String getModel(Context context) { 169 | return android.os.Build.MODEL != null ? android.os.Build.MODEL.replace( 170 | " ", "") : "unknown"; 171 | } 172 | 173 | public static float getDensity(Context context) { 174 | return context.getResources().getDisplayMetrics().density; 175 | } 176 | 177 | public static String getVersionName(Context context) { 178 | try { 179 | PackageInfo pinfo = context.getPackageManager() 180 | .getPackageInfo(context.getPackageName(), 181 | PackageManager.GET_CONFIGURATIONS); 182 | return pinfo.versionName; 183 | } catch (NameNotFoundException e) { 184 | } 185 | 186 | return ""; 187 | } 188 | 189 | public static String getAppName(Context context) { 190 | if (TextUtils.isEmpty(sAppName)) { 191 | sAppName = "com_forlong401_log"; 192 | try { 193 | PackageInfo pinfo = context.getPackageManager().getPackageInfo( 194 | context.getPackageName(), 195 | PackageManager.GET_CONFIGURATIONS); 196 | String packageName = pinfo.packageName; 197 | if (!TextUtils.isEmpty(packageName)) { 198 | sAppName = packageName.replaceAll("\\.", "_"); 199 | } 200 | } catch (NameNotFoundException e) { 201 | } 202 | } 203 | 204 | return sAppName; 205 | } 206 | 207 | public static int getVersionCode(Context context) { 208 | try { 209 | PackageInfo pinfo = context.getPackageManager() 210 | .getPackageInfo(context.getPackageName(), 211 | PackageManager.GET_CONFIGURATIONS); 212 | return pinfo.versionCode; 213 | } catch (NameNotFoundException e) { 214 | } 215 | 216 | return 1; 217 | } 218 | 219 | } 220 | -------------------------------------------------------------------------------- /Log/src/com/forlong401/log/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.forlong401.log.ui; 17 | 18 | import android.app.Activity; 19 | import android.os.Bundle; 20 | import android.view.Menu; 21 | 22 | import com.forlong401.log.R; 23 | import com.forlong401.log.transaction.log.manager.LogManager; 24 | import com.forlong401.log.transaction.utils.LogUtils; 25 | 26 | public class MainActivity extends Activity { 27 | private static final String TAG = "MainActivity"; 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_main); 33 | LogManager.getManager(getApplicationContext()).registerActivity(this); 34 | LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", 35 | LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT); 36 | String crashNullException = null; 37 | crashNullException.charAt(1); 38 | } 39 | 40 | @Override 41 | public boolean onCreateOptionsMenu(Menu menu) { 42 | // Inflate the menu; this adds items to the action bar if it is present. 43 | getMenuInflater().inflate(R.menu.main, menu); 44 | return true; 45 | } 46 | 47 | @Override 48 | protected void onDestroy() { 49 | LogManager.getManager(getApplicationContext()).unregisterActivity(this); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Log Collector 2 | === 3 | 4 | Collect the normal or crash log in Android, then save them into files or upload into server. 5 | 6 | === 7 | 8 | 1. How using the libs? 9 | 10 | 1.1 step1: 11 | 12 | Add the below permission into your manifest xml. 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 1.2 step2: 23 | 24 | Add output\log.jar into your libs folder in your project. 25 | 26 | 1.3 step3: 27 | 28 | Register or unregister the crash handler in your application 29 | 30 | /** 31 | * Log application. 32 | * 33 | * @author Li Cong 34 | * @date 2014-3-23 35 | */ 36 | 37 | public class LogApp extends Application { 38 | 39 | @Override 40 | public void onCreate() { 41 | super.onCreate(); 42 | LogManager.getManager(getApplicationContext()).registerCrashHandler(); 43 | } 44 | 45 | @Override 46 | public void onTerminate() { 47 | super.onTerminate(); 48 | LogManager.getManager(getApplicationContext()).unregisterCrashHandler(); 49 | } 50 | } 51 | 52 | 1.4 step4(Collect carsh log done.): 53 | 54 | Register the activity in the onCreate() of Activity.Unregister the activity in the onDestroy() of Activity. You should register and unregister for all activities in your manifest xml. 55 | 56 | public class MainActivity extends Activity { 57 | private static final String TAG = "MainActivity"; 58 | 59 | @Override 60 | protected void onCreate(Bundle savedInstanceState) { 61 | super.onCreate(savedInstanceState); 62 | setContentView(R.layout.activity_main); 63 | LogManager.getManager(getApplicationContext()).registerActivity(this); 64 | LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", 65 | LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT); 66 | String crashNullException = null; 67 | crashNullException.charAt(1); 68 | } 69 | 70 | @Override 71 | public boolean onCreateOptionsMenu(Menu menu) { 72 | // Inflate the menu; this adds items to the action bar if it is present. 73 | getMenuInflater().inflate(R.menu.main, menu); 74 | return true; 75 | } 76 | 77 | @Override 78 | protected void onDestroy() { 79 | LogManager.getManager(getApplicationContext()).unregisterActivity(this); 80 | } 81 | 82 | } 83 | 84 | 1.5 step5(optional): 85 | 86 | If you need collect the normal log into files or server, you just should call the below method. 87 | 88 | LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT); 89 | 90 | === 91 | 92 | Enable or disable the log: 93 | 94 | public static boolean DEBUG = true; 95 | public static boolean CRASH_SAVE_2_FILE = true; 96 | public static boolean CRASH_UPLOAD_2_NETWORK = false; 97 | 98 | === 99 | 100 | Find the log files: 101 | 102 | SD card dir + package name + log/crash + files. Such as: 103 | sd dir/com_forlong401_log/log/log_timestamp.txt -->normal log 104 | sd dir/com_forlong401_log/crash/crash_timestamp.txt -->crash log 105 | 106 | === 107 | 108 | Advance skills-1: 109 | 110 | Encrypt your data: 111 | 112 | public final class Utils { 113 | 114 | public static String encrypt(String str) { 115 | 116 | // TODO: encrypt data. 117 | 118 | return str; 119 | 120 | } 121 | 122 | } 123 | 124 | Advance skills-2: 125 | 126 | Implement your upload method using your server log API: 127 | 128 | /** 129 | * Handle log task. 130 | * 131 | * @author Li Cong 132 | * @date 2014-3-23 133 | */ 134 | 135 | public class LogTask implements Runnable { 136 | 137 | private void log2Network(String tag, String msg) { 138 | 139 | // TODO: Server API for upload message. 140 | 141 | // TODO: Encode and encrypt the message. 142 | 143 | } 144 | 145 | } 146 | 147 | === 148 | 149 | Copyright, license and contact: 150 | 151 | /* 152 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 153 | * 154 | * Licensed under the Apache License, Version 2.0 (the "License"); 155 | * you may not use this file except in compliance with the License. 156 | * You may obtain a copy of the License at 157 | * 158 | * http://www.apache.org/licenses/LICENSE-2.0 159 | * 160 | * Unless required by applicable law or agreed to in writing, software 161 | * distributed under the License is distributed on an "AS IS" BASIS, 162 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163 | * See the License for the specific language governing permissions and 164 | * limitations under the License. 165 | */ 166 | 167 | 168 | 169 | === 170 | === 171 | 172 | 日志收集器 173 | === 174 | 175 | 记录普通或者crash的日志到文件或网络服务器。 176 | 177 | === 178 | 179 | 1. 如何使用这个库? 180 | 181 | 1.1 步骤一: 182 | 183 | 添加这些permission到你的manifest文件中。 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 1.2 步骤二: 194 | 195 | 添加output目录下的log.jar到你工程的libs目录下,刷新。 196 | 197 | 1.3 步骤三: 198 | 199 | 在Application中Register 或 unregister crash handler。 200 | 201 | /** 202 | * Log application. 203 | * 204 | * @author Li Cong 205 | * @date 2014-3-23 206 | */ 207 | 208 | public class LogApp extends Application { 209 | 210 | @Override 211 | public void onCreate() { 212 | super.onCreate(); 213 | LogManager.getManager(getApplicationContext()).registerCrashHandler(); 214 | } 215 | 216 | @Override 217 | public void onTerminate() { 218 | super.onTerminate(); 219 | LogManager.getManager(getApplicationContext()).unregisterCrashHandler(); 220 | } 221 | } 222 | 223 | 1.4 步骤四(如果只收集crash,这步骤搞完就ok了): 224 | 225 | 在每个Activity的onCreate()中Register,onDestroy()中unregister.所有的Activity都要添加。 226 | 227 | public class MainActivity extends Activity { 228 | private static final String TAG = "MainActivity"; 229 | 230 | @Override 231 | protected void onCreate(Bundle savedInstanceState) { 232 | super.onCreate(savedInstanceState); 233 | setContentView(R.layout.activity_main); 234 | LogManager.getManager(getApplicationContext()).registerActivity(this); 235 | LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", 236 | LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT); 237 | String crashNullException = null; 238 | crashNullException.charAt(1); 239 | } 240 | 241 | @Override 242 | public boolean onCreateOptionsMenu(Menu menu) { 243 | // Inflate the menu; this adds items to the action bar if it is present. 244 | getMenuInflater().inflate(R.menu.main, menu); 245 | return true; 246 | } 247 | 248 | @Override 249 | protected void onDestroy() { 250 | LogManager.getManager(getApplicationContext()).unregisterActivity(this); 251 | } 252 | 253 | } 254 | 255 | 1.5 步骤五(打log): 256 | 257 | 如果你要收集普通的日志到文件或者服务器,那么调用下面的方法即可。 258 | 259 | LogManager.getManager(getApplicationContext()).log(TAG, "onCreate", LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT); 260 | 261 | === 262 | 263 | 打开或关闭log: 264 | 265 | public static boolean DEBUG = true; // 关闭或打开普通日志 266 | 267 | public static boolean CRASH_SAVE_2_FILE = true;// 关闭或打开crash日志写入文件。 268 | 269 | public static boolean CRASH_UPLOAD_2_NETWORK = false;// 关闭或打开crash日志上传服务器。 270 | 271 | === 272 | 273 | 去哪里找到你的日志文件: 274 | 275 | SD card 目录下的包名中点替换为下划线的文件夹下的 + log/crash + 日志文件. 例如: 276 | 277 | sd dir/com_forlong401_log/log/log_timestamp.txt -->普通日志 278 | 279 | sd dir/com_forlong401_log/crash/crash_timestamp.txt -->crash日志 280 | 281 | === 282 | 283 | 高级技能-1: 284 | 285 | 加密你的数据: 286 | 287 | public final class Utils { 288 | 289 | public static String encrypt(String str) { 290 | 291 | // TODO: encrypt data. 292 | 293 | return str; 294 | 295 | } 296 | 297 | } 298 | 299 | 高级技能-2: 300 | 301 | 实现你上传日志的代码: 302 | 303 | /** 304 | * Handle log task. 305 | * 306 | * @author Li Cong 307 | * @date 2014-3-23 308 | */ 309 | 310 | public class LogTask implements Runnable { 311 | 312 | private void log2Network(String tag, String msg) { 313 | 314 | // TODO: Server API for upload message. 315 | 316 | // TODO: Encode and encrypt the message. 317 | 318 | } 319 | 320 | } 321 | 322 | 323 | === 324 | 325 | 版权, 授权协议和联系方式: 326 | 327 | /* 328 | * Copyright (C) 2014 Li Cong, forlong401@163.com http://www.360qihoo.com 329 | * 330 | * Licensed under the Apache License, Version 2.0 (the "License"); 331 | * you may not use this file except in compliance with the License. 332 | * You may obtain a copy of the License at 333 | * 334 | * http://www.apache.org/licenses/LICENSE-2.0 335 | * 336 | * Unless required by applicable law or agreed to in writing, software 337 | * distributed under the License is distributed on an "AS IS" BASIS, 338 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 339 | * See the License for the specific language governing permissions and 340 | * limitations under the License. 341 | */ 342 | 343 | 344 | 345 | -------------------------------------------------------------------------------- /output/log.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/licong/log/28177f85125cb7b93a1e18d5e216a755e2b99a39/output/log.jar --------------------------------------------------------------------------------