1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.views.hilog; 17 18 import com.intellij.icons.AllIcons; 19 import com.intellij.ui.JBColor; 20 import com.intellij.ui.components.JBLabel; 21 import com.intellij.ui.components.JBLayeredPane; 22 import com.intellij.ui.components.JBPanel; 23 import ohos.devtools.datasources.databases.datatable.LogTable; 24 import ohos.devtools.datasources.transport.hdc.HdcWrapper; 25 import ohos.devtools.datasources.utils.device.entity.DeviceIPPortInfo; 26 import ohos.devtools.datasources.utils.device.service.MultiDeviceManager; 27 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager; 28 import ohos.devtools.datasources.utils.quartzmanager.QuartzManager; 29 import ohos.devtools.views.common.Constant; 30 import ohos.devtools.views.common.LayoutConstants; 31 import ohos.devtools.views.common.UtConstant; 32 import ohos.devtools.views.common.customcomp.CustomTextField; 33 import ohos.devtools.views.layout.WelcomePanel; 34 import ohos.devtools.views.layout.utils.EventTrackUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.logging.log4j.LogManager; 37 import org.apache.logging.log4j.Logger; 38 39 import javax.swing.BorderFactory; 40 import javax.swing.BoundedRangeModel; 41 import javax.swing.DefaultComboBoxModel; 42 import javax.swing.JComboBox; 43 import javax.swing.JPanel; 44 import javax.swing.JScrollBar; 45 import javax.swing.JScrollPane; 46 import javax.swing.JTextArea; 47 import javax.swing.SwingWorker; 48 import javax.swing.event.ChangeEvent; 49 import javax.swing.event.ChangeListener; 50 import javax.swing.event.DocumentEvent; 51 import javax.swing.event.DocumentListener; 52 import java.awt.BorderLayout; 53 import java.awt.Cursor; 54 import java.awt.Dimension; 55 import java.awt.Font; 56 import java.awt.GridLayout; 57 import java.awt.Insets; 58 import java.awt.event.ItemEvent; 59 import java.awt.event.ItemListener; 60 import java.awt.event.MouseAdapter; 61 import java.awt.event.MouseEvent; 62 import java.awt.event.MouseListener; 63 import java.io.BufferedReader; 64 import java.io.IOException; 65 import java.io.InputStream; 66 import java.io.InputStreamReader; 67 import java.nio.charset.Charset; 68 import java.util.ArrayList; 69 import java.util.List; 70 import java.util.Objects; 71 import java.util.Queue; 72 import java.util.Vector; 73 import java.util.concurrent.ExecutionException; 74 import java.util.concurrent.ExecutorService; 75 import java.util.concurrent.LinkedBlockingQueue; 76 import java.util.concurrent.ThreadPoolExecutor; 77 import java.util.concurrent.TimeUnit; 78 79 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_HILOG; 80 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_HILOG_C; 81 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_HILOG_R; 82 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_SHELL_HILOG; 83 import static ohos.devtools.datasources.transport.hdc.HdcStdCmdList.HDC_STD_HILOG_R; 84 import static ohos.devtools.datasources.transport.hdc.HdcStdCmdList.HDC_STD_SHELL_HI_LOG; 85 import static ohos.devtools.datasources.transport.hdc.HdcWrapper.conversionCommand; 86 import static ohos.devtools.datasources.utils.device.entity.DeviceType.LEAN_HOS_DEVICE; 87 import static ohos.devtools.views.common.Constant.IS_SUPPORT_NEW_HDC; 88 89 /** 90 * HiLog Panel 91 * 92 * @since 2021/09/22 12:25 93 */ 94 public class HiLogPanel extends JBLayeredPane implements MouseListener { 95 private static final Logger LOGGER = LogManager.getLogger(HiLogPanel.class); 96 97 /** 98 * A tag that can only appear once for a hilog panel 99 */ 100 private static boolean isOpen; 101 private static final String TAB_STR = "HiLog"; 102 private static final String TAB_CLOSE_STR = "x"; 103 private static final String ERROR = "error"; 104 private static final String SUCCESS = "success"; 105 private static final String SELECT_DEVICE = "Please select device"; 106 private static final String LOG_PANEL_DEVICE_REFRESH = "HiLogPanel device refresh"; 107 108 /** 109 * hilog tab width 110 */ 111 private static final int HILOG_TAB_WIDTH = 60; 112 113 /** 114 * hilog tab width 115 */ 116 private static final int HILOG_JPANEL_WIDTH = 80; 117 118 /** 119 * button margin top 120 */ 121 private static final int BUTTON_MARGIN_TOP = 11; 122 123 /** 124 * wrap margin right 125 */ 126 private static final int WRAP_MARGIN_RIGHT = 95; 127 128 /** 129 * end margin right 130 */ 131 private static final int END_MARGIN_RIGHT = 70; 132 133 /** 134 * clear margin right 135 */ 136 private static final int CLEAR_MARGIN_RIGHT = 45; 137 138 /** 139 * btn width or height 140 */ 141 private static final int BTN_WIDTH_HEIGHT = 20; 142 143 /** 144 * font size 145 */ 146 private static final int FONT_SIZE = 14; 147 148 /** 149 * log rows 150 */ 151 private static final int LOG_ROWS = 26; 152 153 /** 154 * log columns 155 */ 156 private static final int LOG_COLUMNS = 130; 157 158 /** 159 * log y 160 */ 161 private static final int LOG_Y = 40; 162 163 /** 164 * log type x 165 */ 166 private static final int LOG_TYPE_X = 470; 167 168 /** 169 * log margin top 170 */ 171 private static final int LOG_MARGIN_TOP = 100; 172 173 /** 174 * log margin right 175 */ 176 private static final int LOG_MARGIN_RIGHT = 30; 177 178 /** 179 * close process wait time 180 */ 181 private static final int WAIT_TIME = 800; 182 183 /** 184 * insert batch num 185 */ 186 private static final int INSERT_BATCH_NUM = 200; 187 188 /** 189 * log interval 190 */ 191 private static final int LOG_INTERVAL = 30; 192 private static final int SPLIT_LENGTH = 6; 193 private static final int DATE_INDEX = 1; 194 private static final int TIME_INDEX = 2; 195 private static final int PID_INDEX = 3; 196 private static final int TID_INDEX = 4; 197 private static final int LOG_TYPE_INDEX = 5; 198 199 /** 200 * period 201 */ 202 private static final long PERIOD = 1000L; 203 204 private final Queue<HiLogBean> logDataQueue; 205 private boolean rollBottomFlag = true; 206 private boolean deviceChangedFlag; 207 private JBPanel parentPanel; 208 private JBPanel welcomePanel; 209 private JBPanel tabPanel; 210 private JBPanel tabLeftPanel; 211 private JBPanel tabRightPanel; 212 private JBLabel tabText; 213 private JBLabel tabCloseBtn; 214 private JBPanel tabContainer; 215 private JComboBox<String> deviceBox; 216 private JComboBox<String> logTypeBox; 217 private JBLabel wrapBtn; 218 private JBLabel endBtn; 219 private JBLabel clearLogBtn; 220 private StringBuilder wholeBuilder; 221 private Vector<String> oldDevice; 222 private Vector<String> deviceItems; 223 private JTextArea logTextArea; 224 private JScrollPane logScroll; 225 private Process process; 226 private LogTable logTable; 227 228 /** 229 * Secondary interface tab container 230 */ 231 private JPanel optionJPanel; 232 233 /** 234 * Secondary interface tab content container 235 */ 236 private JPanel optionJPanelContent; 237 238 /** 239 * device name drop down box 240 */ 241 private CustomTextField searchTextField; 242 243 /** 244 * HiLog Panel 245 * 246 * @param containerPanel JBPanel 247 * @param welcomePanel WelcomePanel 248 */ HiLogPanel(JBPanel containerPanel, WelcomePanel welcomePanel)249 public HiLogPanel(JBPanel containerPanel, WelcomePanel welcomePanel) { 250 if (ProfilerLogManager.isInfoEnabled()) { 251 LOGGER.info("HiLogPanel"); 252 } 253 EventTrackUtils.getInstance().trackHiLogPage(); 254 parentPanel = containerPanel; 255 this.welcomePanel = welcomePanel; 256 logDataQueue = new LinkedBlockingQueue<>(); 257 initComponents(); 258 // 设置属性 259 setAttributes(containerPanel); 260 initTab(containerPanel); 261 addEventListener(); 262 pollingGetDevices(); 263 isOpen = true; 264 } 265 setAttributes(JBPanel containerPanel)266 private void setAttributes(JBPanel containerPanel) { 267 if (ProfilerLogManager.isInfoEnabled()) { 268 LOGGER.info("setAttributes"); 269 } 270 optionJPanelContent.setLayout(null); 271 optionJPanelContent.setOpaque(true); 272 optionJPanelContent.setBackground(JBColor.background().darker()); 273 deviceBox.setBounds(LayoutConstants.EIGHT_NUM, LayoutConstants.NUM_2, LayoutConstants.HEIGHT_PRESSE, 274 LayoutConstants.DEVICE_PRO_X); 275 deviceBox.setName(UtConstant.UT_HILOG_PANEL_DEVICE); 276 logTypeBox.setBounds(LOG_TYPE_X, LayoutConstants.NUM_2, LayoutConstants.DEVICE_ADD_WIDTH, 277 LayoutConstants.DEVICE_PRO_X); 278 searchTextField.setBounds(LayoutConstants.APP_LABEL_Y2, LayoutConstants.NUM_4, LayoutConstants.HEIGHT_PRESSE, 279 LayoutConstants.TASK_SCENE_Y); 280 wrapBtn.setIcon(AllIcons.Actions.ToggleSoftWrap); 281 wrapBtn.setToolTipText("Soft-wrap"); 282 int width = containerPanel.getWidth(); 283 wrapBtn.setBounds(width - WRAP_MARGIN_RIGHT, BUTTON_MARGIN_TOP, BTN_WIDTH_HEIGHT, BTN_WIDTH_HEIGHT); 284 endBtn.setIcon(AllIcons.RunConfigurations.Scroll_down); 285 endBtn.setToolTipText("Scroll to the end"); 286 endBtn.setBounds(width - END_MARGIN_RIGHT, BUTTON_MARGIN_TOP, BTN_WIDTH_HEIGHT, BTN_WIDTH_HEIGHT); 287 clearLogBtn.setIcon(AllIcons.Actions.GC); 288 clearLogBtn.setToolTipText("Clear Log"); 289 clearLogBtn.setBounds(width - CLEAR_MARGIN_RIGHT, BUTTON_MARGIN_TOP, BTN_WIDTH_HEIGHT, BTN_WIDTH_HEIGHT); 290 } 291 292 /** 293 * initComponents 294 */ initComponents()295 private void initComponents() { 296 if (ProfilerLogManager.isInfoEnabled()) { 297 LOGGER.info("initComponents"); 298 } 299 // init tabPanel 300 tabPanel = new JBPanel(); 301 tabLeftPanel = new JBPanel(); 302 tabLeftPanel.setName(UtConstant.UT_HILOG_PANEL_TITLE_LEFT); 303 tabRightPanel = new JBPanel(); 304 tabRightPanel.setName(UtConstant.UT_HILOG_PANEL_CLOSE_RIGHT); 305 tabText = new JBLabel(TAB_STR); 306 tabCloseBtn = new JBLabel(TAB_CLOSE_STR); 307 // init tab 308 tabContainer = new JBPanel(new BorderLayout()); 309 deviceBox = new JComboBox<String>(); 310 logTypeBox = new JComboBox<String>(); 311 logTypeBox.setName(UtConstant.UT_HILOG_PANEL_LOG_TYPE); 312 wrapBtn = new JBLabel(); 313 endBtn = new JBLabel(); 314 clearLogBtn = new JBLabel(); 315 wholeBuilder = new StringBuilder(); 316 oldDevice = new Vector<>(); 317 deviceItems = new Vector<>(); 318 optionJPanel = new JPanel(new BorderLayout()); 319 optionJPanelContent = new JPanel(); 320 searchTextField = new CustomTextField("press"); 321 logTable = new LogTable(); 322 deviceItems.add("Verbose"); 323 deviceItems.add("Debug"); 324 deviceItems.add("Info"); 325 deviceItems.add("Warn"); 326 deviceItems.add("Error"); 327 deviceItems.add("Fatal"); 328 deviceItems.add("Assert"); 329 logTypeBox.setModel(new DefaultComboBoxModel(deviceItems)); 330 } 331 332 /** 333 * setTabAttributes 334 * 335 * @param containerPanel containerPanel 336 */ initTab(JBPanel containerPanel)337 private void initTab(JBPanel containerPanel) { 338 if (ProfilerLogManager.isInfoEnabled()) { 339 LOGGER.info("initTab"); 340 } 341 tabPanel.setOpaque(false); 342 tabPanel.setPreferredSize(new Dimension(HILOG_JPANEL_WIDTH, LayoutConstants.DEVICES_HEIGHT)); 343 tabLeftPanel.setOpaque(false); 344 tabLeftPanel.setLayout(null); 345 tabLeftPanel.setPreferredSize(new Dimension(HILOG_TAB_WIDTH, LayoutConstants.JP_LEFT_HEIGHT)); 346 tabRightPanel.setOpaque(false); 347 tabRightPanel.setLayout(new GridLayout()); 348 tabRightPanel.setPreferredSize(new Dimension(LayoutConstants.JP_RIGHT_WIDTH, LayoutConstants.JP_RIGHT_HEIGHT)); 349 tabPanel.setLayout(new BorderLayout()); 350 tabPanel.add(tabLeftPanel, BorderLayout.WEST); 351 tabPanel.add(tabRightPanel, BorderLayout.CENTER); 352 tabText.setBounds(0, 0, HILOG_TAB_WIDTH, LayoutConstants.JP_SET_HEIGHT); 353 tabLeftPanel.add(tabText); 354 tabRightPanel.add(tabCloseBtn); 355 tabCloseBtn.setHorizontalAlignment(JBLabel.RIGHT); 356 tabCloseBtn.setName(UtConstant.UT_HILOG_TAB_CLOSE); 357 // Add tab content container to tab container 358 optionJPanel.add(optionJPanelContent); 359 addComponent(); 360 tabContainer.add(optionJPanel); 361 Constant.jtasksTab.addTab("HilogTab", tabContainer); 362 Constant.jtasksTab.setTabComponentAt(Constant.jtasksTab.indexOfComponent(tabContainer), tabPanel); 363 containerPanel.setLayout(new BorderLayout()); 364 Constant.jtasksTab.setBounds(0, 0, containerPanel.getWidth(), containerPanel.getHeight()); 365 this.add(Constant.jtasksTab); 366 containerPanel.add(this); 367 double result = Constant.jtasksTab.getTabCount() * LayoutConstants.NUMBER_X; 368 if (result > containerPanel.getWidth()) { 369 for (int index = 0; index < Constant.jtasksTab.getTabCount(); index++) { 370 Object tabObj = Constant.jtasksTab.getTabComponentAt(index); 371 if (tabObj instanceof JBPanel) { 372 ((JBPanel) tabObj).getComponents()[0].setPreferredSize(new Dimension( 373 (((containerPanel.getWidth() - LayoutConstants.MEMORY_WIDTH) / Constant.jtasksTab.getTabCount()) 374 - LayoutConstants.TASK_DEC_NUM) - LayoutConstants.JAVA_HEIGHT, 375 LayoutConstants.JAVA_HEIGHT)); 376 } 377 Constant.jtasksTab.getTabComponentAt(index).setPreferredSize(new Dimension( 378 ((containerPanel.getWidth() - LayoutConstants.MEMORY_WIDTH) / Constant.jtasksTab.getTabCount()) 379 - LayoutConstants.TASK_DEC_NUM, LayoutConstants.JAVA_HEIGHT)); 380 } 381 } 382 } 383 addComponent()384 private void addComponent() { 385 if (ProfilerLogManager.isInfoEnabled()) { 386 LOGGER.info("addComponent"); 387 } 388 optionJPanelContent.add(deviceBox); 389 optionJPanelContent.add(logTypeBox); 390 optionJPanelContent.add(searchTextField); 391 optionJPanelContent.add(wrapBtn); 392 optionJPanelContent.add(endBtn); 393 optionJPanelContent.add(clearLogBtn); 394 logTextArea = new JTextArea("", LOG_ROWS, LOG_COLUMNS); 395 logTextArea.setMargin(new Insets(30, 40, 0, 0)); 396 logTextArea.setBackground(JBColor.background().darker()); 397 logTextArea.setFont(new Font("PingFang SC", Font.PLAIN, FONT_SIZE)); 398 logTextArea.setEditable(false); 399 // Place the text field in a scrolling window 400 logScroll = new JScrollPane(logTextArea); 401 logScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 402 logScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 403 logScroll.setBounds(LayoutConstants.EIGHT_NUM, LOG_Y, parentPanel.getWidth() - LOG_MARGIN_RIGHT, 404 parentPanel.getHeight() - LOG_MARGIN_TOP); 405 // Add JScrollPane to JPanel container 406 optionJPanelContent.add(logScroll); 407 } 408 409 /** 410 * addEventListener 411 */ addEventListener()412 private void addEventListener() { 413 if (ProfilerLogManager.isInfoEnabled()) { 414 LOGGER.info("addEventListener"); 415 } 416 buttonAddEventListener(); 417 boxAddListener(); 418 logScrollAddListener(); 419 searchTextField.getDocument().addDocumentListener(new DocumentListener() { 420 /** 421 * insertUpdate 422 * 423 * @param event event 424 */ 425 @Override 426 public void insertUpdate(DocumentEvent event) { 427 filterLogs(); 428 } 429 430 /** 431 * removeUpdate 432 * 433 * @param event event 434 */ 435 @Override 436 public void removeUpdate(DocumentEvent event) { 437 filterLogs(); 438 } 439 440 /** 441 * changedUpdate 442 * 443 * @param event event 444 */ 445 @Override 446 public void changedUpdate(DocumentEvent event) { 447 } 448 }); 449 } 450 logScrollAddListener()451 private void logScrollAddListener() { 452 if (ProfilerLogManager.isInfoEnabled()) { 453 LOGGER.info("logScrollAddListener"); 454 } 455 BoundedRangeModel model = logScroll.getVerticalScrollBar().getModel(); 456 model.addChangeListener(new ChangeListener() { 457 @Override 458 public void stateChanged(ChangeEvent event) { 459 if (model.getExtent() + model.getValue() == model.getMaximum()) { 460 rollBottomFlag = true; 461 } else { 462 rollBottomFlag = false; 463 } 464 } 465 }); 466 } 467 boxAddListener()468 private void boxAddListener() { 469 if (ProfilerLogManager.isInfoEnabled()) { 470 LOGGER.info("boxAddListener"); 471 } 472 ItemListener logTypeListener = new ItemListener() { 473 @Override 474 public void itemStateChanged(ItemEvent arg0) { 475 if (arg0.getStateChange() == ItemEvent.SELECTED) { 476 String selectedItem = arg0.getItem().toString(); 477 if (deviceItems.contains(selectedItem)) { 478 filterLogs(); 479 } else { 480 if (!selectedItem.equals(SELECT_DEVICE)) { 481 try { 482 Thread.sleep(WAIT_TIME); 483 } catch (InterruptedException ioException) { 484 if (ProfilerLogManager.isErrorEnabled()) { 485 LOGGER.error("interrupted: ", ioException); 486 } 487 } 488 deviceChangedFlag = false; 489 getHilog(); 490 } 491 } 492 } 493 // When switching devices, close the last selected 494 if (arg0.getStateChange() == ItemEvent.DESELECTED) { 495 String selectedItem = arg0.getItem().toString(); 496 if (!deviceItems.contains(selectedItem) && !selectedItem.equals(SELECT_DEVICE)) { 497 DeviceIPPortInfo desSelectDevice = getSelectDevice(selectedItem); 498 deviceChangedFlag = true; 499 process.destroy(); 500 QuartzManager.getInstance().deleteExecutor(desSelectDevice.getDeviceName()); 501 } 502 } 503 } 504 }; 505 deviceBox.addItemListener(logTypeListener); 506 logTypeBox.addItemListener(logTypeListener); 507 } 508 buttonAddEventListener()509 private void buttonAddEventListener() { 510 if (ProfilerLogManager.isInfoEnabled()) { 511 LOGGER.info("buttonAddEventListener"); 512 } 513 tabCloseBtn.addMouseListener(this); 514 endBtn.addMouseListener(new MouseAdapter() { 515 @Override 516 public void mouseClicked(MouseEvent event) { 517 logTextArea.setFocusable(true); 518 logTextArea.setCaretPosition(logTextArea.getDocument().getLength()); 519 JScrollBar verticalScrollBar = logScroll.getVerticalScrollBar(); 520 verticalScrollBar.setValue(verticalScrollBar.getMaximum()); 521 } 522 523 @Override 524 public void mouseEntered(MouseEvent event) { 525 endBtn.setBorder(BorderFactory.createTitledBorder("")); 526 endBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 527 } 528 529 @Override 530 public void mouseExited(MouseEvent event) { 531 endBtn.setBorder(null); 532 endBtn.setCursor(Cursor.getDefaultCursor()); 533 } 534 }); 535 536 wrapBtn.addMouseListener(new MouseAdapter() { 537 @Override 538 public void mouseClicked(MouseEvent mouseEvent) { 539 logTextArea.setLineWrap(!logTextArea.getLineWrap()); 540 } 541 542 @Override 543 public void mouseEntered(MouseEvent event) { 544 wrapBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 545 wrapBtn.setBorder(BorderFactory.createTitledBorder("")); 546 } 547 548 @Override 549 public void mouseExited(MouseEvent event) { 550 wrapBtn.setCursor(Cursor.getDefaultCursor()); 551 wrapBtn.setBorder(null); 552 } 553 }); 554 555 clearButtonAddEventListener(); 556 } 557 clearButtonAddEventListener()558 private void clearButtonAddEventListener() { 559 if (ProfilerLogManager.isInfoEnabled()) { 560 LOGGER.info("clearButtonAddEventListener"); 561 } 562 clearLogBtn.addMouseListener(new MouseAdapter() { 563 @Override 564 public void mouseClicked(MouseEvent event) { 565 new SwingWorker<String, Void>() { 566 @Override 567 protected String doInBackground() { 568 ArrayList clearCmd = getClearCmd(); 569 if (clearCmd.isEmpty()) { 570 return ""; 571 } 572 HdcWrapper.getInstance().execCmdBy(clearCmd); 573 return SUCCESS; 574 } 575 576 @Override 577 protected void done() { 578 try { 579 get(); 580 } catch (InterruptedException | ExecutionException inException) { 581 if (ProfilerLogManager.isErrorEnabled()) { 582 LOGGER.error("interrupted: ", inException); 583 } 584 } 585 logTextArea.setText(""); 586 wholeBuilder.delete(0, wholeBuilder.length()); 587 } 588 }.execute(); 589 } 590 591 @Override 592 public void mouseEntered(MouseEvent event) { 593 clearLogBtn.setBorder(BorderFactory.createTitledBorder("")); 594 clearLogBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 595 } 596 597 @Override 598 public void mouseExited(MouseEvent event) { 599 clearLogBtn.setBorder(null); 600 clearLogBtn.setCursor(Cursor.getDefaultCursor()); 601 } 602 }); 603 } 604 getClearCmd()605 private ArrayList getClearCmd() { 606 if (ProfilerLogManager.isInfoEnabled()) { 607 LOGGER.info("getClearCmd"); 608 } 609 DeviceIPPortInfo selectDevice = getSelectDevice(deviceBox.getSelectedItem() + ""); 610 ArrayList clearLogCmd = new ArrayList<String>(); 611 if (selectDevice != null) { 612 if (IS_SUPPORT_NEW_HDC && selectDevice.getDeviceType() == LEAN_HOS_DEVICE) { 613 clearLogCmd = conversionCommand(HDC_STD_HILOG_R, selectDevice.getDeviceID()); 614 } else if (selectDevice.getDeviceType() == LEAN_HOS_DEVICE) { 615 clearLogCmd = conversionCommand(HDC_HILOG_R, selectDevice.getDeviceID()); 616 } else { 617 clearLogCmd = conversionCommand(HDC_HILOG_C, selectDevice.getDeviceID()); 618 } 619 } 620 return clearLogCmd; 621 } 622 getHilog()623 private void getHilog() { 624 if (ProfilerLogManager.isInfoEnabled()) { 625 LOGGER.info("getHilog"); 626 } 627 DeviceIPPortInfo selectDevice = getSelectDevice(deviceBox.getSelectedItem() + ""); 628 if (selectDevice == null) { 629 return; 630 } 631 wholeBuilder.delete(0, wholeBuilder.length()); 632 logTextArea.setText(""); 633 QuartzManager.getInstance().addExecutor(selectDevice.getDeviceName(), new Runnable() { 634 @Override 635 public void run() { 636 ArrayList clearLogCmd = new ArrayList<String>(); 637 ArrayList hdcCmd = new ArrayList<String>(); 638 if (IS_SUPPORT_NEW_HDC && selectDevice.getDeviceType() == LEAN_HOS_DEVICE) { 639 clearLogCmd = conversionCommand(HDC_STD_HILOG_R, selectDevice.getDeviceID()); 640 hdcCmd = conversionCommand(HDC_STD_SHELL_HI_LOG, selectDevice.getDeviceID()); 641 } else if (selectDevice.getDeviceType() == LEAN_HOS_DEVICE) { 642 clearLogCmd = conversionCommand(HDC_HILOG_R, selectDevice.getDeviceID()); 643 hdcCmd = conversionCommand(HDC_SHELL_HILOG, selectDevice.getDeviceID()); 644 } else { 645 clearLogCmd = conversionCommand(HDC_HILOG_C, selectDevice.getDeviceID()); 646 hdcCmd = conversionCommand(HDC_HILOG, selectDevice.getDeviceID()); 647 } 648 doLineFilter(clearLogCmd, hdcCmd); 649 } 650 }); 651 QuartzManager.getInstance().startExecutor(selectDevice.getDeviceName(), 0, PERIOD); 652 ExecutorService service = new ThreadPoolExecutor(1, 1, 653 0L, TimeUnit.MILLISECONDS, 654 new LinkedBlockingQueue<Runnable>()); 655 service.submit(() -> { 656 while (true) { 657 if (!isOpen) { 658 logTable.insertLogInfo(logDataQueue); 659 break; 660 } 661 if (logDataQueue.size() > INSERT_BATCH_NUM) { 662 logTable.insertLogInfo(logDataQueue); 663 } 664 } 665 }); 666 service.shutdown(); 667 } 668 669 /** 670 * doLineFilter 671 * 672 * @param clearLogCmd clearLogCmd 673 * @param hdcCmd hdcCmd 674 */ doLineFilter(ArrayList clearLogCmd, ArrayList hdcCmd)675 private void doLineFilter(ArrayList clearLogCmd, ArrayList hdcCmd) { 676 if (ProfilerLogManager.isInfoEnabled()) { 677 LOGGER.info("doLineFilter"); 678 } 679 InputStream inputStream = null; 680 InputStream errorStream = null; 681 BufferedReader brInputStream = null; 682 BufferedReader brErrorStream = null; 683 try { 684 HdcWrapper.getInstance().execCmdBy(clearLogCmd); 685 process = new ProcessBuilder(hdcCmd).start(); 686 // Error command result output stream or Get command result output stream 687 inputStream = process.getInputStream(); 688 errorStream = process.getErrorStream(); 689 brInputStream = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); 690 brErrorStream = new BufferedReader(new InputStreamReader(errorStream)); 691 String line; 692 while ((line = brInputStream.readLine()) != null || (line = brErrorStream.readLine()) != null) { 693 try { 694 Thread.sleep(LOG_INTERVAL); 695 } catch (InterruptedException ioException) { 696 if (ProfilerLogManager.isErrorEnabled()) { 697 LOGGER.error("interrupted: " + ioException.getMessage()); 698 } 699 } 700 if (line.contains("error: device") || line.contains("not found") || deviceChangedFlag || !isOpen) { 701 break; 702 } 703 wholeBuilder.append(line).append(System.lineSeparator()); 704 addLogDataToQueue(line); 705 handleExceedMaxRow(); 706 String selectedItem = Objects.requireNonNull(logTypeBox.getSelectedItem()).toString(); 707 String selectType = selectedItem.substring(0, 1); 708 HiLogFilter.getInstance().lineFilter(line, searchTextField.getText(), selectType, logTextArea); 709 if (rollBottomFlag) { 710 logTextArea.setCaretPosition(logTextArea.getDocument().getLength()); 711 } 712 } 713 } catch (IOException ioException) { 714 if (ProfilerLogManager.isErrorEnabled()) { 715 LOGGER.error("ioException: " + ioException.getMessage()); 716 } 717 } finally { 718 closeIOStream(brErrorStream, null); 719 closeIOStream(brInputStream, null); 720 closeIOStream(null, errorStream); 721 closeIOStream(null, inputStream); 722 } 723 } 724 addLogDataToQueue(String line)725 private void addLogDataToQueue(String line) { 726 if (ProfilerLogManager.isInfoEnabled()) { 727 LOGGER.info("addLogDataToQueue"); 728 } 729 boolean isErrorLog = HiLogFilter.getInstance().isErrorLog(line); 730 HiLogBean hiLogBean = new HiLogBean(); 731 DeviceIPPortInfo selectDevice = getSelectDevice(deviceBox.getSelectedItem() + ""); 732 if (isErrorLog) { 733 hiLogBean.setMessage(line); 734 } else { 735 String[] lines = line.split(" "); 736 int index = 1; 737 if (lines.length >= SPLIT_LENGTH) { 738 StringBuilder builder = new StringBuilder(); 739 for (String str : lines) { 740 if (StringUtils.isEmpty(str) && index < SPLIT_LENGTH) { 741 continue; 742 } else { 743 if (index == DATE_INDEX) { 744 hiLogBean.setDate(str); 745 } else if (index == TIME_INDEX) { 746 hiLogBean.setTime(str); 747 } else if (index == PID_INDEX) { 748 hiLogBean.setPid(str); 749 } else if (index == TID_INDEX) { 750 hiLogBean.setTid(str); 751 } else if (index == LOG_TYPE_INDEX) { 752 hiLogBean.setLogType(str); 753 } else { 754 if (index > lines.length - 1) { 755 break; 756 } 757 builder.append(lines[index]).append(" "); 758 } 759 index++; 760 } 761 } 762 hiLogBean.setMessage(builder.toString().trim()); 763 } 764 } 765 hiLogBean.setDeviceName(selectDevice.getDeviceName()); 766 logDataQueue.offer(hiLogBean); 767 } 768 769 /** 770 * closeIOStream 771 * 772 * @param bufferedReader bufferedReader 773 * @param inputStream inputStream 774 */ closeIOStream(BufferedReader bufferedReader, InputStream inputStream)775 private void closeIOStream(BufferedReader bufferedReader, InputStream inputStream) { 776 if (ProfilerLogManager.isInfoEnabled()) { 777 LOGGER.info("closeIOStream"); 778 } 779 if (bufferedReader != null) { 780 try { 781 bufferedReader.close(); 782 } catch (IOException exception) { 783 LOGGER.error(exception.getMessage()); 784 } 785 } 786 787 if (inputStream != null) { 788 try { 789 inputStream.close(); 790 } catch (IOException exception) { 791 LOGGER.error(exception.getMessage()); 792 } 793 } 794 } 795 handleExceedMaxRow()796 private void handleExceedMaxRow() { 797 if (ProfilerLogManager.isInfoEnabled()) { 798 LOGGER.info("handleExceedMaxRow"); 799 } 800 String[] lines = wholeBuilder.toString().split(System.lineSeparator()); 801 if (lines.length >= HiLogFilter.MAX_ROW_NUM) { 802 wholeBuilder.delete(0, lines[0].length() + HiLogFilter.LINE_BREAK_NUM); 803 } 804 } 805 filterLogs()806 private void filterLogs() { 807 if (ProfilerLogManager.isInfoEnabled()) { 808 LOGGER.info("filterLogs"); 809 } 810 String selectedItem = Objects.requireNonNull(logTypeBox.getSelectedItem()).toString(); 811 String selectType = selectedItem.substring(0, 1); 812 String searchValue = searchTextField.getText(); 813 HiLogFilter.getInstance().filterLog(logTextArea, selectType, searchValue, wholeBuilder); 814 } 815 getSelectDevice(String deviceName)816 private DeviceIPPortInfo getSelectDevice(String deviceName) { 817 if (ProfilerLogManager.isInfoEnabled()) { 818 LOGGER.info("getSelectDevice"); 819 } 820 DeviceIPPortInfo selectDevice = null; 821 List<DeviceIPPortInfo> deviceInfos = MultiDeviceManager.getInstance().getDeviceInfoList(); 822 for (DeviceIPPortInfo deviceInfo : deviceInfos) { 823 if (deviceInfo.getDeviceName().equals(deviceName)) { 824 selectDevice = deviceInfo; 825 } 826 } 827 return selectDevice; 828 } 829 pollingGetDevices()830 private void pollingGetDevices() { 831 if (ProfilerLogManager.isInfoEnabled()) { 832 LOGGER.info("pollingGetDevices"); 833 } 834 QuartzManager.getInstance().addExecutor(LOG_PANEL_DEVICE_REFRESH, new Runnable() { 835 @Override 836 public void run() { 837 List<DeviceIPPortInfo> deviceInfos = MultiDeviceManager.getInstance().getDeviceInfoList(); 838 if (!deviceInfos.isEmpty()) { 839 Vector<String> items = new Vector<>(); 840 items.add(SELECT_DEVICE); 841 deviceInfos.forEach(deviceInfo -> { 842 items.add(deviceInfo.getDeviceName()); 843 }); 844 if (!oldDevice.equals(items)) { 845 oldDevice = items; 846 deviceBox.setModel(new DefaultComboBoxModel(items)); 847 } 848 } else { 849 Vector<String> items = new Vector<>(); 850 items.add("No connected devices"); 851 deviceBox.setModel(new DefaultComboBoxModel(items)); 852 } 853 } 854 }); 855 QuartzManager.getInstance().startExecutor(LOG_PANEL_DEVICE_REFRESH, 0, PERIOD); 856 } 857 858 @Override mouseClicked(MouseEvent mouseEvent)859 public void mouseClicked(MouseEvent mouseEvent) { 860 if (ProfilerLogManager.isInfoEnabled()) { 861 LOGGER.info("mouseClicked"); 862 } 863 String name = mouseEvent.getComponent().getName(); 864 if (name.equals(UtConstant.UT_HILOG_TAB_CLOSE)) { 865 removeAll(); 866 Constant.jtasksTab.remove(Constant.jtasksTab.indexOfTabComponent(tabPanel)); 867 add(Constant.jtasksTab); 868 parentPanel.add(this); 869 parentPanel.repaint(); 870 Constant.jtasksTab.updateUI(); 871 if (Constant.jtasksTab.getTabCount() == 0) { 872 removeAll(); 873 Constant.jtasksTab = null; 874 welcomePanel.setVisible(true); 875 } 876 isOpen = false; 877 QuartzManager.getInstance().deleteExecutor(LOG_PANEL_DEVICE_REFRESH); 878 if (process != null) { 879 process.destroy(); 880 } 881 wholeBuilder.delete(0, wholeBuilder.length()); 882 DeviceIPPortInfo selectDevice = getSelectDevice(deviceBox.getSelectedItem() + ""); 883 if (selectDevice == null) { 884 return; 885 } 886 QuartzManager.getInstance().deleteExecutor(selectDevice.getDeviceName()); 887 } 888 } 889 890 @Override mousePressed(MouseEvent mouseEvent)891 public void mousePressed(MouseEvent mouseEvent) { 892 } 893 894 @Override mouseReleased(MouseEvent mouseEvent)895 public void mouseReleased(MouseEvent mouseEvent) { 896 } 897 898 @Override mouseEntered(MouseEvent mouseEvent)899 public void mouseEntered(MouseEvent mouseEvent) { 900 } 901 902 @Override mouseExited(MouseEvent mouseEvent)903 public void mouseExited(MouseEvent mouseEvent) { 904 } 905 isIsOpen()906 public static boolean isIsOpen() { 907 return isOpen; 908 } 909 setIsOpen(boolean isOpen)910 public static void setIsOpen(boolean isOpen) { 911 HiLogPanel.isOpen = isOpen; 912 } 913 } 914