#10 Keep log file open

pull/11/head
Geoffrey Merck 5 years ago
parent 0914d3145d
commit b696b92da4

@ -24,34 +24,79 @@
#include "LogFileTarget.h" #include "LogFileTarget.h"
#define LOG_FILE_ROOT "dstargateway"
CLogFileTarget::CLogFileTarget(LOG_SEVERITY logLevel, const std::string & dir, bool rotate) : CLogFileTarget::CLogFileTarget(LOG_SEVERITY logLevel, const std::string & dir, bool rotate) :
CLogTarget(logLevel), CLogTarget(logLevel),
m_dir(dir), m_dir(dir),
m_rotate(rotate) m_rotate(rotate),
m_file(),
m_day(0)
{ {
} }
void CLogFileTarget::printLogInt(const std::string& msg) CLogFileTarget::~CLogFileTarget()
{
if(m_file.is_open()) {
m_file.close();
}
}
void CLogFileTarget::printLogIntFixed(const std::string& msg)
{
if(m_file.is_open()) {
m_file << msg;
m_file.flush();
return;
}
std::string filename(m_dir);
if(filename[filename.length() - 1U] != '/') filename.push_back('/');
filename.append(LOG_FILE_ROOT).append(".log");
m_file.open(filename, std::ios::app);
if(m_file.is_open()) {
printLogIntFixed(msg);
}
else {
std::cerr << "FAILED TO OPEN LOG FILE :" << filename;
}
}
void CLogFileTarget::printLogIntRotate(const std::string& msg)
{ {
// construct filename std::time_t now = std::time(0);
std::string fileName(m_dir); std::tm* now_tm = std::gmtime(&now);
if(fileName[fileName.length() - 1U] != '/') fileName.push_back('/'); if(now_tm->tm_yday != m_day) {
fileName.append("dstargateway"); m_day = now_tm->tm_yday;
if(m_file.is_open()) {
if(m_rotate) { m_file.close();
std::time_t now = std::time(0); }
std::tm* now_tm = std::gmtime(&now); }
if(!m_file.is_open()) {
std::string filename(m_dir);
if(filename[filename.length() - 1U] != '/') filename.push_back('/');
char buf[64]; char buf[64];
std::strftime(buf, 42, "-%Y-%m-%d", now_tm); std::strftime(buf, 42, "-%Y-%m-%d", now_tm);
fileName.append(std::string(buf)); filename.append(LOG_FILE_ROOT).append(buf).append(".log");
m_file.open(filename, std::ios::app);
if(!m_file.is_open()) {
std::cerr << "FAILED TO OPEN LOG FILE :" << filename;
}
} }
fileName.append(".log");
std::ofstream file; if(m_file.is_open()) {
file.open(fileName, std::ios::app); m_file << msg;
if(file.is_open()) { m_file.flush();
file << msg; return;
file.close();
} }
} }
void CLogFileTarget::printLogInt(const std::string& msg)
{
if(m_rotate)
printLogIntRotate(msg);
else
printLogIntFixed(msg);
}

@ -19,6 +19,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <fstream>
#include "LogTarget.h" #include "LogTarget.h"
@ -26,11 +27,17 @@ class CLogFileTarget : public CLogTarget
{ {
public: public:
CLogFileTarget(LOG_SEVERITY logLevel, const std::string& directory, bool rotate); CLogFileTarget(LOG_SEVERITY logLevel, const std::string& directory, bool rotate);
~CLogFileTarget();
protected: protected:
virtual void printLogInt(const std::string& msg); virtual void printLogInt(const std::string& msg);
private: private:
void printLogIntRotate(const std::string& msg);
void printLogIntFixed(const std::string& msg);
std::string buildFileName();
std::string m_dir; std::string m_dir;
bool m_rotate; bool m_rotate;
std::fstream m_file;
int m_day;
}; };
Loading…
Cancel
Save

Powered by TurnKey Linux.