mirror of
https://github.com/tnodir/fort
synced 2024-11-15 06:35:23 +00:00
UI: Logger: Append to last log file
This commit is contained in:
parent
e2169e33f9
commit
041db3e46f
@ -142,38 +142,96 @@ QString Logger::fileNameSuffix() const
|
||||
|
||||
bool Logger::openLogFile()
|
||||
{
|
||||
const QString fileName =
|
||||
fileNamePrefix() + getDateString("yyyy-MM-dd_HH-mm-ss_zzz") + fileNameSuffix();
|
||||
|
||||
if (tryOpenLogFile(m_dir, fileName))
|
||||
if (openDirFile())
|
||||
return true;
|
||||
|
||||
m_dir.setPath(FileUtil::pathSlash(FileUtil::appConfigLocation()) + "logs/");
|
||||
if (tryOpenLogFile(m_dir, fileName))
|
||||
if (openDirFile())
|
||||
return true;
|
||||
|
||||
m_dir.setPath(FileUtil::pathSlash(FileUtil::tempLocation()) + APP_NAME);
|
||||
if (tryOpenLogFile(m_dir, fileName))
|
||||
if (openDirFile())
|
||||
return true;
|
||||
|
||||
qCDebug(LC) << "Cannot open log file:" << m_file.fileName() << m_file.errorString();
|
||||
qCWarning(LC) << "Cannot open log file:" << m_file.fileName() << m_file.errorString();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Logger::tryOpenLogFile(const QDir &dir, const QString &fileName)
|
||||
bool Logger::openDirFile()
|
||||
{
|
||||
dir.mkpath("./");
|
||||
m_dir.mkpath("./");
|
||||
|
||||
m_file.setFileName(dir.filePath(fileName));
|
||||
|
||||
return m_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
|
||||
return openLastFile() || createNewFile();
|
||||
}
|
||||
|
||||
void Logger::closeLogFile()
|
||||
bool Logger::openLastFile()
|
||||
{
|
||||
const auto fileNames = FileUtil::getFileNames(m_dir, fileNamePrefix(), fileNameSuffix());
|
||||
if (fileNames.isEmpty())
|
||||
return false;
|
||||
|
||||
const auto fileName = fileNames.first();
|
||||
if (!openFile(fileName))
|
||||
return false;
|
||||
|
||||
if (!checkFileSize()) {
|
||||
closeFile();
|
||||
return false;
|
||||
}
|
||||
|
||||
writeLogLine("\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Logger::createNewFile()
|
||||
{
|
||||
const QString fileName =
|
||||
fileNamePrefix() + getDateString("yyyy-MM-dd_HH-mm-ss_zzz") + fileNameSuffix();
|
||||
|
||||
return openFile(fileName);
|
||||
}
|
||||
|
||||
bool Logger::openFile(const QString &fileName)
|
||||
{
|
||||
m_file.setFileName(m_dir.filePath(fileName));
|
||||
|
||||
return m_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
|
||||
}
|
||||
|
||||
void Logger::closeFile()
|
||||
{
|
||||
m_file.close();
|
||||
}
|
||||
|
||||
bool Logger::checkFileOpen(const QString &dateString)
|
||||
{
|
||||
// Create file when required to avoid empty files
|
||||
if (m_file.isOpen())
|
||||
return true;
|
||||
|
||||
FileUtil::removeOldFiles(m_dir, fileNamePrefix(), fileNameSuffix(), LOGGER_KEEP_FILES);
|
||||
|
||||
if (!openLogFile())
|
||||
return false;
|
||||
|
||||
// Write file header
|
||||
writeLogLine(makeLogLine(Info, dateString, getFileTitle()));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Logger::checkFileSize()
|
||||
{
|
||||
if (m_file.size() < LOGGER_FILE_MAX_SIZE)
|
||||
return true;
|
||||
|
||||
closeFile(); // too big file
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Logger::writeLogLine(const QString &logLine)
|
||||
{
|
||||
m_file.write(logLine.toUtf8());
|
||||
@ -187,24 +245,9 @@ void Logger::writeLog(const QString &dateString, const QString &logLine)
|
||||
|
||||
m_writing = true;
|
||||
|
||||
// Create file when required to avoid empty files
|
||||
if (!m_file.isOpen()) {
|
||||
FileUtil::removeOldFiles(
|
||||
m_dir.path(), fileNamePrefix(), fileNameSuffix(), LOGGER_KEEP_FILES);
|
||||
|
||||
if (!openLogFile()) {
|
||||
m_writing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Write file header
|
||||
writeLogLine(makeLogLine(Info, dateString, getFileTitle()));
|
||||
}
|
||||
|
||||
writeLogLine(logLine);
|
||||
|
||||
if (m_file.size() > LOGGER_FILE_MAX_SIZE) {
|
||||
closeLogFile(); // Too big file
|
||||
if (checkFileOpen(dateString)) {
|
||||
writeLogLine(logLine);
|
||||
checkFileSize();
|
||||
}
|
||||
|
||||
m_writing = false;
|
||||
|
@ -46,8 +46,14 @@ private:
|
||||
QString fileNameSuffix() const;
|
||||
|
||||
bool openLogFile();
|
||||
bool tryOpenLogFile(const QDir &dir, const QString &fileName);
|
||||
void closeLogFile();
|
||||
bool openDirFile();
|
||||
bool openLastFile();
|
||||
bool createNewFile();
|
||||
bool openFile(const QString &fileName);
|
||||
void closeFile();
|
||||
|
||||
bool checkFileOpen(const QString &dateString);
|
||||
bool checkFileSize();
|
||||
|
||||
void writeLogLine(const QString &logLine);
|
||||
|
||||
|
@ -393,15 +393,18 @@ QString tempLocation()
|
||||
return QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
||||
}
|
||||
|
||||
void removeOldFiles(const QString &path, const QString &fileNamePrefix,
|
||||
const QString &fileNameSuffix, int keepFiles)
|
||||
QStringList getFileNames(QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix)
|
||||
{
|
||||
QDir dir(path);
|
||||
const auto fileNames =
|
||||
dir.entryList({ fileNamePrefix + '*' + fileNameSuffix }, QDir::Files, QDir::Time);
|
||||
return dir.entryList({ fileNamePrefix + '*' + fileNameSuffix }, QDir::Files, QDir::Time);
|
||||
}
|
||||
|
||||
void removeOldFiles(
|
||||
QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix, int keepFiles)
|
||||
{
|
||||
const auto fileNames = getFileNames(dir, fileNamePrefix, fileNameSuffix);
|
||||
|
||||
for (const QString &fileName : fileNames) {
|
||||
if (--keepFiles < 0) {
|
||||
if (--keepFiles <= 0) {
|
||||
dir.remove(fileName);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <QDateTime>
|
||||
#include <QObject>
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QDir)
|
||||
|
||||
namespace FileUtil {
|
||||
|
||||
QString systemAppDescription();
|
||||
@ -71,8 +73,9 @@ QString appConfigLocation();
|
||||
QString applicationsLocation();
|
||||
QString tempLocation();
|
||||
|
||||
void removeOldFiles(const QString &path, const QString &fileNamePrefix,
|
||||
const QString &fileNameSuffix, int keepFiles);
|
||||
QStringList getFileNames(QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix);
|
||||
void removeOldFiles(
|
||||
QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix, int keepFiles);
|
||||
};
|
||||
|
||||
#endif // FILEUTIL_H
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
|
||||
#ifdef USE_VISUAL_LEAK_DETECTOR
|
||||
# include <vld.h>
|
||||
@ -65,7 +66,8 @@ void setupCrashHandler(CrashHandler &crashHandler, const FortSettings &settings)
|
||||
const QString fileNameSuffix = ".dmp";
|
||||
constexpr int CRASH_KEEP_FILES = 7;
|
||||
|
||||
FileUtil::removeOldFiles(dumpPath, fileNamePrefix, fileNameSuffix, CRASH_KEEP_FILES);
|
||||
QDir dumpDir(dumpPath);
|
||||
FileUtil::removeOldFiles(dumpDir, fileNamePrefix, fileNameSuffix, CRASH_KEEP_FILES);
|
||||
|
||||
crashHandler.setFileNamePrefix(fileNamePrefix + APP_VERSION_STR + APP_VERSION_BUILD_STR + '_');
|
||||
crashHandler.setFileNameSuffix(fileNameSuffix);
|
||||
|
Loading…
Reference in New Issue
Block a user