mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-14 02:36:51 -07:00
Log to File
This commit is contained in:
parent
db25969efa
commit
1811fe2133
4 changed files with 96 additions and 7 deletions
|
@ -21,7 +21,9 @@
|
||||||
#include <chiaki/log.h>
|
#include <chiaki/log.h>
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
|
class QFile;
|
||||||
class StreamSession;
|
class StreamSession;
|
||||||
|
|
||||||
class SessionLog
|
class SessionLog
|
||||||
|
@ -30,15 +32,19 @@ class SessionLog
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StreamSession *session;
|
StreamSession *session;
|
||||||
ChiakiLog chiaki_log;
|
ChiakiLog log;
|
||||||
|
QFile *file;
|
||||||
|
QMutex file_mutex;
|
||||||
|
|
||||||
void Log(ChiakiLogLevel level, const char *msg);
|
void Log(ChiakiLogLevel level, const char *msg);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SessionLog(StreamSession *session, uint32_t level_mask, const QString &filename);
|
SessionLog(StreamSession *session, uint32_t level_mask, const QString &filename);
|
||||||
|
~SessionLog();
|
||||||
|
|
||||||
ChiakiLog *GetChiakiLog() { return &chiaki_log; }
|
ChiakiLog *GetChiakiLog() { return &log; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QString CreateLogFilename();
|
||||||
|
|
||||||
#endif //CHIAKI_SESSIONLOG_H
|
#endif //CHIAKI_SESSIONLOG_H
|
||||||
|
|
|
@ -65,6 +65,7 @@ int main(int argc, char *argv[])
|
||||||
StreamSessionConnectInfo connect_info;
|
StreamSessionConnectInfo connect_info;
|
||||||
|
|
||||||
connect_info.log_level_mask = CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE;
|
connect_info.log_level_mask = CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE;
|
||||||
|
connect_info.log_file = CreateLogFilename();
|
||||||
|
|
||||||
connect_info.host = host;
|
connect_info.host = host;
|
||||||
connect_info.registkey = parser.value(regist_key_option);
|
connect_info.registkey = parser.value(regist_key_option);
|
||||||
|
|
|
@ -18,19 +18,57 @@
|
||||||
#include <sessionlog.h>
|
#include <sessionlog.h>
|
||||||
#include <chiaki/log.h>
|
#include <chiaki/log.h>
|
||||||
|
|
||||||
|
#include <QStandardPaths>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
|
||||||
static void LogCb(ChiakiLogLevel level, const char *msg, void *user);
|
static void LogCb(ChiakiLogLevel level, const char *msg, void *user);
|
||||||
|
|
||||||
SessionLog::SessionLog(StreamSession *session, uint32_t level_mask, const QString &filename)
|
SessionLog::SessionLog(StreamSession *session, uint32_t level_mask, const QString &filename)
|
||||||
: session(session)
|
: session(session)
|
||||||
{
|
{
|
||||||
chiaki_log_init(&chiaki_log, level_mask, LogCb, this);
|
chiaki_log_init(&log, level_mask, LogCb, this);
|
||||||
|
|
||||||
// TODO: file
|
if(filename.isEmpty())
|
||||||
|
{
|
||||||
|
file = nullptr;
|
||||||
|
CHIAKI_LOGI(&log, "Logging to file disabled");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
file = new QFile(filename);
|
||||||
|
if(!file->open(QIODevice::ReadWrite))
|
||||||
|
{
|
||||||
|
delete file;
|
||||||
|
file = nullptr;
|
||||||
|
CHIAKI_LOGI(&log, "Failed to open file %s for logging", filename.toLocal8Bit().constData());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CHIAKI_LOGI(&log, "Logging to file %s", filename.toLocal8Bit().constData());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SessionLog::~SessionLog()
|
||||||
|
{
|
||||||
|
delete file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SessionLog::Log(ChiakiLogLevel level, const char *msg)
|
void SessionLog::Log(ChiakiLogLevel level, const char *msg)
|
||||||
{
|
{
|
||||||
chiaki_log_cb_print(level, msg, nullptr);
|
chiaki_log_cb_print(level, msg, nullptr);
|
||||||
|
|
||||||
|
if(file)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&file_mutex);
|
||||||
|
// TODO: add timestamp and level
|
||||||
|
file->write(msg);
|
||||||
|
file->write("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SessionLogPrivate
|
class SessionLogPrivate
|
||||||
|
@ -43,4 +81,49 @@ static void LogCb(ChiakiLogLevel level, const char *msg, void *user)
|
||||||
{
|
{
|
||||||
auto log = reinterpret_cast<SessionLog *>(user);
|
auto log = reinterpret_cast<SessionLog *>(user);
|
||||||
SessionLogPrivate::Log(log, level, msg);
|
SessionLogPrivate::Log(log, level, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define KEEP_LOG_FILES_COUNT 5
|
||||||
|
|
||||||
|
static const QString date_format = "yyyy-MM-dd_HH-mm-ss-zzzz";
|
||||||
|
static const QString session_log_wildcard = "chiaki_session_*.log";
|
||||||
|
static const QRegularExpression session_log_regex("chiaki_session_(.*).log");
|
||||||
|
|
||||||
|
QString CreateLogFilename()
|
||||||
|
{
|
||||||
|
auto base_dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||||
|
if(base_dir.isEmpty())
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
QDir dir(base_dir);
|
||||||
|
if(!dir.mkpath("log"))
|
||||||
|
return QString();
|
||||||
|
if(!dir.cd("log"))
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
dir.setNameFilters({ session_log_wildcard });
|
||||||
|
auto existing_files = dir.entryList();
|
||||||
|
QVector<QPair<QString, QDateTime>> existing_files_date;
|
||||||
|
existing_files_date.resize(existing_files.count());
|
||||||
|
std::transform(existing_files.begin(), existing_files.end(), existing_files_date.begin(), [](const QString &filename) {
|
||||||
|
QDateTime date;
|
||||||
|
auto match = session_log_regex.match(filename);
|
||||||
|
if(match.hasMatch())
|
||||||
|
date = QDateTime::fromString(match.captured(1), date_format);
|
||||||
|
return QPair<QString, QDateTime>(filename, date);
|
||||||
|
});
|
||||||
|
std::sort(existing_files_date.begin(), existing_files_date.end(), [](const QPair<QString, QDateTime> &a, const QPair<QString, QDateTime> &b) {
|
||||||
|
return a.second > b.second;
|
||||||
|
});
|
||||||
|
|
||||||
|
for(int i=KEEP_LOG_FILES_COUNT; i<existing_files_date.count(); i++)
|
||||||
|
{
|
||||||
|
const auto &pair = existing_files_date[i];
|
||||||
|
if(!pair.second.isValid())
|
||||||
|
break;
|
||||||
|
dir.remove(pair.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString filename = "chiaki_session_" + QDateTime::currentDateTime().toString(date_format) + ".log";
|
||||||
|
return dir.absoluteFilePath(filename);
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
|
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QAudioOutput>
|
#include <QAudioOutput>
|
||||||
#include <QDebug>
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue