Skip to content

Commit

Permalink
Supports modifying the log file path
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Apr 27, 2018
1 parent e26d7b2 commit cb762ac
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/me/pqpo/log4a/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
public class FileUtils {

public static File getLogDir(Context context) {
File log = context.getExternalFilesDir("log");
File log = context.getExternalFilesDir("logs");
if (log == null) {
log = new File(context.getFilesDir(), "log");
log = new File(context.getFilesDir(), "logs");
}
if (!log.exists()) {
log.mkdir();
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/me/pqpo/log4a/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -93,6 +95,13 @@ public void onClick(View view) {
}
});

findViewById(R.id.btn_change_log).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeLogPath();
}
});

}

private void performanceTest(int times) {
Expand Down Expand Up @@ -219,6 +228,23 @@ public String getLogPath() {
return logPath;
}

public void changeLogPath() {
Logger logger = Log4a.getLogger();
if (logger instanceof AppenderLogger) {
List<Appender> appenderList = ((AppenderLogger)logger).getAppenderList();
for (Appender appender : appenderList) {
if (appender instanceof FileAppender) {
FileAppender fileAppender = (FileAppender) appender;
File log = FileUtils.getLogDir(this);
String time = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault()).format(new Date());
String logPath = new File(log, time + "-" + System.currentTimeMillis() + ".txt").getAbsolutePath();
fileAppender.changeLogPath(logPath);
break;
}
}
}
}

@Override
public void onBackPressed() {
super.onBackPressed();
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@

</LinearLayout>

<Button
android:id="@+id/btn_change_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="change log path" />


<TextView
android:layout_width="match_parent"
Expand Down
18 changes: 18 additions & 0 deletions librarylog4a/src/main/cpp/LogBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ size_t LogBuffer::append(const char *log, size_t len) {
return writeSize;
}

void LogBuffer::setAsyncFileFlush(AsyncFileFlush *_fileFlush) {
fileFlush = _fileFlush;
}

void LogBuffer::async_flush() {
async_flush(fileFlush);
}

void LogBuffer::async_flush(AsyncFileFlush *fileFlush) {
if(fileFlush == nullptr) {
return;
}
std::lock_guard<std::recursive_mutex> lck_clear(log_mtx);
if (length() > 0) {
if (is_compress && Z_NULL != zStream.state) {
Expand Down Expand Up @@ -147,6 +158,13 @@ bool LogBuffer::openSetLogFile(const char *log_path) {
return false;
}

void LogBuffer::changeLogPath(char *log_path) {
if(log_file != nullptr) {
async_flush();
}
initData(log_path, strlen(log_path), is_compress);
}




Expand Down
8 changes: 4 additions & 4 deletions librarylog4a/src/main/cpp/LogBufferHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Header* LogBufferHeader::getHeader() {
size_t log_path_len = 0;
memcpy(&log_path_len, data_ptr + sizeof(char) + sizeof(size_t), sizeof(size_t));
header->log_path_len = log_path_len;
char *log_path = new char[log_path_len];
memset(log_path, 0, log_path_len);
char *log_path = new char[log_path_len + 1];
memset(log_path, 0, log_path_len + 1);
memcpy(log_path, data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t), log_path_len);
header->log_path = log_path;
char isCompress = (data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t) + log_path_len)[0];
Expand Down Expand Up @@ -101,8 +101,8 @@ char *LogBufferHeader::getLogPath() {
if (isAvailable()) {
size_t log_path_len = getLogPathLen();
if (log_path_len > 0) {
char *log_path = new char[log_path_len];
memset(log_path, 0, log_path_len);
char *log_path = new char[log_path_len + 1];
memset(log_path, 0, log_path_len + 1);
memcpy(log_path, data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t), log_path_len);
return log_path;
}
Expand Down
5 changes: 4 additions & 1 deletion librarylog4a/src/main/cpp/includes/LogBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class LogBuffer {
void release();
size_t emptySize();
char *getLogPath();
void setAsyncFileFlush(AsyncFileFlush *fileFlush);
void async_flush();
void async_flush(AsyncFileFlush *fileFlush);
void changeLogPath(char *log_path);

public:
bool map_buffer = true;
Expand All @@ -43,7 +46,7 @@ class LogBuffer {
bool openSetLogFile(const char *log_path);

FILE* log_file = nullptr;

AsyncFileFlush *fileFlush = nullptr;
char* const buffer_ptr = nullptr;
char* data_ptr = nullptr;
char* write_ptr = nullptr;
Expand Down
15 changes: 15 additions & 0 deletions librarylog4a/src/main/cpp/log4a-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static jlong initNative(JNIEnv *env, jclass type, jstring buffer_path_,
env->ReleaseStringUTFChars(log_path_, log_path);

LogBuffer* logBuffer = new LogBuffer(buffer_ptr, buffer_size);
logBuffer->setAsyncFileFlush(fileFlush);
//将buffer内的数据清0, 并写入日志文件路径
logBuffer->initData((char *) log_path, strlen(log_path), compress_);
logBuffer->map_buffer = map_buffer;
Expand Down Expand Up @@ -99,6 +100,14 @@ static void releaseNative(JNIEnv *env, jobject instance, jlong ptr) {
fileFlush = nullptr;
}

static void changeLogPathNative(JNIEnv *env, jobject instance, jlong ptr,
jstring logFilePath) {
const char *log_path = env->GetStringUTFChars(logFilePath, 0);
LogBuffer* logBuffer = reinterpret_cast<LogBuffer*>(ptr);
logBuffer->changeLogPath(const_cast<char *>(log_path));
env->ReleaseStringUTFChars(logFilePath, log_path);
}

static void flushAsyncNative(JNIEnv *env, jobject instance, jlong ptr) {
LogBuffer* logBuffer = reinterpret_cast<LogBuffer*>(ptr);
logBuffer->async_flush(fileFlush);
Expand All @@ -124,6 +133,12 @@ static JNINativeMethod gMethods[] = {
(void*)flushAsyncNative
},

{
"changeLogPathNative",
"(JLjava/lang/String;)V",
(void*)changeLogPathNative
},

{
"releaseNative",
"(J)V",
Expand Down
13 changes: 13 additions & 0 deletions librarylog4a/src/main/java/me/pqpo/librarylog4a/LogBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public LogBuffer(String bufferPath, int capacity, String logPath, boolean compre
}
}

public void changeLogPath(String logPath) {
if (ptr != 0) {
try {
changeLogPathNative(ptr, logPath);
this.logPath = logPath;
}catch (Exception e) {
Log.e(TAG, Log4a.getStackTraceString(e));
}
}
}

public boolean isCompress() {
return compress;
}
Expand Down Expand Up @@ -86,4 +97,6 @@ public void release() {

private native void releaseNative(long ptr);

private native void changeLogPathNative(long ptr, String logPath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public String getLogPath() {
return logBuffer.getLogPath();
}

public void changeLogPath(String logPath) {
logBuffer.changeLogPath(logPath);
}

public void setFormatter(Formatter formatter) {
if (formatter != null) {
this.formatter = formatter;
Expand Down

0 comments on commit cb762ac

Please sign in to comment.