在android中记录磁盘,如循环或环形缓冲区

时间:2016-01-06 23:30:18

标签: java android

我需要能够将日志写入磁盘,以便出于调试目的,它可以由用户发送给我。如果我在android中使用Log类,它似乎只能写入logcat并且logcat不是那么有用,因为相关日志会在一段时间后消失。所以对我来说重要的是将日志写入磁盘并随时可以恢复。因此,我需要Logcat之类的功能(有限大小的环形缓冲区),但是持久存储到磁盘以在App崩溃和设备重新启动时保持不变。

Android中是否有任何可以帮助我的类

  • 将日志写入磁盘
  • 线程安全,
  • 并在磁盘上实现某种环形缓冲区(这样日志文件不能超过预定义的大小并且总是有最新的日志)

它的性能也应该与现有Logcat实现的默认环形缓冲区大小相似(在高端设备上为64kB到1MB)

我真的不想重新发明轮子,我很乐意使用第三方库,如果必须的话,请指教。

否则,如何使用现有的Android框架API和类库实现这一点?

1 个答案:

答案 0 :(得分:-1)

看看这个课程DiscourseLogger。我们在很多项目中使用过。你需要修改它,但这是一个很好的起点。

    /*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email.mail.transport;

import com.android.emailcommon.Logging;
import com.android.mail.utils.LogUtils;

import java.util.ArrayList;

/**
 * A class to keep last N of lines sent to the server and responses received from the server.
 * They are sent to logcat when {@link #logLastDiscourse} is called.
 *
 * <p>This class is used to log the recent network activities when a response parser crashes.
 */
public class DiscourseLogger {
    private final int mBufferSize;
    private String[] mBuffer;
    private int mPos;
    private final StringBuilder mReceivingLine = new StringBuilder(100);

    public DiscourseLogger(int bufferSize) {
        mBufferSize = bufferSize;
        initBuffer();
    }

    private void initBuffer() {
        mBuffer = new String[mBufferSize];
    }

    /** Add a single line to {@link #mBuffer}. */
    private void addLine(String s) {
        mBuffer[mPos] = s;
        mPos++;
        if (mPos >= mBufferSize) {
            mPos = 0;
        }
    }

    private void addReceivingLineToBuffer() {
        if (mReceivingLine.length() > 0) {
            addLine(mReceivingLine.toString());
            mReceivingLine.delete(0, Integer.MAX_VALUE);
        }
    }

    /**
     * Store a single byte received from the server in {@link #mReceivingLine}.  When LF is
     * received, the content of {@link #mReceivingLine} is added to {@link #mBuffer}.
     */
    public void addReceivedByte(int b) {
        if (0x20 <= b && b <= 0x7e) { // Append only printable ASCII chars.
            mReceivingLine.append((char) b);
        } else if (b == '\n') { // LF
            addReceivingLineToBuffer();
        } else if (b == '\r') { // CR
        } else {
            final String hex = "00" + Integer.toHexString(b);
            mReceivingLine.append("\\x" + hex.substring(hex.length() - 2, hex.length()));
        }
    }

    /** Add a line sent to the server to {@link #mBuffer}. */
    public void addSentCommand(String command) {
        addLine(command);
    }

    /** @return the contents of {@link #mBuffer} as a String array. */
    /* package for testing */ String[] getLines() {
        addReceivingLineToBuffer();

        ArrayList<String> list = new ArrayList<String>();

        final int start = mPos;
        int pos = mPos;
        do {
            String s = mBuffer[pos];
            if (s != null) {
                list.add(s);
            }
            pos = (pos + 1) % mBufferSize;
        } while (pos != start);

        String[] ret = new String[list.size()];
        list.toArray(ret);
        return ret;
    }

    /**
     * Log the contents of the {@link mBuffer}, and clears it out.  (So it's okay to call this
     * method successively more than once.  There will be no duplicate log.)
     */
    public void logLastDiscourse() {
        String[] lines = getLines();
        if (lines.length == 0) {
            return;
        }

        LogUtils.w(Logging.LOG_TAG, "Last network activities:");
        for (String r : getLines()) {
            LogUtils.w(Logging.LOG_TAG, "%s", r);
        }
        initBuffer();
    }
}