feat: Apply log level on IPC connection

This commit is contained in:
Nick Bolton 2025-02-10 14:12:27 +00:00
parent 5733541b2a
commit 5743db3040
4 changed files with 44 additions and 17 deletions

View file

@ -159,6 +159,9 @@ CoreProcess::CoreProcess(const IAppConfig &appConfig, const IServerConfig &serve
m_daemonIpcClient{new ipc::DaemonIpcClient(this)} m_daemonIpcClient{new ipc::DaemonIpcClient(this)}
{ {
if (m_appConfig.processMode() == ProcessMode::kService) { if (m_appConfig.processMode() == ProcessMode::kService) {
connect(m_daemonIpcClient, &ipc::DaemonIpcClient::connected, this, &CoreProcess::daemonIpcClientConnected);
const auto logPath = requestDaemonLogPath(); const auto logPath = requestDaemonLogPath();
if (!logPath.isEmpty()) { if (!logPath.isEmpty()) {
qInfo() << "daemon log path:" << logPath; qInfo() << "daemon log path:" << logPath;
@ -167,6 +170,8 @@ CoreProcess::CoreProcess(const IAppConfig &appConfig, const IServerConfig &serve
} }
} }
connect(m_daemonIpcClient, &ipc::DaemonIpcClient::connected, this, &CoreProcess::daemonIpcClientConnected);
connect(&m_pDeps->process(), &QProcessProxy::finished, this, &CoreProcess::onProcessFinished); connect(&m_pDeps->process(), &QProcessProxy::finished, this, &CoreProcess::onProcessFinished);
connect( connect(
@ -224,6 +229,18 @@ void CoreProcess::onProcessReadyReadStandardError()
} }
} }
void CoreProcess::daemonIpcClientConnected()
{
applyLogLevel();
const auto logPath = requestDaemonLogPath();
if (!logPath.isEmpty()) {
qInfo() << "daemon log path:" << logPath;
m_daemonFileTail = new FileTail(logPath, this);
connect(m_daemonFileTail, &FileTail::newLine, this, &CoreProcess::handleLogLines);
}
}
void CoreProcess::onProcessFinished(int exitCode, QProcess::ExitStatus) void CoreProcess::onProcessFinished(int exitCode, QProcess::ExitStatus)
{ {
const auto wasStarted = m_processState == ProcessState::Started; const auto wasStarted = m_processState == ProcessState::Started;

View file

@ -142,6 +142,7 @@ private slots:
void onProcessFinished(int exitCode, QProcess::ExitStatus); void onProcessFinished(int exitCode, QProcess::ExitStatus);
void onProcessReadyReadStandardOutput(); void onProcessReadyReadStandardOutput();
void onProcessReadyReadStandardError(); void onProcessReadyReadStandardError();
void daemonIpcClientConnected();
private: private:
void startForegroundProcess(const QString &app, const QStringList &args); void startForegroundProcess(const QString &app, const QStringList &args);

View file

@ -43,74 +43,80 @@ bool DaemonIpcClient::connectToServer()
m_connected = true; m_connected = true;
qInfo() << "ipc client connected to server:" << kDaemonIpcName; qInfo() << "ipc client connected to server:" << kDaemonIpcName;
Q_EMIT connected();
return true; return true;
} }
void DaemonIpcClient::handleDisconnected() void DaemonIpcClient::handleDisconnected()
{ {
qInfo() << "ipc client disconnected from server"; qWarning() << "ipc client disconnected from server";
m_connected = false; m_connected = false;
if (!connectToServer()) {
qWarning() << "ipc client failed to reconnect to server";
}
} }
void DaemonIpcClient::handleErrorOccurred() void DaemonIpcClient::handleErrorOccurred()
{ {
qCritical() << "ipc client error:" << m_socket->errorString(); qWarning() << "ipc client error:" << m_socket->errorString();
m_connected = false; m_connected = false;
} }
bool DaemonIpcClient::sendMessage(const QString &message, const QString &expectAck, const bool expectConnected) bool DaemonIpcClient::sendMessage(const QString &message, const QString &expectAck, const bool expectConnected)
{ {
if (expectConnected && !m_connected) { if (expectConnected && !m_connected) {
qWarning() << "cannot send command, ipc not connected"; qWarning() << "cannot send command, ipc client not connected";
return false; return false;
} }
QByteArray messageData = message.toUtf8() + "\n"; QByteArray messageData = message.toUtf8() + "\n";
m_socket->write(messageData); m_socket->write(messageData);
if (!m_socket->waitForBytesWritten(kTimeout)) { if (!m_socket->waitForBytesWritten(kTimeout)) {
qWarning() << "ipc failed to write command"; qWarning() << "ipc client failed to write command";
return false; return false;
} }
if (!expectAck.isEmpty()) { if (!expectAck.isEmpty()) {
qDebug() << "ipc waiting for ack: " << expectAck; qDebug() << "ipc client waiting for ack: " << expectAck;
if (!m_socket->waitForReadyRead(kTimeout)) { if (!m_socket->waitForReadyRead(kTimeout)) {
qWarning() << "ipc failed to read response"; qWarning() << "ipc client failed to read response";
return false; return false;
} }
QByteArray response = m_socket->readAll(); QByteArray response = m_socket->readAll();
if (response.isEmpty()) { if (response.isEmpty()) {
qWarning() << "ipc got empty response"; qWarning() << "ipc client got empty response";
return false; return false;
} }
QString responseData = QString::fromUtf8(response); QString responseData = QString::fromUtf8(response);
if (responseData.isEmpty()) { if (responseData.isEmpty()) {
qWarning() << "ipc failed to convert response to string"; qWarning() << "ipc client failed to convert response to string";
return false; return false;
} }
if (responseData != expectAck + "\n") { if (responseData != expectAck + "\n") {
qWarning() << "ipc got unexpected response: " << responseData; qWarning() << "ipc client got unexpected response: " << responseData;
return false; return false;
} }
} }
qDebug() << "ipc sent message: " << messageData; qDebug() << "ipc client sent message: " << messageData;
return true; return true;
} }
bool DaemonIpcClient::keepAlive() bool DaemonIpcClient::keepAlive()
{ {
if (!isConnected() && !connectToServer()) { if (!isConnected() && !connectToServer()) {
qWarning() << "ipc keep alive failed to connect"; qWarning() << "ipc client keep alive failed to connect";
return false; return false;
} }
if (!sendMessage("noop")) { if (!sendMessage("noop")) {
qWarning() << "ipc keep alive ping failed"; qWarning() << "ipc client keep alive ping failed";
m_connected = false; m_connected = false;
return false; return false;
} }
@ -158,31 +164,31 @@ QString DaemonIpcClient::requestLogPath()
} }
if (!m_socket->waitForReadyRead(kTimeout)) { if (!m_socket->waitForReadyRead(kTimeout)) {
qWarning() << "ipc failed to read log path response"; qWarning() << "ipc client failed to read log path response";
return QString(); return QString();
} }
QByteArray response = m_socket->readAll(); QByteArray response = m_socket->readAll();
if (response.isEmpty()) { if (response.isEmpty()) {
qWarning() << "ipc got empty log path response"; qWarning() << "ipc client got empty log path response";
return QString(); return QString();
} }
QString responseData = QString::fromUtf8(response); QString responseData = QString::fromUtf8(response);
if (responseData.isEmpty()) { if (responseData.isEmpty()) {
qWarning() << "ipc failed to convert log path response to string"; qWarning() << "ipc client failed to convert log path response to string";
return QString(); return QString();
} }
// Trimming removes newline from end of message. // Trimming removes newline from end of message.
QStringList parts = responseData.trimmed().split("="); QStringList parts = responseData.trimmed().split("=");
if (parts.size() != 2) { if (parts.size() != 2) {
qWarning() << "ipc got invalid log path response: " << responseData; qWarning() << "ipc client got invalid log path response: " << responseData;
return QString(); return QString();
} }
if (parts[0] != "logPath") { if (parts[0] != "logPath") {
qWarning() << "ipc got unexpected log path response: " << responseData; qWarning() << "ipc client got unexpected log path response: " << responseData;
return QString(); return QString();
} }

View file

@ -31,6 +31,9 @@ public:
return m_connected; return m_connected;
} }
signals:
void connected();
private slots: private slots:
void handleDisconnected(); void handleDisconnected();
void handleErrorOccurred(); void handleErrorOccurred();