├── .classpath ├── .project ├── LICENSE ├── README.md ├── libs ├── MyJavaLib.jar ├── commons-io-2.4.jar └── swt.jar ├── out ├── AndroidLogViewer.jar ├── AndroidLogViewer_lib │ ├── MyJavaLib.jar │ ├── commons-io-2.4.jar │ └── swt.jar └── run.bat ├── pics └── p1.jpg └── src ├── AndroidLogViewer.java ├── FilterConfig.java ├── FilterEditDialog.java ├── LogFilter.java └── LogInfo.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | AndroidLogViewer 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /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. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AndroidLogViewer 2 | ================ 3 | 4 | A tool for android developers to help them view log files more efficient.
5 | I am an android developer and it is always annoying me when a bug issue contains several log files which is so large and contains more than 10000 lines of log. Reading these log file is a hard job. As google didn't offer a tool for reading android log files, so I have to develop my own one. I started to modify DDMS's logcat tool and added some useful features, so that I can use this tool to improve my work efficiency. If you have suffered the same problem, you can just use my tool. Just download and enter into directory **out**, click **run.bat** and that's over. 6 | 7 | Overview 8 | -------- 9 | ![](https://raw.githubusercontent.com/lrscp/AndroidLogViewer/master/pics/p1.jpg) 10 | 11 | Functions 12 | --------- 13 | 14 | - Parse android log file and show them in a colorful way, similar to the DDMS's logcat. The difference is this can show log file which DDMS's logcat can't do. 15 | 16 | - Filter log items, so that you will find desired log more efficiently. 17 | 18 | - Copy log item or items. 19 | 20 | - Add log item to fast location list.
21 | 22 | This is a very useful function when you want to locate the log item when you change your filter or some other situations. 23 | 24 | - Listening for log clients using tcp and show logs in real time 25 | 26 | This is a simple protocol for my application development. Using an unified tool to manage logs from many applications that I developed. 27 | 28 | 29 | Contact me 30 | ---------- 31 | * lrscp(675486378@qq.com) -------------------------------------------------------------------------------- /libs/MyJavaLib.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/libs/MyJavaLib.jar -------------------------------------------------------------------------------- /libs/commons-io-2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/libs/commons-io-2.4.jar -------------------------------------------------------------------------------- /libs/swt.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/libs/swt.jar -------------------------------------------------------------------------------- /out/AndroidLogViewer.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/out/AndroidLogViewer.jar -------------------------------------------------------------------------------- /out/AndroidLogViewer_lib/MyJavaLib.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/out/AndroidLogViewer_lib/MyJavaLib.jar -------------------------------------------------------------------------------- /out/AndroidLogViewer_lib/commons-io-2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/out/AndroidLogViewer_lib/commons-io-2.4.jar -------------------------------------------------------------------------------- /out/AndroidLogViewer_lib/swt.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/out/AndroidLogViewer_lib/swt.jar -------------------------------------------------------------------------------- /out/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | start javaw -jar AndroidLogViewer.jar -------------------------------------------------------------------------------- /pics/p1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lrscp/AndroidLogViewer/155871400c034a58850704de9235efbdd6aad787/pics/p1.jpg -------------------------------------------------------------------------------- /src/AndroidLogViewer.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import java.awt.datatransfer.Clipboard; 3 | import java.awt.datatransfer.StringSelection; 4 | import java.io.BufferedReader; 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.io.InputStreamReader; 8 | import java.net.ServerSocket; 9 | import java.net.Socket; 10 | import java.util.ArrayList; 11 | import java.util.HashSet; 12 | import java.util.LinkedList; 13 | import java.util.List; 14 | import java.util.Set; 15 | 16 | import lrscp.lib.Log; 17 | import lrscp.lib.Preference; 18 | import lrscp.lib.swt.SwtUtils; 19 | 20 | import org.apache.commons.io.FileUtils; 21 | import org.eclipse.swt.SWT; 22 | import org.eclipse.swt.dnd.DND; 23 | import org.eclipse.swt.dnd.DropTarget; 24 | import org.eclipse.swt.dnd.DropTargetAdapter; 25 | import org.eclipse.swt.dnd.DropTargetEvent; 26 | import org.eclipse.swt.dnd.FileTransfer; 27 | import org.eclipse.swt.dnd.Transfer; 28 | import org.eclipse.swt.events.ControlEvent; 29 | import org.eclipse.swt.events.ControlListener; 30 | import org.eclipse.swt.events.KeyEvent; 31 | import org.eclipse.swt.events.KeyListener; 32 | import org.eclipse.swt.events.SelectionAdapter; 33 | import org.eclipse.swt.events.SelectionEvent; 34 | import org.eclipse.swt.events.SelectionListener; 35 | import org.eclipse.swt.graphics.Color; 36 | import org.eclipse.swt.layout.GridData; 37 | import org.eclipse.swt.layout.GridLayout; 38 | import org.eclipse.swt.widgets.Button; 39 | import org.eclipse.swt.widgets.Combo; 40 | import org.eclipse.swt.widgets.Composite; 41 | import org.eclipse.swt.widgets.Display; 42 | import org.eclipse.swt.widgets.Event; 43 | import org.eclipse.swt.widgets.Label; 44 | import org.eclipse.swt.widgets.Listener; 45 | import org.eclipse.swt.widgets.Menu; 46 | import org.eclipse.swt.widgets.MenuItem; 47 | import org.eclipse.swt.widgets.MessageBox; 48 | import org.eclipse.swt.widgets.Shell; 49 | import org.eclipse.swt.widgets.Table; 50 | import org.eclipse.swt.widgets.TableColumn; 51 | import org.eclipse.swt.widgets.TableItem; 52 | import org.eclipse.swt.widgets.Text; 53 | 54 | public class AndroidLogViewer extends Shell { 55 | private static class LogClient implements Runnable { 56 | private static final int MAX_LINES = 10000; 57 | private List mClientLogs = new LinkedList(); 58 | private LogListener mLsnr; 59 | private String mName = ""; 60 | private Socket mSocket; 61 | private boolean mPause = false; 62 | private boolean isReadingLines = false; 63 | 64 | public LogClient(Socket socket) { 65 | mSocket = socket; 66 | } 67 | 68 | public List getLogs() { 69 | return mClientLogs; 70 | } 71 | 72 | public String getName() { 73 | return mName; 74 | } 75 | 76 | @Override 77 | public void run() { 78 | Log.d(TAG, "start client " + mSocket); 79 | try { 80 | BufferedReader br = new BufferedReader(new InputStreamReader(mSocket.getInputStream(), "utf-8")); 81 | isReadingLines = true; 82 | mName = br.readLine(); 83 | if (shell != null) { 84 | shell.updateClientsView(); 85 | } 86 | String line = null; 87 | int i = 0; 88 | isReadingLines = true; 89 | while ((line = br.readLine()) != null) { 90 | isReadingLines = false; 91 | while (mPause) { 92 | isReadingLines = true; 93 | try { 94 | Thread.sleep(100); 95 | } catch (InterruptedException e) {} 96 | } 97 | LogInfo info = new LogInfo(line, i++); 98 | synchronized (this) { 99 | if (mClientLogs.size() < MAX_LINES) { 100 | mClientLogs.add(info); 101 | } else { 102 | mClientLogs.remove(0); 103 | mClientLogs.add(info); 104 | } 105 | } 106 | if (mLsnr != null) { 107 | mLsnr.onLogAdded(this, info); 108 | } 109 | isReadingLines = true; 110 | } 111 | } catch (IOException e) { 112 | e.printStackTrace(); 113 | mClients.remove(this); 114 | if (shell != null) { 115 | shell.updateClientsView(); 116 | } 117 | } 118 | } 119 | 120 | public void setLogListener(LogListener lsnr) { 121 | mLsnr = lsnr; 122 | } 123 | 124 | public void start() { 125 | new Thread(this).start(); 126 | } 127 | 128 | /** 129 | * Stop receiving new log items until resume is called. Blocked until 130 | * the thread is paused. 131 | */ 132 | public synchronized void pause() { 133 | mPause = true; 134 | while (!isReadingLines) { 135 | try { 136 | Thread.sleep(100); 137 | } catch (InterruptedException e) {} 138 | } 139 | } 140 | 141 | /** 142 | * Start receiving new log items. 143 | */ 144 | public void resume() { 145 | mPause = false; 146 | } 147 | 148 | public synchronized void clearLogs() { 149 | mClientLogs.clear(); 150 | } 151 | } 152 | 153 | private static interface LogListener { 154 | void onLogAdded(LogClient client, LogInfo info); 155 | } 156 | 157 | private static class LogServerThread implements Runnable { 158 | public LogServerThread() {} 159 | 160 | @Override 161 | public void run() { 162 | ServerSocket server = null; 163 | try { 164 | server = new ServerSocket(Log.LOG_VIEWER_PORT); 165 | while (true) { 166 | Socket socket = server.accept(); 167 | LogClient client = new LogClient(socket); 168 | mClients.add(client); 169 | client.start(); 170 | } 171 | } catch (IOException e) { 172 | e.printStackTrace(); 173 | } 174 | } 175 | } 176 | 177 | public static class TimeRecorder { 178 | private static TimeRecorder sInstance = null; 179 | 180 | public static TimeRecorder getInstance() { 181 | if (sInstance == null) { 182 | sInstance = new TimeRecorder(); 183 | } 184 | return sInstance; 185 | } 186 | 187 | private long mTableLoadStart; 188 | 189 | public long setTableLoadEnd() { 190 | return System.currentTimeMillis() - mTableLoadStart; 191 | } 192 | 193 | public void setTableLoadStart() { 194 | mTableLoadStart = System.currentTimeMillis(); 195 | } 196 | } 197 | 198 | private static final String TAG = AndroidLogViewer.class.getSimpleName(); 199 | 200 | private static Display display; 201 | private static List mClients = new ArrayList(); 202 | private static Thread mLogServerThread = null; 203 | private static AndroidLogViewer shell; 204 | 205 | private static boolean isServerPortInUse() { 206 | try { 207 | ServerSocket server = new ServerSocket(Log.LOG_VIEWER_PORT); 208 | server.close(); 209 | return false; 210 | } catch (Exception e) { 211 | e.printStackTrace(); 212 | } 213 | return true; 214 | } 215 | 216 | /** 217 | * Launch the application. 218 | * 219 | * @param args 220 | */ 221 | public static void main(String args[]) { 222 | try { 223 | display = Display.getDefault(); 224 | if (isServerPortInUse()) { 225 | MessageBox msg = new MessageBox(new Shell()); 226 | msg.setText("Warning"); 227 | msg.setMessage("AndroidLogViewer is running!"); 228 | msg.open(); 229 | Display.getDefault().readAndDispatch(); 230 | System.exit(0); 231 | return; 232 | } 233 | startLogServer(); 234 | shell = new AndroidLogViewer(display); 235 | shell.open(); 236 | shell.layout(); 237 | while (!shell.isDisposed()) { 238 | if (!display.readAndDispatch()) { 239 | display.sleep(); 240 | } 241 | } 242 | } catch (Exception e) { 243 | Log.e(TAG, "exception in main " + e.getLocalizedMessage()); 244 | } 245 | System.exit(0); 246 | } 247 | 248 | private static void startLogServer() { 249 | if (mLogServerThread == null) { 250 | mLogServerThread = new Thread(new LogServerThread()); 251 | mLogServerThread.start(); 252 | } 253 | } 254 | 255 | private Combo cbLevel; 256 | private Composite cpLeft; 257 | private org.eclipse.swt.widgets.List list; 258 | private File mCurFile; 259 | 260 | private Set mFastLocateItems = new HashSet(); 261 | 262 | private List mFileLogs = new LinkedList(); 263 | 264 | /** The final log items that will be shown in the tabel **/ 265 | private List mFilteredLogs = new LinkedList(); 266 | 267 | private LogListener mLogLsnr = new LogListener() { 268 | @Override 269 | public void onLogAdded(final LogClient client, final LogInfo log) { 270 | getDisplay().asyncExec(new Runnable() { 271 | @Override 272 | public void run() { 273 | if (mCurClient == client) { 274 | onTableLogAdded(log); 275 | } else { 276 | // Log.e(TAG, "onLogAdded diff client!"); 277 | } 278 | } 279 | }); 280 | } 281 | }; 282 | 283 | private List mLogs = new LinkedList(); 284 | 285 | Listener mLsnr = new Listener() { 286 | public void handleEvent(Event event) { 287 | TableItem item = (TableItem) event.item; 288 | int index = event.index; 289 | LogInfo log = mFilteredLogs.get(index); 290 | showLog(item, index, log); 291 | } 292 | }; 293 | 294 | private boolean mScrollEnabled; 295 | 296 | private Table table; 297 | 298 | private TableColumn tblclmnContent; 299 | 300 | private TableColumn tblclmnIndex; 301 | 302 | private TableColumn tblclmnLevel; 303 | 304 | private TableColumn tblclmnPid; 305 | 306 | private TableColumn tblclmnTag; 307 | 308 | private TableColumn tblclmnTid; 309 | 310 | private TableColumn tblclmnTime; 311 | 312 | private Text text; 313 | private Button btnCheckButton; 314 | 315 | /** Update the client's log count thread **/ 316 | private Thread mUpdateClientsCountThread; 317 | 318 | private LogClient mCurClient; 319 | private Composite composite_1; 320 | 321 | private org.eclipse.swt.widgets.List mFilterListView; 322 | 323 | private List mFilterConfigs = new ArrayList(); 324 | private Button butnClear; 325 | private Button checkShowUnTaged; 326 | 327 | private boolean isShowExtra = true; 328 | 329 | /** 330 | * Create the shell. 331 | * 332 | * @param display 333 | */ 334 | public AndroidLogViewer(Display display) { 335 | super(display, SWT.SHELL_TRIM); 336 | setLayout(new GridLayout(2, false)); 337 | 338 | cpLeft = new Composite(this, SWT.NONE); 339 | cpLeft.setLayout(new GridLayout(1, false)); 340 | GridData gd_cpLeft = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 2); 341 | gd_cpLeft.widthHint = 241; 342 | cpLeft.setLayoutData(gd_cpLeft); 343 | 344 | Label lblLog = new Label(cpLeft, SWT.NONE); 345 | lblLog.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1)); 346 | lblLog.setText("Clients"); 347 | 348 | list = new org.eclipse.swt.widgets.List(cpLeft, SWT.BORDER | SWT.RESIZE); 349 | GridData gd_list = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1); 350 | gd_list.widthHint = 198; 351 | list.setLayoutData(gd_list); 352 | 353 | Label label = new Label(cpLeft, SWT.NONE); 354 | label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1)); 355 | label.setText("Filters"); 356 | 357 | composite_1 = new Composite(cpLeft, SWT.NONE); 358 | composite_1.setLayout(new GridLayout(3, false)); 359 | composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); 360 | 361 | Button butnAddFilter = new Button(composite_1, SWT.NONE); 362 | butnAddFilter.addSelectionListener(new SelectionAdapter() { 363 | @Override 364 | public void widgetSelected(SelectionEvent e) { 365 | createAddFilterDialog(); 366 | } 367 | }); 368 | butnAddFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 369 | butnAddFilter.setBounds(0, 0, 92, 30); 370 | butnAddFilter.setText("Add"); 371 | 372 | Button butnDelFilter = new Button(composite_1, SWT.NONE); 373 | butnDelFilter.addSelectionListener(new SelectionAdapter() { 374 | @Override 375 | public void widgetSelected(SelectionEvent e) { 376 | int index = mFilterListView.getSelectionIndex(); 377 | if (index > 0) { 378 | createDelFilterDialog(index - 1); 379 | } 380 | } 381 | }); 382 | butnDelFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 383 | butnDelFilter.setText("Delete"); 384 | 385 | Button butnEditFilter = new Button(composite_1, SWT.NONE); 386 | butnEditFilter.addSelectionListener(new SelectionAdapter() { 387 | @Override 388 | public void widgetSelected(SelectionEvent e) { 389 | int index = mFilterListView.getSelectionIndex(); 390 | if (index > 0) { 391 | createEditFilterDialog(index - 1); 392 | } 393 | } 394 | }); 395 | butnEditFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 396 | butnEditFilter.setText("Edit"); 397 | 398 | list.addSelectionListener(new SelectionAdapter() { 399 | @Override 400 | public void widgetSelected(SelectionEvent e) { 401 | onSelectClient(list.getSelectionIndex()); 402 | } 403 | }); 404 | updateClientsView(); 405 | startUpdateClientLogCountThread(); 406 | 407 | mFilterListView = new org.eclipse.swt.widgets.List(cpLeft, SWT.BORDER); 408 | mFilterListView.addSelectionListener(new SelectionAdapter() { 409 | @Override 410 | public void widgetSelected(SelectionEvent e) { 411 | loadTableByCurrentFilter(); 412 | } 413 | }); 414 | mFilterListView.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1)); 415 | 416 | Composite composite = new Composite(this, SWT.NONE); 417 | composite.setLayout(new GridLayout(5, false)); 418 | composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 419 | 420 | text = new Text(composite, SWT.BORDER); 421 | text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 422 | text.addKeyListener(new KeyListener() { 423 | @Override 424 | public void keyPressed(KeyEvent e) {} 425 | 426 | @Override 427 | public void keyReleased(KeyEvent e) { 428 | // press Enter 429 | if (e.keyCode == 13) { 430 | loadTableByCurrentFilter(); 431 | } 432 | } 433 | }); 434 | 435 | btnCheckButton = new Button(composite, SWT.CHECK); 436 | btnCheckButton.addSelectionListener(new SelectionAdapter() { 437 | @Override 438 | public void widgetSelected(SelectionEvent e) { 439 | mScrollEnabled = btnCheckButton.getSelection(); 440 | scrollToBottomIfNeeded(); 441 | } 442 | }); 443 | btnCheckButton.setText("scroll"); 444 | btnCheckButton.setSelection(true); 445 | mScrollEnabled = btnCheckButton.getSelection(); 446 | 447 | checkShowUnTaged = new Button(composite, SWT.CHECK); 448 | checkShowUnTaged.addSelectionListener(new SelectionAdapter() { 449 | @Override 450 | public void widgetSelected(SelectionEvent arg0) { 451 | handleCheckShowExtra(); 452 | } 453 | }); 454 | checkShowUnTaged.setText("Show Extra"); 455 | checkShowUnTaged.setSelection(true); 456 | 457 | butnClear = new Button(composite, SWT.NONE); 458 | butnClear.addSelectionListener(new SelectionAdapter() { 459 | @Override 460 | public void widgetSelected(SelectionEvent e) { 461 | onClearLogs(); 462 | } 463 | }); 464 | butnClear.setText("Clear"); 465 | 466 | cbLevel = new Combo(composite, SWT.NONE); 467 | GridData gd_combo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1); 468 | gd_combo.widthHint = 60; 469 | cbLevel.setLayoutData(gd_combo); 470 | cbLevel.setItems(LogInfo.levelStrings); 471 | cbLevel.select(0); 472 | cbLevel.addSelectionListener(new SelectionListener() { 473 | @Override 474 | public void widgetDefaultSelected(SelectionEvent e) {} 475 | 476 | @Override 477 | public void widgetSelected(SelectionEvent e) { 478 | loadTableByCurrentFilter(); 479 | } 480 | }); 481 | 482 | table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI | SWT.VIRTUAL); 483 | table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); 484 | table.setHeaderVisible(true); 485 | table.setLinesVisible(true); 486 | table.setMenu(createPopupMenu(new Menu(this, SWT.POP_UP))); 487 | createDropTarget(); 488 | 489 | /* 490 | * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. 491 | * Therefore, it is critical for performance that these methods be as 492 | * efficient as possible. 493 | */ 494 | // Listener paintListener = new Listener() { 495 | // public void handleEvent(Event event) { 496 | // switch (event.type) { 497 | // case SWT.MeasureItem: { 498 | // TableItem item = (TableItem) event.item; 499 | // String text = item.getText(event.index); 500 | // Point size = event.gc.textExtent(text); 501 | // event.width = size.x; 502 | // event.height = Math.max(event.height, size.y); 503 | // break; 504 | // } 505 | // case SWT.PaintItem: { 506 | // TableItem item = (TableItem) event.item; 507 | // String text = item.getText(event.index); 508 | // Point size = event.gc.textExtent(text); 509 | // int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) 510 | // / 2) : 0; 511 | // event.gc.drawText(text, event.x, event.y + offset2, true); 512 | // break; 513 | // } 514 | // case SWT.EraseItem: { 515 | // event.detail &= ~SWT.FOREGROUND; 516 | // break; 517 | // } 518 | // } 519 | // } 520 | // }; 521 | // table.addListener(SWT.MeasureItem, paintListener); 522 | // table.addListener(SWT.PaintItem, paintListener); 523 | // table.addListener(SWT.EraseItem, paintListener); 524 | 525 | tblclmnIndex = new TableColumn(table, SWT.NONE); 526 | tblclmnIndex.setWidth(Preference.getInt("indexColumnW", 60)); 527 | tblclmnIndex.setText("index"); 528 | tblclmnIndex.addControlListener(createControlLsnr("indexColumnW", tblclmnIndex)); 529 | 530 | tblclmnLevel = new TableColumn(table, SWT.NONE); 531 | tblclmnLevel.setWidth(Preference.getInt("levelColumnW", 41)); 532 | tblclmnLevel.setText("Level"); 533 | tblclmnLevel.addControlListener(createControlLsnr("levelColumnW", tblclmnLevel)); 534 | 535 | tblclmnTime = new TableColumn(table, SWT.NONE); 536 | tblclmnTime.setWidth(Preference.getInt("timeColumnW", 93)); 537 | tblclmnTime.setText("Time"); 538 | tblclmnTime.addControlListener(createControlLsnr("timeColumnW", tblclmnTime)); 539 | 540 | tblclmnPid = new TableColumn(table, SWT.NONE); 541 | tblclmnPid.setWidth(Preference.getInt("pidColumnW", 40)); 542 | tblclmnPid.setText("Pid"); 543 | tblclmnPid.addControlListener(createControlLsnr("pidColumnW", tblclmnPid)); 544 | 545 | tblclmnTid = new TableColumn(table, SWT.NONE); 546 | tblclmnTid.setWidth(Preference.getInt("tidColumnW", 39)); 547 | tblclmnTid.setText("Tid"); 548 | tblclmnTid.addControlListener(createControlLsnr("tidColumnW", tblclmnTid)); 549 | 550 | tblclmnTag = new TableColumn(table, SWT.NONE); 551 | tblclmnTag.setWidth(Preference.getInt("tagColumnW", 108)); 552 | tblclmnTag.setText("Tag"); 553 | tblclmnTag.addControlListener(createControlLsnr("tagColumnW", tblclmnTag)); 554 | 555 | tblclmnContent = new TableColumn(table, SWT.NONE); 556 | tblclmnContent.setWidth(Preference.getInt("contentColumnW", 10000)); 557 | tblclmnContent.setText("Content"); 558 | tblclmnContent.addControlListener(createControlLsnr("contentColumnW", tblclmnContent)); 559 | 560 | createContents(); 561 | } 562 | 563 | protected void handleCheckShowExtra() { 564 | isShowExtra = checkShowUnTaged.getSelection(); 565 | loadTableByCurrentFilter(); 566 | } 567 | 568 | protected void onClearLogs() { 569 | if (mCurClient != null) { 570 | mCurClient.clearLogs(); 571 | loadTableByCurrentFilter(); 572 | } 573 | } 574 | 575 | protected void createEditFilterDialog(int index) { 576 | FilterConfig f = mFilterConfigs.get(index); 577 | FilterEditDialog dlg = new FilterEditDialog(shell, 0); 578 | dlg.setFilterConfig(f); 579 | String[] r = (String[]) dlg.open(); 580 | if (r != null) { 581 | f = new FilterConfig(); 582 | f.name = r[0]; 583 | f.tag = r[1]; 584 | mFilterConfigs.set(index, f); 585 | mFilterListView.setItem(index, f.name); 586 | saveFilters(); 587 | loadTableByCurrentFilter(); 588 | } 589 | } 590 | 591 | private void saveFilters() { 592 | StringBuilder sb = new StringBuilder(); 593 | for (FilterConfig f : mFilterConfigs) { 594 | sb.append(f.name); 595 | sb.append(','); 596 | sb.append(f.tag); 597 | sb.append(' '); 598 | } 599 | Preference.setString("mFilterConfigs", sb.toString()); 600 | } 601 | 602 | private void loadFilters() { 603 | String s = Preference.getString("mFilterConfigs", ""); 604 | for (String seg : s.split(" ")) { 605 | if (!seg.isEmpty()) { 606 | String[] segs = seg.split(","); 607 | FilterConfig f = new FilterConfig(); 608 | f.name = segs[0]; 609 | f.tag = segs[1]; 610 | mFilterConfigs.add(f); 611 | } 612 | } 613 | updateFilterList(); 614 | } 615 | 616 | protected void createDelFilterDialog(int index) { 617 | if (SwtUtils.messageOkCancel(shell, "Warning", "Are you sure to delete this filter? " + mFilterConfigs.get(index).name)) { 618 | mFilterConfigs.remove(index); 619 | mFilterListView.remove(index, index); 620 | saveFilters(); 621 | loadTableByCurrentFilter(); 622 | } 623 | } 624 | 625 | protected void createAddFilterDialog() { 626 | FilterEditDialog dlg = new FilterEditDialog(shell, 0); 627 | String[] r = (String[]) dlg.open(); 628 | if (r != null) { 629 | FilterConfig f = new FilterConfig(); 630 | f.name = r[0]; 631 | f.tag = r[1]; 632 | mFilterConfigs.add(f); 633 | mFilterListView.add(f.name); 634 | saveFilters(); 635 | } 636 | } 637 | 638 | private void updateFilterList() { 639 | mFilterListView.removeAll(); 640 | mFilterListView.add("All messages"); 641 | for (FilterConfig f : mFilterConfigs) { 642 | mFilterListView.add(f.name); 643 | } 644 | } 645 | 646 | private void startUpdateClientLogCountThread() { 647 | mUpdateClientsCountThread = new Thread(new Runnable() { 648 | @Override 649 | public void run() { 650 | while (true) { 651 | updateClientsView(); 652 | try { 653 | Thread.sleep(1000); 654 | } catch (InterruptedException e) {} 655 | } 656 | } 657 | }); 658 | mUpdateClientsCountThread.start(); 659 | } 660 | 661 | @Override 662 | protected void checkSubclass() { 663 | // Disable the check that prevents subclassing of SWT components 664 | } 665 | 666 | private void copySelectedItems() { 667 | StringBuilder sb = new StringBuilder(); 668 | for (TableItem ti : table.getSelection()) { 669 | sb.append(((LogInfo) ti.getData()).originalLine); 670 | sb.append("\n"); 671 | } 672 | 673 | Toolkit toolkit = Toolkit.getDefaultToolkit(); 674 | Clipboard clipboard = toolkit.getSystemClipboard(); 675 | StringSelection stringSel = new StringSelection(sb.toString()); 676 | clipboard.setContents(stringSel, null); 677 | } 678 | 679 | /** 680 | * Create contents of the shell. 681 | */ 682 | protected void createContents() { 683 | setText("SWT Application"); 684 | setSize(1211, 732); 685 | SwtUtils.center(this, 0); 686 | setFile(Preference.getString("path", "")); 687 | loadFilters(); 688 | } 689 | 690 | private ControlListener createControlLsnr(final String key, final TableColumn tableColumn) { 691 | return new ControlListener() { 692 | @Override 693 | public void controlMoved(ControlEvent e) {} 694 | 695 | @Override 696 | public void controlResized(ControlEvent e) { 697 | Preference.setInt(key, tableColumn.getWidth()); 698 | } 699 | }; 700 | } 701 | 702 | private DropTarget createDropTarget() { 703 | DropTarget dropTarget = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK); 704 | dropTarget.setTransfer(new Transfer[] { FileTransfer.getInstance() }); 705 | dropTarget.addDropListener(new DropTargetAdapter() { 706 | public void dragEnter(DropTargetEvent event) {} 707 | 708 | public void dragLeave(DropTargetEvent event) {} 709 | 710 | public void dragOver(DropTargetEvent event) {} 711 | 712 | public void drop(DropTargetEvent event) { 713 | String[] files = (String[]) event.data; 714 | if (files != null && files.length == 1) { 715 | setFile(files[0]); 716 | } 717 | } 718 | }); 719 | return dropTarget; 720 | } 721 | 722 | private Menu createPopupMenu(Menu popUpMenu) { 723 | for (MenuItem mi : popUpMenu.getItems()) { 724 | mi.dispose(); 725 | } 726 | 727 | final MenuItem itemCopy = new MenuItem(popUpMenu, SWT.PUSH); 728 | itemCopy.setText("Copy Log"); 729 | itemCopy.addSelectionListener(new SelectionAdapter() { 730 | public void widgetSelected(SelectionEvent e) { 731 | if (table.getSelection().length > 0) { 732 | copySelectedItems(); 733 | } 734 | } 735 | }); 736 | new MenuItem(popUpMenu, SWT.SEPARATOR); 737 | 738 | // final MenuItem itemEdit = new MenuItem(popUpMenu, SWT.PUSH); 739 | // itemEdit.setText("截取后面的Log并保存"); 740 | // itemEdit.addSelectionListener(new SelectionAdapter() { 741 | // public void widgetSelected(SelectionEvent e) { 742 | // if (table.getSelection().length > 0) { 743 | // FileDialog dlg = new FileDialog(getShell()); 744 | // dlg.setFilterExtensions(new String[] { "*.txt", "*.log" }); 745 | // String filePath = dlg.open(); 746 | // Log.i("path " + filePath); 747 | // if (filePath != null) { 748 | // FileWriter fw; 749 | // try { 750 | // fw = new FileWriter(filePath); 751 | // int i = 0; 752 | // int index = table.getSelectionIndex(); 753 | // for (LogInfo log : mLogs) { 754 | // if (i < index) {} else { 755 | // fw.write(log.originalLine); 756 | // fw.write('\n'); 757 | // } 758 | // i++; 759 | // } 760 | // fw.close(); 761 | // } catch (IOException e1) { 762 | // e1.printStackTrace(); 763 | // } 764 | // 765 | // setFile(filePath); 766 | // } 767 | // } 768 | // } 769 | // }); 770 | // new MenuItem(popUpMenu, SWT.SEPARATOR); 771 | 772 | final MenuItem itemAddFastLocate = new MenuItem(popUpMenu, SWT.PUSH); 773 | itemAddFastLocate.setText("Add to fast location list"); 774 | itemAddFastLocate.addSelectionListener(new SelectionAdapter() { 775 | public void widgetSelected(SelectionEvent e) { 776 | if (table.getSelection().length > 0) { 777 | TableItem ti = table.getSelection()[0]; 778 | LogInfo log = (LogInfo) ti.getData(); 779 | mFastLocateItems.add(log); 780 | createPopupMenu(table.getMenu()); 781 | } 782 | } 783 | }); 784 | new MenuItem(popUpMenu, SWT.SEPARATOR); 785 | 786 | for (final LogInfo log : mFastLocateItems) { 787 | final MenuItem item = new MenuItem(popUpMenu, SWT.PUSH); 788 | 789 | // cut the long string 790 | String s = log.content; 791 | if (s.length() > 100) { 792 | s = s.substring(0, 100); 793 | } 794 | item.setText(s); 795 | 796 | item.addSelectionListener(new SelectionAdapter() { 797 | public void widgetSelected(SelectionEvent e) { 798 | scrollToItem(log); 799 | } 800 | }); 801 | } 802 | 803 | final MenuItem item = new MenuItem(popUpMenu, SWT.PUSH); 804 | item.setText("Clear fast location list"); 805 | item.addSelectionListener(new SelectionAdapter() { 806 | public void widgetSelected(SelectionEvent e) { 807 | mFastLocateItems.clear(); 808 | createPopupMenu(table.getMenu()); 809 | } 810 | }); 811 | 812 | new MenuItem(popUpMenu, SWT.SEPARATOR); 813 | 814 | return popUpMenu; 815 | } 816 | 817 | private LogFilter getCurLogFilter() { 818 | String regx = text.getText(); 819 | int index = mFilterListView.getSelectionIndex(); 820 | if (index > 0) { 821 | FilterConfig f = mFilterConfigs.get(index - 1); 822 | regx += "tag:" + f.tag + " "; 823 | } 824 | return new LogFilter(LogInfo.levelStrings[cbLevel.getSelectionIndex()], regx); 825 | } 826 | 827 | private Color getLevelColor(String level) { 828 | Color c = getDisplay().getSystemColor(SWT.COLOR_BLACK); 829 | if (level.equals("I")) { 830 | c = new Color(null, 0x00, 0x99, 0x00); 831 | } else if (level.equals("V")) { 832 | c = new Color(null, 0x66, 0x66, 0x66); 833 | } else if (level.equals("D")) { 834 | c = new Color(null, 0x44, 0x33, 0x33); 835 | } else if (level.equals("W")) { 836 | c = new Color(null, 0xf7, 0x97, 0x09); 837 | } else if (level.equals("E")) { 838 | c = new Color(null, 0xbb, 0x00, 0x00); 839 | } 840 | if (level.equals("F")) { 841 | c = new Color(null, 0xaa, 0x00, 0x00); 842 | } 843 | return c; 844 | } 845 | 846 | private List getLogSource() { 847 | return mLogs; 848 | } 849 | 850 | private String getTitle() { 851 | StringBuilder sb = new StringBuilder(); 852 | sb.append("Android Log Viewer By lrscp"); 853 | if (mCurFile != null) { 854 | sb.append(" -- "); 855 | sb.append(mCurFile.getAbsolutePath()); 856 | } 857 | return sb.toString(); 858 | } 859 | 860 | void loadFile(String path) { 861 | try { 862 | long mStartTime = System.currentTimeMillis(); 863 | // parse lines 864 | mFileLogs.clear(); 865 | List lines = FileUtils.readLines(new File(path), "utf-8"); 866 | int index = 0; 867 | for (String line : lines) { 868 | if (line.isEmpty()) { 869 | continue; 870 | } 871 | LogInfo info = new LogInfo(line, index); 872 | mFileLogs.add(info); 873 | index++; 874 | } 875 | Log.d(TAG, "load file time " + (System.currentTimeMillis() - mStartTime)); 876 | 877 | setLogSource(mFileLogs); 878 | // show log items 879 | loadTableByCurrentFilter(); 880 | } catch (IOException e) { 881 | e.printStackTrace(); 882 | } finally {} 883 | } 884 | 885 | /** 886 | * Clear the table item and load log items filtered by the current filter. 887 | * 888 | * @param filter 889 | */ 890 | public void loadTable(LogFilter filter) { 891 | table.removeAll(); 892 | mFilteredLogs.clear(); 893 | for (final LogInfo log : getLogSource()) { 894 | if (!filter.consume(log, isShowExtra)) { 895 | mFilteredLogs.add(log); 896 | } 897 | } 898 | // parse time closeness 899 | // for (LogInfo log : mFilteredLogs) { 900 | // long time = log.timeMillis; 901 | // // TODO 902 | // } 903 | table.setItemCount(mFilteredLogs.size()); 904 | table.removeListener(SWT.SetData, mLsnr); 905 | table.addListener(SWT.SetData, mLsnr); 906 | // for (LogInfo log : mFilteredLogs) { 907 | // Log.d(TAG, log); 908 | // } 909 | } 910 | 911 | // show the table according to current filter setting 912 | protected void loadTableByCurrentFilter() { 913 | loadTable(getCurLogFilter()); 914 | } 915 | 916 | protected void onSelectClient(int index) { 917 | Log.d(TAG, "onSelectClient " + index); 918 | 919 | mCurClient = mClients.get(index); 920 | 921 | // stop receiving new logs until log listener is set. 922 | mCurClient.pause(); 923 | 924 | // reset LogListener 925 | for (LogClient c : mClients) { 926 | c.setLogListener(null); 927 | } 928 | 929 | setLogSource(mCurClient.getLogs()); 930 | 931 | loadTableByCurrentFilter(); 932 | 933 | mCurClient.setLogListener(mLogLsnr); 934 | 935 | mCurClient.resume(); 936 | } 937 | 938 | private void scrollToBottomIfNeeded() { 939 | if (mScrollEnabled) { 940 | int index = mFilteredLogs.size() - 1; 941 | table.setTopIndex(index); 942 | } 943 | } 944 | 945 | private void scrollToItem(LogInfo log) { 946 | if (getCurLogFilter().consume(log, isShowExtra)) { 947 | // item is not showing in the table 948 | showMsg("The log is not found!"); 949 | return; 950 | } 951 | 952 | int index = 0; 953 | for (LogInfo logCur : mFilteredLogs) { 954 | Log.d(TAG, "logCur " + logCur + " log " + log); 955 | if (logCur.id == log.id) { 956 | // table.showItem(ti); 957 | table.select(index); 958 | table.setTopIndex(index); 959 | return; 960 | } 961 | index++; 962 | } 963 | 964 | } 965 | 966 | protected void setFile(String path) { 967 | Log.d(TAG, "set file " + path); 968 | Preference.setString("path", path); 969 | mCurFile = new File(path); 970 | setText(getTitle()); 971 | loadFile(path); 972 | } 973 | 974 | /** 975 | * Change log source, the source can be socket clients or file lines 976 | * 977 | * @param logs 978 | */ 979 | private void setLogSource(List logs) { 980 | mLogs = logs; 981 | } 982 | 983 | protected void showLog(TableItem ti, int index, LogInfo info) { 984 | ti.setForeground(getLevelColor(info.logLevel)); 985 | ti.setText(new String[] { "" + index, info.logLevel, info.time, info.pid, info.tid, info.tag, info.content }); 986 | ti.setData(info); 987 | } 988 | 989 | private void showMsg(String text) { 990 | MessageBox msg = new MessageBox(getShell()); 991 | msg.setMessage(text); 992 | msg.open(); 993 | } 994 | 995 | public void updateClientsView() { 996 | getDisplay().asyncExec(new Runnable() { 997 | @Override 998 | public void run() { 999 | // Log.d(TAG, "updateClientsView"); 1000 | int sel = list.getSelectionIndex(); 1001 | list.removeAll(); 1002 | for (LogClient c : mClients) { 1003 | String name = null; 1004 | if (c.getName().isEmpty()) { 1005 | name = "?"; 1006 | } else { 1007 | name = c.getName(); 1008 | } 1009 | name += "(" + c.getLogs().size() + ")"; 1010 | list.add(name); 1011 | } 1012 | if (sel < list.getItemCount()) { 1013 | list.select(sel); 1014 | } else { 1015 | list.select(0); 1016 | } 1017 | } 1018 | }); 1019 | } 1020 | 1021 | /** 1022 | * Scroll the table to the bottom when a new item is added. 1023 | * 1024 | * @param log 1025 | */ 1026 | private void onTableLogAdded(LogInfo log) { 1027 | LogFilter filter = getCurLogFilter(); 1028 | if (!filter.consume(log, isShowExtra)) { 1029 | mFilteredLogs.add(log); 1030 | } 1031 | table.setItemCount(mFilteredLogs.size()); 1032 | scrollToBottomIfNeeded(); 1033 | } 1034 | } 1035 | -------------------------------------------------------------------------------- /src/FilterConfig.java: -------------------------------------------------------------------------------- 1 | public class FilterConfig { 2 | String name = ""; 3 | String tag = ""; 4 | } -------------------------------------------------------------------------------- /src/FilterEditDialog.java: -------------------------------------------------------------------------------- 1 | import lrscp.lib.swt.SwtUtils; 2 | 3 | import org.eclipse.swt.SWT; 4 | import org.eclipse.swt.widgets.Button; 5 | import org.eclipse.swt.widgets.Dialog; 6 | import org.eclipse.swt.widgets.Display; 7 | import org.eclipse.swt.widgets.Label; 8 | import org.eclipse.swt.widgets.Shell; 9 | import org.eclipse.swt.widgets.Text; 10 | import org.eclipse.swt.events.SelectionAdapter; 11 | import org.eclipse.swt.events.SelectionEvent; 12 | 13 | public class FilterEditDialog extends Dialog { 14 | 15 | protected Object result; 16 | protected Shell shell; 17 | private Text text; 18 | private Text textTag; 19 | 20 | private FilterConfig mFilter; 21 | 22 | /** 23 | * Create the dialog. 24 | * 25 | * @param parent 26 | * @param style 27 | */ 28 | public FilterEditDialog(Shell parent, int style) { 29 | super(parent, SWT.DIALOG_TRIM); 30 | setText("SWT Dialog"); 31 | } 32 | 33 | /** 34 | * Open the dialog. 35 | * 36 | * @return the result 37 | */ 38 | public Object open() { 39 | createContents(); 40 | shell.open(); 41 | shell.layout(); 42 | Display display = getParent().getDisplay(); 43 | while (!shell.isDisposed()) { 44 | if (!display.readAndDispatch()) { 45 | display.sleep(); 46 | } 47 | } 48 | return result; 49 | } 50 | 51 | /** 52 | * Create contents of the dialog. 53 | */ 54 | private void createContents() { 55 | shell = new Shell(getParent(), getStyle()); 56 | shell.setSize(271, 212); 57 | shell.setText("过滤器"); 58 | shell.setLayout(null); 59 | 60 | Label lblTag = new Label(shell, SWT.NONE); 61 | lblTag.setBounds(23, 33, 38, 20); 62 | lblTag.setText("名称"); 63 | 64 | text = new Text(shell, SWT.BORDER); 65 | text.setBounds(65, 30, 172, 26); 66 | if (mFilter != null) { 67 | text.setText(mFilter.name); 68 | } 69 | 70 | Button button = new Button(shell, SWT.NONE); 71 | button.addSelectionListener(new SelectionAdapter() { 72 | @Override 73 | public void widgetSelected(SelectionEvent e) { 74 | onClickOk(); 75 | } 76 | }); 77 | button.setBounds(23, 140, 92, 30); 78 | button.setText("确定"); 79 | 80 | Button button_1 = new Button(shell, SWT.NONE); 81 | button_1.addSelectionListener(new SelectionAdapter() { 82 | @Override 83 | public void widgetSelected(SelectionEvent e) { 84 | shell.dispose(); 85 | } 86 | }); 87 | button_1.setBounds(145, 140, 92, 30); 88 | button_1.setText("取消"); 89 | 90 | SwtUtils.center(shell, 10); 91 | 92 | Label label = new Label(shell, SWT.NONE); 93 | label.setText("TAG"); 94 | label.setBounds(23, 82, 38, 20); 95 | 96 | textTag = new Text(shell, SWT.BORDER); 97 | textTag.setBounds(65, 79, 172, 26); 98 | if (mFilter != null) { 99 | textTag.setText(mFilter.tag); 100 | } 101 | } 102 | 103 | protected void onClickOk() { 104 | if (text.getText().isEmpty() || textTag.getText().isEmpty()) { 105 | return; 106 | } 107 | String[] r = new String[2]; 108 | r[0] = text.getText(); 109 | r[1] = textTag.getText(); 110 | result = r; 111 | shell.dispose(); 112 | } 113 | 114 | public void setFilterConfig(FilterConfig f) { 115 | mFilter = f; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/LogFilter.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | import lrscp.lib.Log; 5 | 6 | public class LogFilter { 7 | private static final String TAG = LogFilter.class.getSimpleName(); 8 | 9 | private String mLogLevel; 10 | private String mRegx; 11 | private String tag; 12 | private String pid; 13 | private List keywords = new ArrayList(); 14 | 15 | public LogFilter(String logLevel, String regx) { 16 | mLogLevel = logLevel; 17 | mRegx = regx; 18 | String[] segs = mRegx.split(" "); 19 | for (String s : segs) { 20 | if (s.startsWith("tag:")) { 21 | tag = s.substring(4); 22 | } else if (s.startsWith("pid:")) { 23 | pid = s.substring(4); 24 | } else { 25 | keywords.add(s); 26 | } 27 | } 28 | // Log.d(TAG, "tag=" + tag); 29 | } 30 | 31 | public boolean consume(LogInfo log, boolean isShowExtra) { 32 | try { 33 | if (log.tag.contains("DEBUG") || log.tag.contains("libc")) { 34 | return false; 35 | } 36 | 37 | if (!isShowExtra && (log.tag == null || log.tag.isEmpty())) { 38 | return true; 39 | } 40 | 41 | if (pid != null && !log.pid.equals(pid)) { 42 | return true; 43 | } 44 | 45 | if (LogInfo.compareLevel(log.logLevel, mLogLevel) < 0) { 46 | return true; 47 | } 48 | 49 | if (tag != null && !log.tag.toLowerCase().contains(tag.toLowerCase())) { 50 | return true; 51 | } 52 | 53 | for (String keyword : keywords) { 54 | if (!log.content.toLowerCase().contains(keyword.toLowerCase())) { 55 | return true; 56 | } 57 | } 58 | } catch (Exception e) { 59 | // Log.e("compare log level error:" + mLogLevel); 60 | } 61 | 62 | return false; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/LogInfo.java: -------------------------------------------------------------------------------- 1 | import java.util.Calendar; 2 | import java.util.HashMap; 3 | import java.util.Map; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | import lrscp.lib.Log; 8 | 9 | public class LogInfo { 10 | // private static Pattern pat = Pattern.compile( 11 | // "^\\[\\s(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+)" 12 | // + "\\s+(\\d*):\\s*(\\S+)\\s([VDIWEAF])/(.*)\\]$"); 13 | private static Pattern PAT1 = Pattern.compile("(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+)\\s+(\\d*)\\s*(\\S+)\\s([VDIWEAF])\\s([^:]+):(.+)"); 14 | private static Pattern PAT_TIME_MILLIS = Pattern.compile("()"); 15 | private static Pattern PAT2 = Pattern.compile("([VDIWEAF])/([^:]+):(.*)"); 16 | private static Pattern PAT3 = Pattern.compile("(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+): ([VDIWEAF])/([^\\(]+)\\((\\d+\\)):(.+)"); 17 | private static Pattern PAT4 = Pattern.compile("(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+)\\s([VDIWEAF])/([^\\(]+)\\(\\s*(\\d+)\\):(.+)"); 18 | private static Pattern PAT5 = Pattern.compile("(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d):\\s([VDIWEAF])/([^\\(]+)\\(\\s*(\\d+)\\):(.+)"); 19 | public static Pattern[] pats = new Pattern[] { PAT1, PAT2, PAT3, PAT4, PAT5 }; 20 | 21 | public String time = ""; 22 | public String pid = ""; 23 | public String tid = ""; 24 | public String logLevel = ""; 25 | public String tag = ""; 26 | public String content = ""; 27 | public String originalLine = ""; 28 | public int id = 0; 29 | public int timeGroup; 30 | public long timeMillis = 0; 31 | 32 | public static final Map levelMap = new HashMap(); 33 | public static String[] levelStrings = new String[] { "V", "D", "I", "W", "E", "F" }; 34 | static { 35 | levelMap.put("V", 0); 36 | levelMap.put("D", 1); 37 | levelMap.put("I", 2); 38 | levelMap.put("W", 3); 39 | levelMap.put("E", 4); 40 | levelMap.put("F", 4); 41 | } 42 | 43 | public LogInfo(String line, int index) { 44 | if (line == null || line.isEmpty()) { 45 | throw new IllegalArgumentException("line is empty"); 46 | } 47 | originalLine = line; 48 | id = index; 49 | parse(line); 50 | } 51 | 52 | private long getTime(String time) { 53 | Matcher m = PAT_TIME_MILLIS.matcher(time); 54 | if (m.matches()) { 55 | int month = Integer.valueOf(m.group(1)); 56 | int date = Integer.valueOf(m.group(2)); 57 | int hourOfDay = Integer.valueOf(m.group(3)); 58 | int minute = Integer.valueOf(m.group(4)); 59 | int second = Integer.valueOf(m.group(5)); 60 | int millis = Integer.valueOf(m.group(6)); 61 | Calendar cal = Calendar.getInstance(); 62 | cal.set(2000, month, date, hourOfDay, minute, second); 63 | return cal.getTimeInMillis() / 1000 * 1000 + millis; 64 | } 65 | return 0; 66 | } 67 | 68 | private void parse(String line) { 69 | for (int i = 0; i < pats.length; i++) { 70 | Pattern pat = pats[i]; 71 | Matcher m = pat.matcher(line); 72 | if (!m.matches()) { 73 | continue; 74 | } 75 | if (pats[0] != pat) { 76 | // swap 77 | Pattern pTmp = pats[0]; 78 | pats[0] = pat; 79 | pats[i] = pTmp; 80 | } 81 | if (pat == PAT1) { 82 | time = m.group(1); 83 | timeMillis = getTime(time); 84 | pid = m.group(2); 85 | tid = m.group(3); 86 | logLevel = m.group(4); 87 | checkLogLevel(); 88 | tag = m.group(5).trim(); 89 | content = m.group(6); 90 | 91 | /* 92 | * LogLevel doesn't support messages with severity "F". 93 | * Log.wtf() is supposed to generate "A", but generates "F". 94 | */ 95 | if (logLevel == null && m.group(4).equals("F")) { 96 | logLevel = "F"; 97 | } 98 | } else if (pat == PAT2) { 99 | time = ""; 100 | timeMillis = 0; 101 | pid = ""; 102 | tid = ""; 103 | logLevel = m.group(1); 104 | checkLogLevel(); 105 | tag = m.group(2).trim(); 106 | content = m.group(3); 107 | } else if (pat == PAT3) { 108 | time = m.group(1); 109 | timeMillis = getTime(time); 110 | pid = m.group(4); 111 | logLevel = m.group(2); 112 | checkLogLevel(); 113 | tag = m.group(3).trim(); 114 | content = m.group(5); 115 | 116 | /* 117 | * LogLevel doesn't support messages with severity "F". 118 | * Log.wtf() is supposed to generate "A", but generates "F". 119 | */ 120 | if (logLevel == null && m.group(2).equals("F")) { 121 | logLevel = "F"; 122 | } 123 | } else if (pat == PAT4) { 124 | time = m.group(1); 125 | timeMillis = getTime(time); 126 | pid = m.group(4); 127 | logLevel = m.group(2); 128 | checkLogLevel(); 129 | tag = m.group(3).trim(); 130 | content = m.group(5); 131 | 132 | /* 133 | * LogLevel doesn't support messages with severity "F". 134 | * Log.wtf() is supposed to generate "A", but generates "F". 135 | */ 136 | if (logLevel == null && m.group(2).equals("F")) { 137 | logLevel = "F"; 138 | } 139 | } else if (pat == PAT5) { 140 | time = m.group(1); 141 | timeMillis = getTime(time); 142 | pid = m.group(4); 143 | logLevel = m.group(2); 144 | checkLogLevel(); 145 | tag = m.group(3).trim(); 146 | content = m.group(5); 147 | 148 | /* 149 | * LogLevel doesn't support messages with severity "F". 150 | * Log.wtf() is supposed to generate "A", but generates "F". 151 | */ 152 | if (logLevel == null && m.group(2).equals("F")) { 153 | logLevel = "F"; 154 | } 155 | } 156 | return; 157 | } 158 | 159 | content = line; 160 | } 161 | 162 | private void checkLogLevel() { 163 | if (!levelMap.containsKey(logLevel)) { 164 | Log.e("!levelMap.containsKey(logLevel)"); 165 | } 166 | } 167 | 168 | @Override 169 | public String toString() { 170 | return String.format("mCurTime=%s,mCurPid=%s,mCurTid=%s,mCurTag=%s,mCurLogLevel=%s,msg=%s", time, pid, tid, tag, logLevel, content); 171 | } 172 | 173 | public static int compareLevel(String level1, String level2) { 174 | return levelMap.get(level1) - levelMap.get(level2); 175 | } 176 | } 177 | --------------------------------------------------------------------------------