转载请标明出处: http://blog.youkuaiyun.com/linuxfu/article/details/61915473
前言
看xlog源码的初衷是为了将xlog运用在项目中,因为项目中已经有了一些对于日志的操作逻辑,希望xlog能适应之前的逻辑,使整个日志的替换能平滑过渡,因此需要对xlog作一些修改,例如表现在如下几个方面:
–修改xlog的日志清理操作;
–修改压缩方式;
–修改xlog默认的格式;
–加密方式的增加;
看完下面的代码分析,对于上面几个方面应该可以知道怎么修改。
代码风格
sg_ 开头的变量,static global 全局static
PtrBuffer buff_ 变量名后带_,表示成员变量
bool __Reset(); 函数前面带__,表示成员函数
AutoBuffer& _buff 变量名前带_,表示局部变量
代码分析
我们先从入口开始分析,主要也就这两个native函数,具体的jni接口定义在Java2C_Xlog.cc中
public static native void appenderOpen(int level, int mode, String cacheDir, String logDir, String nameprefix);
public static native void logWrite2(int level, String tag, String filename, String funcname, int line, int pid, long tid, long maintid, String log);
以logWrite2函数为例,主要是调用了xlogger_Write。appenderOpen则是调用了appender_open_with_cache
DEFINE_FIND_STATIC_METHOD(KXlog_logWrite2, KXlog, "logWrite2", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;IIJJLjava/lang/String;)V")
JNIEXPORT void JNICALL Java_com_tencent_mars_xlog_Xlog_logWrite2
(JNIEnv *env, jclass, int _level, jstring _tag, jstring _filename,
jstring _funcname, jint _line, jint _pid, jlong _tid, jlong _maintid, jstring _log) {
if (!xlogger_IsEnabledFor((TLogLevel)_level)) {
return;
}
XLoggerInfo xlog_info;
gettimeofday(&xlog_info.timeval, NULL);
xlog_info.level = (TLogLevel)_level;
xlog_info.line = (int)_line;
xlog_info.pid = (int)_pid;
xlog_info.tid = LONGTHREADID2INT(_tid);
xlog_info.maintid = LONGTHREADID2INT(_maintid);
const char* tag_cstr = NULL;
const char* filename_cstr = NULL;
const char* funcname_cstr = NULL;
const char* log_cstr = NULL;
if (NULL != _tag) {
tag_cstr = env->GetStringUTFChars(_tag, NULL);
}
if (NULL != _filename) {
filename_cstr = env->GetStringUTFChars(_filename, NULL);
}
if (NULL != _funcname) {
funcname_cstr = env->GetStringUTFChars(_funcname, NULL);
}
if (NULL != _log) {
log_cstr = env->GetStringUTFChars(_log, NULL);
}
xlog_info.tag = NULL == tag_cstr ? "" : tag_cstr;
xlog_info.filename = NULL == filename_cstr ? "" : filename_cstr;
xlog_info.func_name = NULL == funcname_cstr ? "" : funcname_cstr;
xlogger_Write(&xlog_info, NULL == log_cstr ? "NULL == log" : log_cstr);
if (NULL != _tag) {
env->ReleaseStringUTFChars(_tag, tag_cstr);
}
if (NULL != _filename) {
env->ReleaseStringUTFChars(_filename, filename_cstr);
}
if (NULL != _funcname) {
env->ReleaseStringUTFChars(_funcname, funcname_cstr);
}
if (NULL != _log) {
env->ReleaseStringUTFChars(_log, log_cstr);
}
}
我们先来分析下appender_open_with_cache,先删除_cachedir里面过期的日志,删除逻辑是根据日志文件的上次修改时间,现在是10天前的日志就删除。注意可能会删除文件夹里面所有文件,这儿做的不够好,应该根据后缀名删除的,更合理些;__move_old_files 将日志文件从_cachedir移到_logdir,这个函数中匹配了prefix和end,只有匹配到的文件才会move过去。
void appender_open_with_cache(TAppenderMode _mode, const std::string& _cachedir, const std::string& _logdir, const char* _nameprefix) {
assert(!_cachedir.empty());
assert(!_logdir.empty());
assert(_nameprefix);
sg_logdir = _logdir;
if (!_cachedir.empty()) {
sg_cache_logdir = _cachedir;
boost::filesystem::create_directories(_cachedir);
__del_timeout_file(_cachedir); 1.删除_cachedir里面过期的日志
// "_nameprefix" must explicitly convert to "std::string", or when the thread is ready to run, "_nameprefix" has been released.
Thread(boost::bind(&__move_old_files, _cachedir, _logdir, std::string(_nameprefix))).start_after(3 * 60 * 1000);//2.开启线程move 老文件
}
appender_open(_mode, _logdir.c_str(), _nameprefix);
}
static void __del_timeout_file(const std::string& _log_path) {
time_t now_time = time(NULL);
boost::filesystem::path path(_log_path);
if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)){
boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator iter(path); iter != end_iter; ++iter) {
time_t fileModifyTime = boost::filesystem::last_write_time(iter->path());
if (now_time > fileModifyTime && now_time - fileModifyTime > kMaxLogAliveTime) {//1.超过kMaxLogAliveTime就删除
if (boost::filesystem::is_regular_file(iter->status())) {
boost::filesystem::remove(iter->path());
}
else if (boost::filesystem::is_directory(iter->status())) {//2.注意可能会删除文件夹里面所有文件
__del_files(iter->pat