mirror of
https://github.com/tnodir/fort
synced 2024-11-15 08:15:10 +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()
|
bool Logger::openLogFile()
|
||||||
{
|
{
|
||||||
const QString fileName =
|
if (openDirFile())
|
||||||
fileNamePrefix() + getDateString("yyyy-MM-dd_HH-mm-ss_zzz") + fileNameSuffix();
|
|
||||||
|
|
||||||
if (tryOpenLogFile(m_dir, fileName))
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
m_dir.setPath(FileUtil::pathSlash(FileUtil::appConfigLocation()) + "logs/");
|
m_dir.setPath(FileUtil::pathSlash(FileUtil::appConfigLocation()) + "logs/");
|
||||||
if (tryOpenLogFile(m_dir, fileName))
|
if (openDirFile())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
m_dir.setPath(FileUtil::pathSlash(FileUtil::tempLocation()) + APP_NAME);
|
m_dir.setPath(FileUtil::pathSlash(FileUtil::tempLocation()) + APP_NAME);
|
||||||
if (tryOpenLogFile(m_dir, fileName))
|
if (openDirFile())
|
||||||
return true;
|
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;
|
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 openLastFile() || createNewFile();
|
||||||
|
|
||||||
return m_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
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)
|
void Logger::writeLogLine(const QString &logLine)
|
||||||
{
|
{
|
||||||
m_file.write(logLine.toUtf8());
|
m_file.write(logLine.toUtf8());
|
||||||
@ -187,24 +245,9 @@ void Logger::writeLog(const QString &dateString, const QString &logLine)
|
|||||||
|
|
||||||
m_writing = true;
|
m_writing = true;
|
||||||
|
|
||||||
// Create file when required to avoid empty files
|
if (checkFileOpen(dateString)) {
|
||||||
if (!m_file.isOpen()) {
|
writeLogLine(logLine);
|
||||||
FileUtil::removeOldFiles(
|
checkFileSize();
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_writing = false;
|
m_writing = false;
|
||||||
|
@ -46,8 +46,14 @@ private:
|
|||||||
QString fileNameSuffix() const;
|
QString fileNameSuffix() const;
|
||||||
|
|
||||||
bool openLogFile();
|
bool openLogFile();
|
||||||
bool tryOpenLogFile(const QDir &dir, const QString &fileName);
|
bool openDirFile();
|
||||||
void closeLogFile();
|
bool openLastFile();
|
||||||
|
bool createNewFile();
|
||||||
|
bool openFile(const QString &fileName);
|
||||||
|
void closeFile();
|
||||||
|
|
||||||
|
bool checkFileOpen(const QString &dateString);
|
||||||
|
bool checkFileSize();
|
||||||
|
|
||||||
void writeLogLine(const QString &logLine);
|
void writeLogLine(const QString &logLine);
|
||||||
|
|
||||||
|
@ -393,15 +393,18 @@ QString tempLocation()
|
|||||||
return QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
return QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeOldFiles(const QString &path, const QString &fileNamePrefix,
|
QStringList getFileNames(QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix)
|
||||||
const QString &fileNameSuffix, int keepFiles)
|
|
||||||
{
|
{
|
||||||
QDir dir(path);
|
return dir.entryList({ fileNamePrefix + '*' + fileNameSuffix }, QDir::Files, QDir::Time);
|
||||||
const auto fileNames =
|
}
|
||||||
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) {
|
for (const QString &fileName : fileNames) {
|
||||||
if (--keepFiles < 0) {
|
if (--keepFiles <= 0) {
|
||||||
dir.remove(fileName);
|
dir.remove(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QDir)
|
||||||
|
|
||||||
namespace FileUtil {
|
namespace FileUtil {
|
||||||
|
|
||||||
QString systemAppDescription();
|
QString systemAppDescription();
|
||||||
@ -71,8 +73,9 @@ QString appConfigLocation();
|
|||||||
QString applicationsLocation();
|
QString applicationsLocation();
|
||||||
QString tempLocation();
|
QString tempLocation();
|
||||||
|
|
||||||
void removeOldFiles(const QString &path, const QString &fileNamePrefix,
|
QStringList getFileNames(QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix);
|
||||||
const QString &fileNameSuffix, int keepFiles);
|
void removeOldFiles(
|
||||||
|
QDir &dir, const QString &fileNamePrefix, const QString &fileNameSuffix, int keepFiles);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILEUTIL_H
|
#endif // FILEUTIL_H
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
#ifdef USE_VISUAL_LEAK_DETECTOR
|
#ifdef USE_VISUAL_LEAK_DETECTOR
|
||||||
# include <vld.h>
|
# include <vld.h>
|
||||||
@ -65,7 +66,8 @@ void setupCrashHandler(CrashHandler &crashHandler, const FortSettings &settings)
|
|||||||
const QString fileNameSuffix = ".dmp";
|
const QString fileNameSuffix = ".dmp";
|
||||||
constexpr int CRASH_KEEP_FILES = 7;
|
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.setFileNamePrefix(fileNamePrefix + APP_VERSION_STR + APP_VERSION_BUILD_STR + '_');
|
||||||
crashHandler.setFileNameSuffix(fileNameSuffix);
|
crashHandler.setFileNameSuffix(fileNameSuffix);
|
||||||
|
Loading…
Reference in New Issue
Block a user