Search in sources :

Example 1 with CircularArrayList

use of io.mycat.util.CircularArrayList in project Mycat-Server by MyCATApache.

the class PackageBufINf method showLogSum.

private static PackageBufINf showLogSum(ManagerConnection c, ByteBuffer buffer, byte packetId) {
    PackageBufINf bufINf = new PackageBufINf();
    File[] logFiles = new File(SystemConfig.getHomePath(), "logs").listFiles();
    String fileNames = "";
    for (File f : logFiles) {
        if (f.isFile()) {
            fileNames += "  " + f.getName();
        }
    }
    File file = getLogFile(DEFAULT_LOGFILE);
    BufferedReader br = null;
    int totalLines = 0;
    CircularArrayList<String> queue = new CircularArrayList<String>(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()));
        row.packetId = ++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()));
        row.packetId = ++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()));
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
        bufINf.buffer = buffer;
        bufINf.packetId = packetId;
        return bufINf;
    } catch (Exception e) {
        LOGGER.error("showLogSumError", e);
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(e.toString(), c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
        bufINf.buffer = buffer;
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.error("showLogSumError", e);
            }
        }
    }
    bufINf.packetId = packetId;
    return bufINf;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) CircularArrayList(io.mycat.util.CircularArrayList) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 2 with CircularArrayList

use of io.mycat.util.CircularArrayList in project Mycat_plus by coderczp.

the class PackageBufINf method showLogSum.

private static PackageBufINf showLogSum(ManagerConnection c, ByteBuffer buffer, byte packetId) {
    PackageBufINf bufINf = new PackageBufINf();
    File[] logFiles = new File(SystemConfig.getHomePath(), "logs").listFiles();
    String fileNames = "";
    for (File f : logFiles) {
        if (f.isFile()) {
            fileNames += "  " + f.getName();
        }
    }
    File file = getLogFile(DEFAULT_LOGFILE);
    BufferedReader br = null;
    int totalLines = 0;
    CircularArrayList<String> queue = new CircularArrayList<String>(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()));
        row.packetId = ++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()));
        row.packetId = ++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()));
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
        bufINf.buffer = buffer;
        bufINf.packetId = packetId;
        return bufINf;
    } catch (Exception e) {
        LOGGER.error("showLogSumError", e);
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(e.toString(), c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
        bufINf.buffer = buffer;
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.error("showLogSumError", e);
            }
        }
    }
    bufINf.packetId = packetId;
    return bufINf;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) CircularArrayList(io.mycat.util.CircularArrayList) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

RowDataPacket (io.mycat.net.mysql.RowDataPacket)2 CircularArrayList (io.mycat.util.CircularArrayList)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2