Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
garryyan committed Jan 3, 2019
2 parents f9eac7d + 0f90ff4 commit 6ab30a7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
9 changes: 8 additions & 1 deletion mars/app/app_logic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#ifdef __APPLE__
#include <TargetConditionals.h>
#include "mars/comm/objc/data_protect_attr.h"
#endif

#include "mars/comm/xlogger/xlogger.h"
Expand Down Expand Up @@ -153,7 +154,13 @@ void SetCallback(Callback* const callback) {

std::string GetAppFilePath() {
xassert2(sg_callback != NULL);
return sg_callback->GetAppFilePath();

std::string path = sg_callback->GetAppFilePath();
#ifdef __APPLE__
setAttrProtectionNone(path.c_str());
#endif

return path;
}

AccountInfo GetAccountInfo() {
Expand Down
15 changes: 9 additions & 6 deletions mars/comm/objc/data_protect_attr.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import <Foundation/Foundation.h>
#endif


// If '_path' is directory, the function has effect on new file but ignores existed file.
bool setAttrProtectionNone(const char* _path) {

#if !TARGET_OS_IPHONE
Expand All @@ -37,11 +37,14 @@ bool setAttrProtectionNone(const char* _path) {
[path release];
return false;
}

NSDictionary* attr = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey];

BOOL ret = [fileManager setAttributes:attr ofItemAtPath:path error:nil];


BOOL ret = YES;
NSDictionary* old_attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL];
NSString* protection = [old_attr valueForKey:NSFileProtectionKey];
if ([protection isEqualToString:NSFileProtectionNone] == NO) {
NSDictionary* attr = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey];
ret = [fileManager setAttributes:attr ofItemAtPath:path error:nil];
}
[path release];

return ret;
Expand Down
39 changes: 26 additions & 13 deletions mars/log/src/appender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
#include "mars/comm/tickcount.h"
#include "mars/comm/verinfo.h"

#ifdef __APPLE__
#include "mars/comm/objc/data_protect_attr.h"
#endif

#include "log_buffer.h"

#define LOG_EXT "xlog"
Expand Down Expand Up @@ -262,11 +266,17 @@ static void __del_timeout_file(const std::string& _log_path) {
for (boost::filesystem::directory_iterator iter(path); iter != end_iter; ++iter) {
time_t file_modify_time = boost::filesystem::last_write_time(iter->path());

if (now_time > file_modify_time && now_time - file_modify_time > kMaxLogAliveTime
&& boost::filesystem::is_regular_file(iter->status())
&& iter->path().extension()==(std::string(".") + LOG_EXT)) {

boost::filesystem::remove(iter->path());
if (now_time > file_modify_time && now_time - file_modify_time > kMaxLogAliveTime) {
if(boost::filesystem::is_regular_file(iter->status())
&& iter->path().extension() == (std::string(".") + LOG_EXT)) {
boost::filesystem::remove(iter->path());
}
if (boost::filesystem::is_directory(iter->status())) {
std::string filename = iter->path().filename().c_str();
if (filename.size() == 8 && filename.find_first_not_of("0123456789") == std::string::npos) {
boost::filesystem::remove_all(iter->path());
}
}
}
}
}
Expand Down Expand Up @@ -821,14 +831,16 @@ void appender_open(TAppenderMode _mode, const char* _dir, const char* _nameprefi
boost::filesystem::create_directories(_dir);
tickcount_t tick;
tick.gettickcount();
__del_timeout_file(_dir);

tickcountdiff_t del_timeout_file_time = tickcount_t().gettickcount() - tick;
Thread(boost::bind(&__del_timeout_file, _dir)).start_after(2 * 60 * 1000);

tick.gettickcount();

#ifdef __APPLE__
setAttrProtectionNone(_dir);
#endif

char mmap_file_path[512] = {0};
snprintf(mmap_file_path, sizeof(mmap_file_path), "%s/%s.mmap2", sg_cache_logdir.empty()?_dir:sg_cache_logdir.c_str(), _nameprefix);
snprintf(mmap_file_path, sizeof(mmap_file_path), "%s/%s.mmap3", sg_cache_logdir.empty()?_dir:sg_cache_logdir.c_str(), _nameprefix);

bool use_mmap = false;
if (OpenMmapFile(mmap_file_path, kBufferBlockLength, sg_mmmap_file)) {
Expand Down Expand Up @@ -872,9 +884,6 @@ void appender_open(TAppenderMode _mode, const char* _dir, const char* _nameprefi

xlogger_appender(NULL, appender_info);
char logmsg[256] = {0};
snprintf(logmsg, sizeof(logmsg), "del time out files time: %" PRIu64, (int64_t)del_timeout_file_time);
xlogger_appender(NULL, logmsg);

snprintf(logmsg, sizeof(logmsg), "get mmap time: %" PRIu64, (int64_t)get_mmap_time);
xlogger_appender(NULL, logmsg);

Expand Down Expand Up @@ -913,11 +922,15 @@ void appender_open_with_cache(TAppenderMode _mode, const std::string& _cachedir,
if (!_cachedir.empty()) {
sg_cache_logdir = _cachedir;
boost::filesystem::create_directories(_cachedir);
__del_timeout_file(_cachedir);

Thread(boost::bind(&__del_timeout_file, _cachedir)).start_after(2 * 60 * 1000);
// "_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);
}

#ifdef __APPLE__
setAttrProtectionNone(_cachedir.c_str());
#endif
appender_open(_mode, _logdir.c_str(), _nameprefix, _pub_key);

}
Expand Down
2 changes: 2 additions & 0 deletions samples/iOS/iOSDemoXlog/iOSDemoXlog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Kernel",
Expand All @@ -476,6 +477,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Kernel",
Expand Down

0 comments on commit 6ab30a7

Please sign in to comment.