Skip to content

Commit

Permalink
remove log file from AsyncFileFlush
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Apr 27, 2018
1 parent c934c5e commit e26d7b2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
9 changes: 5 additions & 4 deletions librarylog4a/src/main/cpp/AsyncFileFlush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "AsyncFileFlush.h"

AsyncFileFlush::AsyncFileFlush(FILE* file):file(file) {
AsyncFileFlush::AsyncFileFlush() {
async_thread = std::thread(&AsyncFileFlush::async_log_thread, this);
}

Expand All @@ -29,9 +29,10 @@ void AsyncFileFlush::async_log_thread() {

ssize_t AsyncFileFlush::flush(FlushBuffer* flushBuffer) {
ssize_t written = 0;
if(file != NULL && flushBuffer->length() > 0) {
written = fwrite(flushBuffer->ptr(), flushBuffer->length(), 1, file);
fflush(file);
FILE* log_file = flushBuffer->logFile();
if(log_file != nullptr && flushBuffer->length() > 0) {
written = fwrite(flushBuffer->ptr(), flushBuffer->length(), 1, log_file);
fflush(log_file);
}
delete flushBuffer;
return written;
Expand Down
6 changes: 5 additions & 1 deletion librarylog4a/src/main/cpp/FlushBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
#include <FlushBuffer.h>

FlushBuffer::FlushBuffer(size_t size) : capacity(size) {}
FlushBuffer::FlushBuffer(FILE* log_file, size_t size) : capacity(size), log_file(log_file) {}

FlushBuffer::~FlushBuffer() {
if (data_ptr != nullptr) {
Expand Down Expand Up @@ -58,4 +58,8 @@ void FlushBuffer::reset() {
}
}

FILE *FlushBuffer::logFile() {
return log_file;
}


20 changes: 19 additions & 1 deletion librarylog4a/src/main/cpp/LogBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ LogBuffer::LogBuffer(char *ptr, size_t buffer_size):
if(logHeader.getIsCompress()) {
initCompress(true);
}
char* log_path = getLogPath();
if(log_path != nullptr) {
openSetLogFile(log_path);
delete[] log_path;
}
}
memset(&zStream, 0, sizeof(zStream));
}
Expand Down Expand Up @@ -66,7 +71,7 @@ void LogBuffer::async_flush(AsyncFileFlush *fileFlush) {
if (is_compress && Z_NULL != zStream.state) {
deflateEnd(&zStream);
}
FlushBuffer* flushBuffer = new FlushBuffer();
FlushBuffer* flushBuffer = new FlushBuffer(log_file);
flushBuffer->write(data_ptr, length());
clear();
fileFlush->async_flush(flushBuffer);
Expand Down Expand Up @@ -112,6 +117,8 @@ void LogBuffer::initData(char *log_path, size_t log_path_len, bool is_compress)

data_ptr = (char *) logHeader.ptr();
write_ptr = (char *) logHeader.write_ptr();

openSetLogFile(log_path);
}

char *LogBuffer::getLogPath() {
Expand All @@ -129,6 +136,17 @@ bool LogBuffer::initCompress(bool compress) {
return false;
}

bool LogBuffer::openSetLogFile(const char *log_path) {
if (log_path != nullptr) {
FILE* _file_log = fopen(log_path, "ab+");
if(_file_log != NULL) {
log_file = _file_log;
return true;
}
}
return false;
}




Expand Down
3 changes: 1 addition & 2 deletions librarylog4a/src/main/cpp/includes/AsyncFileFlush.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class AsyncFileFlush {

public:
AsyncFileFlush(FILE* file);
AsyncFileFlush();
~AsyncFileFlush();
bool async_flush(FlushBuffer* flushBuffer);
void stopFlush();
Expand All @@ -26,7 +26,6 @@ class AsyncFileFlush {
ssize_t flush(FlushBuffer* flushBuffer);

bool exit = false;
FILE* file;
std::vector<FlushBuffer*> async_buffer;
std::thread async_thread;
std::condition_variable async_condition;
Expand Down
6 changes: 5 additions & 1 deletion librarylog4a/src/main/cpp/includes/FlushBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
#include <sys/types.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

class FlushBuffer {

public:
FlushBuffer(size_t size = 128);
FlushBuffer(FILE* log_file, size_t size = 128);
~FlushBuffer();
void write(void* data, size_t len);
void reset();
size_t length();
void* ptr();
FILE* logFile();

private:
FILE* log_file = nullptr;

char* data_ptr = nullptr;
char* write_ptr = nullptr;
size_t capacity;
Expand Down
3 changes: 3 additions & 0 deletions librarylog4a/src/main/cpp/includes/LogBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class LogBuffer {
void clear();
void setLength(size_t len);
bool initCompress(bool compress);
bool openSetLogFile(const char *log_path);

FILE* log_file = nullptr;

char* const buffer_ptr = nullptr;
char* data_ptr = nullptr;
Expand Down
14 changes: 3 additions & 11 deletions librarylog4a/src/main/cpp/log4a-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ static jlong initNative(JNIEnv *env, jclass type, jstring buffer_path_,
const char *log_path = env->GetStringUTFChars(log_path_, 0);
size_t buffer_size = static_cast<size_t>(capacity);
int buffer_fd = open(buffer_path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
FILE* log_fd = fopen(log_path, "ab+");
// buffer 的第一个字节会用于存储日志路径名称长度,后面紧跟日志路径,之后才是日志信息
if (fileFlush == nullptr) {
fileFlush = new AsyncFileFlush(log_fd);
fileFlush = new AsyncFileFlush();
}
// 加上头占用的大小
buffer_size = buffer_size + LogBufferHeader::calculateHeaderLen(strlen(log_path));
Expand All @@ -36,6 +35,7 @@ static jlong initNative(JNIEnv *env, jclass type, jstring buffer_path_,
}
env->ReleaseStringUTFChars(buffer_path_, buffer_path);
env->ReleaseStringUTFChars(log_path_, log_path);

LogBuffer* logBuffer = new LogBuffer(buffer_ptr, buffer_size);
//将buffer内的数据清0, 并写入日志文件路径
logBuffer->initData((char *) log_path, strlen(log_path), compress_);
Expand Down Expand Up @@ -69,15 +69,7 @@ static void writeDirtyLogToFile(int buffer_fd) {
LogBuffer tmp(buffer_ptr_tmp, buffered_size);
size_t data_size = tmp.length();
if (data_size > 0) {
char* log_path = tmp.getLogPath();
if (log_path != nullptr) {
FILE* log_fd = fopen(log_path, "ab+");
if(log_fd != NULL) {
AsyncFileFlush tmpFlush(log_fd);
tmp.async_flush(&tmpFlush);
}
delete[] log_path;
}
tmp.async_flush(fileFlush);
}
}
}
Expand Down

0 comments on commit e26d7b2

Please sign in to comment.