diff --git a/app/src/main/java/me/pqpo/log4a/LogInit.java b/app/src/main/java/me/pqpo/log4a/LogInit.java index c2ad2ff..57a5dcd 100644 --- a/app/src/main/java/me/pqpo/log4a/LogInit.java +++ b/app/src/main/java/me/pqpo/log4a/LogInit.java @@ -10,6 +10,7 @@ import me.pqpo.librarylog4a.Logger; import me.pqpo.librarylog4a.appender.AndroidAppender; import me.pqpo.librarylog4a.appender.FileAppender; +import me.pqpo.librarylog4a.formatter.DateFileFormatter; import me.pqpo.librarylog4a.interceptor.Interceptor; /** @@ -17,6 +18,8 @@ */ public class LogInit { + public static final int BUFFER_SIZE = 1024 * 400; //400k + public static void init(Context context) { int level = Level.DEBUG; Interceptor wrapInterceptor = new Interceptor() { @@ -38,9 +41,10 @@ public boolean intercept(LogData logData) { .setLevel(level) .addInterceptor(wrapInterceptor) .setBufferFilePath(buffer_path) - .setBufferSize(4096); + .setFormatter(new DateFileFormatter()) + .setBufferSize(BUFFER_SIZE); Logger logger = new Logger.Builder() -// .enableAndroidAppender(androidBuild) + .enableAndroidAppender(androidBuild) .enableFileAppender(fileBuild) .create(); Log4a.setLogger(logger); diff --git a/app/src/main/java/me/pqpo/log4a/MainActivity.java b/app/src/main/java/me/pqpo/log4a/MainActivity.java index 0b83629..f459c74 100644 --- a/app/src/main/java/me/pqpo/log4a/MainActivity.java +++ b/app/src/main/java/me/pqpo/log4a/MainActivity.java @@ -9,9 +9,6 @@ import android.widget.Toast; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -24,6 +21,8 @@ import me.pqpo.librarylog4a.appender.Appender; import me.pqpo.librarylog4a.appender.FileAppender; +import static me.pqpo.log4a.LogInit.BUFFER_SIZE; + public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @@ -101,7 +100,6 @@ private void performanceTest(int times) { logger.addAppender(createLog4aFileAppender()); doPerformanceTest("log4a" ,times); -// logger.flush(); Log4a.release(); Log4a.setLogger(logger); @@ -110,7 +108,7 @@ private void performanceTest(int times) { Log4a.release(); Log4a.setLogger(logger); - List buffer = new ArrayList<>(); + List buffer = new ArrayList<>(times); logger.addAppender(createMemAppender(buffer)); doPerformanceTest("array list log" ,times); buffer.clear(); @@ -126,28 +124,9 @@ private void performanceTest(int times) { } private Appender createFileAppender() { - File log = FileUtils.getLogDir(this); - File logFile = new File(log, "logFileTest.txt"); + File logFile = new File(FileUtils.getLogDir(this), "logFileTest.txt"); logFile.delete(); - OutputStream os = null; - try { - logFile.createNewFile(); - os = new FileOutputStream(logFile); - } catch (IOException e) { - e.printStackTrace(); - } - final OutputStream oss = os; - return new AbsAppender() { - @Override - protected void doAppend(int logLevel, String tag, String msg) { - String logStr = String.format("%s/%s: %s\n", Level.getShortLevelName(logLevel), tag, msg); - try { - oss.write(logStr.getBytes()); - } catch (IOException e) { - e.printStackTrace(); - } - } - }; + return new NoBufferFileAppender(logFile); } private Appender createMemAppender(final List buffer) { @@ -167,6 +146,7 @@ private Appender createLog4aFileAppender() { logFile.delete(); FileAppender.Builder fileBuild = new FileAppender.Builder(this) .setLogFilePath(logFile.getAbsolutePath()) + .setBufferSize(BUFFER_SIZE) .setBufferFilePath(cacheFile.getAbsolutePath()); return fileBuild.create(); } diff --git a/app/src/main/java/me/pqpo/log4a/NoBufferFileAppender.java b/app/src/main/java/me/pqpo/log4a/NoBufferFileAppender.java new file mode 100644 index 0000000..9b18095 --- /dev/null +++ b/app/src/main/java/me/pqpo/log4a/NoBufferFileAppender.java @@ -0,0 +1,62 @@ +package me.pqpo.log4a; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import me.pqpo.librarylog4a.Level; +import me.pqpo.librarylog4a.appender.AbsAppender; + +/** + * Created by pqpo on 2017/11/28. + */ +public class NoBufferFileAppender extends AbsAppender { + + private File logFile; + private OutputStream outputStream; + + public NoBufferFileAppender(File logFile) { + this.logFile = logFile; + openFileOutputStream(); + } + + private void openFileOutputStream() { + if (!logFile.exists()) { + try { + if(!logFile.createNewFile()) { + return; + } + outputStream = new FileOutputStream(logFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + protected void doAppend(int logLevel, String tag, String msg) { + if (outputStream == null) { + return; + } + String logStr = String.format("%s/%s: %s\n", Level.getShortLevelName(logLevel), tag, msg); + try { + outputStream.write(logStr.getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void release() { + super.release(); + if (outputStream != null) { + try { + outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/librarylog4a/src/main/cpp/LogBuffer.cpp b/librarylog4a/src/main/cpp/LogBuffer.cpp index 3951113..67b6788 100644 --- a/librarylog4a/src/main/cpp/LogBuffer.cpp +++ b/librarylog4a/src/main/cpp/LogBuffer.cpp @@ -7,6 +7,7 @@ LogBuffer::LogBuffer(char *ptr, size_t capacity): buffer_ptr(ptr), buffer_capacity(capacity) { + offset = strlen(ptr); } LogBuffer::~LogBuffer() { @@ -22,7 +23,7 @@ size_t LogBuffer::capacity() { } size_t LogBuffer::size() { - return strlen(ptr()); + return offset; } char *LogBuffer::dataCopy() { @@ -38,7 +39,8 @@ size_t LogBuffer::append(const char *log) { size_t len = strlen(log); size_t freeSize = emptySize(); size_t writeSize = len <= freeSize ? len : freeSize; - memcpy(ptr() + size(), log, writeSize); + memcpy(ptr() + offset, log, writeSize); + offset += writeSize; return writeSize; } @@ -55,6 +57,7 @@ void LogBuffer::async_flush(AsyncFileFlush *fileFlush) { void LogBuffer::clear() { std::lock_guard lck_clear(log_mtx); memset(ptr(), '\0', capacity()); + offset = 0; } void LogBuffer::release() { diff --git a/librarylog4a/src/main/cpp/includes/LogBuffer.h b/librarylog4a/src/main/cpp/includes/LogBuffer.h index 3218e14..a24b9e2 100644 --- a/librarylog4a/src/main/cpp/includes/LogBuffer.h +++ b/librarylog4a/src/main/cpp/includes/LogBuffer.h @@ -35,10 +35,9 @@ class LogBuffer { bool map_buffer = true; private: + size_t offset = 0; char* buffer_ptr = nullptr; size_t buffer_capacity = 0; - int log_file_fd = -1; - bool exit = false; std::recursive_mutex log_mtx; };