Add filepath and new filename of log file's

This commit is contained in:
Aleksey
2026-01-17 11:01:21 +03:00
parent 3311930dc4
commit 78412be6fe
3 changed files with 24 additions and 9 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
build/
build/
*.log

View File

@@ -3,7 +3,8 @@
int main() {
LoggerClass logger;
logger.logName = "Test";
logger.appName = "Test";
logger.pathToLogFiles = "/home/jam/local-gitea/CPlusPlus/logging/";
logger.logLevel=0;
logger.debug("YA SMOG STROKU PEREDAT'");

View File

@@ -14,10 +14,10 @@ enum LogLevels {
class LoggerClass {
public:
std::string logName;
std::string filepath = "test.log";
std::string appName;
std::string pathToLogFiles = "/var/log/" + appName;
int logLevel;
void debug(const std::string& message) {
action(LogLevels::DEBUG, message);
}
@@ -29,16 +29,17 @@ class LoggerClass {
void warn(const std::string& message) {
action(LogLevels::WARN, message);
}
void error(const std::string& message) {
action(LogLevels::ERROR, message);
}
void critical(const std::string& message) {
action(CRITICAL, message);
}
private:
std::string filepath = "";
bool openLogFileFailed = false;
void action(int logLevel, const std::string message) {
@@ -57,7 +58,7 @@ class LoggerClass {
case CRITICAL: logLevelStr = std::string("CRITICAL"); 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) {
print(line);
}
@@ -75,6 +76,10 @@ class LoggerClass {
}
void addToFile(const std::string& message) {
if (filepath == "") {
filepath = pathToLogFiles + "/" + appName + " " + getDateString() + ".log";
}
if (!openLogFileFailed) {
ofstream logFile;
logFile.open(filepath, std::ios_base::app);
@@ -85,5 +90,13 @@ class LoggerClass {
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);
}
};