Skip to content

Commit

Permalink
Supports automatic creation of custom log directories
Browse files Browse the repository at this point in the history
  • Loading branch information
doleyzi committed Nov 23, 2023
1 parent 08a9746 commit 501eea6
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,42 @@
#define INLONG_SDK_LOGGER_H

#include "sdk_conf.h"
#include <iostream>
#include <log4cplus/fileappender.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/initializer.h>
#include <log4cplus/log4cplus.h>
#include <log4cplus/logger.h>
#include <log4cplus/loglevel.h>
#include <sys/stat.h>
using namespace log4cplus::helpers;

namespace inlong {
static const char kDefaultPath[] = "./";
static const char kLogName[] = "/inlong-sdk.log";
static bool CheckPath(const std::string &path) {
struct stat st_stat = {0};
int ret = stat(path.c_str(), &st_stat);
if (ret && errno != ENOENT) {
std::cout << "Check directory error:" << strerror(errno) << std::endl;
return false;
}

if ((ret && errno == ENOENT) || (!ret && !S_ISDIR(st_stat.st_mode))) {
if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
std::cout << "Crate directory error:" << strerror(errno) << std::endl;
return false;
}
}
return true;
}

static void initLog4cplus() {
std::string log_path = SdkConfig::getInstance()->log_path_;
if (log_path.back() != '/') {
log_path = "./inlong-sdk-logs/";
if (!CheckPath(log_path)) {
log_path = kDefaultPath;
}
std::string log_name = log_path + "/inlong-sdk.log";
std::string log_name = log_path + kLogName;
log4cplus::SharedAppenderPtr the_append(new log4cplus::RollingFileAppender(
log_name, SdkConfig::getInstance()->log_size_,
SdkConfig::getInstance()->log_num_));
Expand Down

0 comments on commit 501eea6

Please sign in to comment.