Skip to content

Commit f36ee5d

Browse files
committed
Fixed #10 added WsjcppLog::setEnableLogFile
1 parent dfa9724 commit f36ee5d

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ In main() you need to init logger first.
3232
3333
int main(int argc, char* argv[]) {
3434
std::string TAG = "MAIN";
35-
if (!WsjcppCore::dirExists(".logs")) {
36-
WsjcppCore::makeDir(".logs");
37-
}
3835
WsjcppLog::setLogDirectory(".logs");
3936
WsjcppLog::setPrefixLogFile("app");
40-
37+
// disable log file
38+
// WsjcppLog::setEnableLogFile(false);
4139
// ...
4240
return 0;
4341
}

src/main.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ int main(int argc, char* argv[]) {
88
std::string appName = std::string(WSJCPP_APP_NAME);
99
std::string appVersion = std::string(WSJCPP_APP_VERSION);
1010

11-
if (!WsjcppCore::dirExists(".logs")) {
12-
WsjcppCore::makeDir(".logs");
13-
}
11+
// WsjcppLog::setEnableLogFile(false);
1412
WsjcppLog::setLogDirectory(".logs");
1513
WsjcppLog::setPrefixLogFile("wsjcpp_core");
1614

src/wsjcpp_core.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -746,15 +746,17 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
746746
// ---------------------------------------------------------------------
747747
// WsjcppLog
748748

749-
// Last log messages
750-
std::deque<std::string> * WsjcppLog::g_WSJCPP_LOG_LAST_MESSAGES = nullptr;
751749
std::mutex * WsjcppLog::g_WSJCPP_LOG_MUTEX = nullptr;
752750
std::string WsjcppLog::g_WSJCPP_LOG_DIR = "./";
753751
std::string WsjcppLog::g_WSJCPP_LOG_FILE = "";
754752
std::string WsjcppLog::g_WSJCPP_LOG_PREFIX_FILE = "";
753+
bool WsjcppLog::g_WSJCPP_ENABLE_LOG_FILE = true;
755754
long WsjcppLog::g_WSJCPP_LOG_START_TIME = 0;
756755
long WsjcppLog::g_WSJCPP_LOG_ROTATION_PERIOD_IN_SECONDS = 51000;
757756

757+
// Last log messages
758+
std::deque<std::string> * WsjcppLog::g_WSJCPP_LOG_LAST_MESSAGES = nullptr;
759+
758760
// ---------------------------------------------------------------------
759761

760762
void WsjcppLog::doLogRotateUpdateFilename(bool bForce) {
@@ -820,6 +822,11 @@ std::vector<std::string> WsjcppLog::getLastLogMessages() {
820822

821823
void WsjcppLog::setLogDirectory(const std::string &sDirectoryPath) {
822824
WsjcppLog::g_WSJCPP_LOG_DIR = sDirectoryPath;
825+
if (!WsjcppCore::dirExists(WsjcppLog::g_WSJCPP_LOG_DIR)) {
826+
if (!WsjcppCore::makeDir(WsjcppLog::g_WSJCPP_LOG_DIR)) {
827+
WsjcppLog::err("setLogDirectory", "Could not create log directory '" + sDirectoryPath + "'");
828+
}
829+
}
823830
WsjcppLog::doLogRotateUpdateFilename(true);
824831
}
825832

@@ -832,9 +839,14 @@ void WsjcppLog::setPrefixLogFile(const std::string &sPrefixLogFile) {
832839

833840
// ---------------------------------------------------------------------
834841

842+
void WsjcppLog::setEnableLogFile(bool bEnable) {
843+
WsjcppLog::g_WSJCPP_ENABLE_LOG_FILE = bEnable;
844+
}
845+
846+
// ---------------------------------------------------------------------
847+
835848
void WsjcppLog::setRotationPeriodInSec(long nRotationPeriodInSec) {
836849
WsjcppLog::g_WSJCPP_LOG_ROTATION_PERIOD_IN_SECONDS = nRotationPeriodInSec;
837-
838850
}
839851

840852
// ---------------------------------------------------------------------
@@ -876,15 +888,18 @@ void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const st
876888
while (g_WSJCPP_LOG_LAST_MESSAGES->size() > 50) {
877889
g_WSJCPP_LOG_LAST_MESSAGES->pop_back();
878890
}
879-
// TODO try create global variable
880-
std::ofstream logFile(WsjcppLog::g_WSJCPP_LOG_FILE, std::ios::app);
881-
if (!logFile) {
882-
std::cout << "Error Opening File" << std::endl;
883-
return;
884-
}
885891

886-
logFile << sLogMessage << std::endl;
887-
logFile.close();
892+
// log file
893+
if (WsjcppLog::g_WSJCPP_ENABLE_LOG_FILE) {
894+
std::ofstream logFile(WsjcppLog::g_WSJCPP_LOG_FILE, std::ios::app);
895+
if (!logFile) {
896+
std::cout << "Error Opening File" << std::endl;
897+
return;
898+
}
899+
900+
logFile << sLogMessage << std::endl;
901+
logFile.close();
902+
}
888903
}
889904

890905

src/wsjcpp_core.h

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class WsjcppLog {
107107
static std::string g_WSJCPP_LOG_DIR;
108108
static std::string g_WSJCPP_LOG_PREFIX_FILE;
109109
static std::string g_WSJCPP_LOG_FILE;
110+
static bool g_WSJCPP_ENABLE_LOG_FILE;
110111
static long g_WSJCPP_LOG_START_TIME;
111112
static long g_WSJCPP_LOG_ROTATION_PERIOD_IN_SECONDS;
112113
static std::mutex * g_WSJCPP_LOG_MUTEX;
@@ -121,6 +122,7 @@ class WsjcppLog {
121122
static std::vector<std::string> getLastLogMessages();
122123
static void setLogDirectory(const std::string &sDirectoryPath);
123124
static void setPrefixLogFile(const std::string &sPrefixLogFile);
125+
static void setEnableLogFile(bool bEnable);
124126
static void setRotationPeriodInSec(long nRotationPeriodInSec);
125127
static void initGlobalVariables();
126128
static void deinitGlobalVariables();

0 commit comments

Comments
 (0)