Add filepath and new filename of log file's
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
*.log
|
||||||
|
|||||||
3
main.cpp
3
main.cpp
@@ -3,7 +3,8 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
LoggerClass logger;
|
LoggerClass logger;
|
||||||
logger.logName = "Test";
|
logger.appName = "Test";
|
||||||
|
logger.pathToLogFiles = "/home/jam/local-gitea/CPlusPlus/logging/";
|
||||||
logger.logLevel=0;
|
logger.logLevel=0;
|
||||||
|
|
||||||
logger.debug("YA SMOG STROKU PEREDAT'");
|
logger.debug("YA SMOG STROKU PEREDAT'");
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ enum LogLevels {
|
|||||||
|
|
||||||
class LoggerClass {
|
class LoggerClass {
|
||||||
public:
|
public:
|
||||||
std::string logName;
|
std::string appName;
|
||||||
std::string filepath = "test.log";
|
std::string pathToLogFiles = "/var/log/" + appName;
|
||||||
int logLevel;
|
int logLevel;
|
||||||
|
|
||||||
void debug(const std::string& message) {
|
void debug(const std::string& message) {
|
||||||
action(LogLevels::DEBUG, message);
|
action(LogLevels::DEBUG, message);
|
||||||
}
|
}
|
||||||
@@ -29,16 +29,17 @@ class LoggerClass {
|
|||||||
void warn(const std::string& message) {
|
void warn(const std::string& message) {
|
||||||
action(LogLevels::WARN, message);
|
action(LogLevels::WARN, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void error(const std::string& message) {
|
void error(const std::string& message) {
|
||||||
action(LogLevels::ERROR, message);
|
action(LogLevels::ERROR, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void critical(const std::string& message) {
|
void critical(const std::string& message) {
|
||||||
action(CRITICAL, message);
|
action(CRITICAL, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::string filepath = "";
|
||||||
bool openLogFileFailed = false;
|
bool openLogFileFailed = false;
|
||||||
|
|
||||||
void action(int logLevel, const std::string message) {
|
void action(int logLevel, const std::string message) {
|
||||||
@@ -57,7 +58,7 @@ class LoggerClass {
|
|||||||
case CRITICAL: logLevelStr = std::string("CRITICAL"); break;
|
case CRITICAL: logLevelStr = std::string("CRITICAL"); break;
|
||||||
default: logLevelStr = std::string("UNKNOWN"); break;
|
default: logLevelStr = std::string("UNKNOWN"); break;
|
||||||
};
|
};
|
||||||
line = std::string(str) + " - [" + logLevelStr + "] " + message + "\n";
|
line = appName + " - " + std::string(str) + " - [" + logLevelStr + "] " + message + "\n";
|
||||||
if (logLevel > 2) {
|
if (logLevel > 2) {
|
||||||
print(line);
|
print(line);
|
||||||
}
|
}
|
||||||
@@ -75,6 +76,10 @@ class LoggerClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addToFile(const std::string& message) {
|
void addToFile(const std::string& message) {
|
||||||
|
if (filepath == "") {
|
||||||
|
filepath = pathToLogFiles + "/" + appName + " " + getDateString() + ".log";
|
||||||
|
}
|
||||||
|
|
||||||
if (!openLogFileFailed) {
|
if (!openLogFileFailed) {
|
||||||
ofstream logFile;
|
ofstream logFile;
|
||||||
logFile.open(filepath, std::ios_base::app);
|
logFile.open(filepath, std::ios_base::app);
|
||||||
@@ -85,5 +90,13 @@ class LoggerClass {
|
|||||||
logFile.close();
|
logFile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string getDateString() {
|
||||||
|
time_t nowTime = time(NULL);
|
||||||
|
struct tm *now = localtime(&nowTime);
|
||||||
|
char str[12];
|
||||||
|
strftime(str, sizeof(str), "%Y-%m-%e", now);
|
||||||
|
return std::string(str);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user