Search in sources :

Example 1 with CircularArrayList

use of com.actiontech.dble.util.CircularArrayList in project dble by actiontech.

the class ShowServerLog method showLogSum.

private static PackageBufINf showLogSum(ManagerConnection c, ByteBuffer buffer, byte packetId) {
    PackageBufINf bufINf = new PackageBufINf();
    File[] logFiles = new File(SystemConfig.getHomePath(), "logs").listFiles();
    StringBuilder fileNames = new StringBuilder();
    if (logFiles != null) {
        for (File f : logFiles) {
            if (f.isFile()) {
                fileNames.append("  ");
                fileNames.append(f.getName());
            }
        }
    }
    File file = getLogFile(DEFAULT_LOGFILE);
    BufferedReader br = null;
    int totalLines = 0;
    CircularArrayList<String> queue = new CircularArrayList<>(50);
    try {
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            totalLines++;
            if (queue.size() == queue.capacity()) {
                queue.remove(0);
            }
            queue.add(line);
        }
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode("files in log dir:" + totalLines + fileNames, c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode("Total lines " + totalLines + " ,tail " + queue.size() + " line is following:", c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        int size = queue.size() - 1;
        for (int i = size; i >= 0; i--) {
            String data = queue.get(i);
            row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(data, c.getCharset().getResults()));
            row.setPacketId(++packetId);
            buffer = row.write(buffer, c, true);
        }
        bufINf.setBuffer(buffer);
        bufINf.setPacketId(packetId);
        return bufINf;
    } catch (Exception e) {
        LOGGER.info("showLogSumError", e);
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(e.toString(), c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        bufINf.setBuffer(buffer);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.info("showLogSumError", e);
            }
        }
    }
    bufINf.setPacketId(packetId);
    return bufINf;
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) CircularArrayList(com.actiontech.dble.util.CircularArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)1 CircularArrayList (com.actiontech.dble.util.CircularArrayList)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1