Skip to content

Commit

Permalink
update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
qiulinmin committed Nov 28, 2017
1 parent ec780c4 commit cd277d0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 32 deletions.
8 changes: 6 additions & 2 deletions app/src/main/java/me/pqpo/log4a/LogInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
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;

/**
* Created by pqpo on 2017/11/24.
*/
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() {
Expand All @@ -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);
Expand Down
32 changes: 6 additions & 26 deletions app/src/main/java/me/pqpo/log4a/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -101,7 +100,6 @@ private void performanceTest(int times) {

logger.addAppender(createLog4aFileAppender());
doPerformanceTest("log4a" ,times);
// logger.flush();
Log4a.release();

Log4a.setLogger(logger);
Expand All @@ -110,7 +108,7 @@ private void performanceTest(int times) {
Log4a.release();

Log4a.setLogger(logger);
List<String> buffer = new ArrayList<>();
List<String> buffer = new ArrayList<>(times);
logger.addAppender(createMemAppender(buffer));
doPerformanceTest("array list log" ,times);
buffer.clear();
Expand All @@ -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<String> buffer) {
Expand All @@ -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();
}
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/me/pqpo/log4a/NoBufferFileAppender.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
7 changes: 5 additions & 2 deletions librarylog4a/src/main/cpp/LogBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
LogBuffer::LogBuffer(char *ptr, size_t capacity):
buffer_ptr(ptr),
buffer_capacity(capacity) {
offset = strlen(ptr);
}

LogBuffer::~LogBuffer() {
Expand All @@ -22,7 +23,7 @@ size_t LogBuffer::capacity() {
}

size_t LogBuffer::size() {
return strlen(ptr());
return offset;
}

char *LogBuffer::dataCopy() {
Expand All @@ -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;
}

Expand All @@ -55,6 +57,7 @@ void LogBuffer::async_flush(AsyncFileFlush *fileFlush) {
void LogBuffer::clear() {
std::lock_guard<std::recursive_mutex> lck_clear(log_mtx);
memset(ptr(), '\0', capacity());
offset = 0;
}

void LogBuffer::release() {
Expand Down
3 changes: 1 addition & 2 deletions librarylog4a/src/main/cpp/includes/LogBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};
Expand Down

0 comments on commit cd277d0

Please sign in to comment.