Merge pull request #142 from mammonsama666/0522-dev

feat(log): Add log file.
This commit is contained in:
chenlelin 2021-05-22 17:23:24 +08:00 committed by GitHub
commit a058591a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 11 deletions

View File

@ -218,6 +218,7 @@ void BackThread::execConnLan(QString connName, QString ifname, QString connectTy
} else {
mycmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + ifname + "'";
}
qDebug()<<"Trying to connect wifi. ssid="<<connName;
if (isWiredCableAlready) {
QStringList options;
@ -375,6 +376,7 @@ void BackThread::execConnRememberedHiddenWifi(QString wifiName)
}
if (! is_hidden) {
QString cmd = "nmcli connection up '" + wifiName + "'";
qDebug()<<"Trying to connect wifi. ssid="<<wifiName;
int res = Utils::m_system(cmd.toUtf8().data());
if (res == 0) {
emit connDone(6);
@ -413,6 +415,7 @@ void BackThread::execConnWifi(QString connName, QString connIfName)
//qDebug() << "-------------------------> connName = " << connName;
cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + connIfName + "'\n";
}
qDebug()<<"Trying to connect wifi. ssid="<<connName;
QStringList options;
options << "-c" << cmdStr;
@ -427,6 +430,7 @@ void BackThread::execReconnWIfi(QString uuid)
Utils::m_system(cmd.toUtf8().data());
cmd = "nmcli connection up " + uuid;
Utils::m_system(cmd.toUtf8().data());
qDebug()<<"Trying to connect wifi. uuid="<<uuid;
}
void BackThread::onReadOutputWifi()

View File

@ -27,15 +27,62 @@
#include <QDebug>
#include <QDesktopWidget>
#include <X11/Xlib.h>
#include <syslog.h>
#include <QFile>
#define LOG_IDENT "ukui_kylin_nm"
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit();
bool showDebug = true;
QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/ukui/kylin-nm.log";
//若不需要自动创建日志文件,请放开此注释
// if (!QFile::exists(logFilePath)) {
// showDebug = false;
// }
FILE *log_file = nullptr;
if (showDebug) {
log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+");
}
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
if (!log_file) {
break;
}
fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtCriticalMsg:
fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
}
if (log_file)
fclose(log_file);
}
int main(int argc, char *argv[])
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication a(argc, argv);
qInstallMessageHandler(messageOutput);
openlog(LOG_IDENT, LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_USER);
syslog(LOG_DEBUG, "Kylin Network Manager Is Already Launched");

View File

@ -1802,6 +1802,7 @@ void MainWindow::getWifiListDone(QStringList slist)
int mdf_res = system(modifyCmd.toUtf8().data());
qDebug()<<"Modification finished, cmd = "<<modifyCmd<<". res = "<<mdf_res;
QString reconnectWifiCmd = "nmcli connection up \"" + wifiSsid + "\"";
qDebug()<<"Trying to connect wifi. ssid="<<wifiSsid;
int con_res = system(reconnectWifiCmd.toUtf8().data());
qDebug()<<"Reconnect finished, cmd = "<<reconnectWifiCmd<<". res = "<<con_res;
emit this->stopReconnectWifi(wifiSsid);
@ -2254,7 +2255,7 @@ QVector<structWifiProperty> MainWindow::connectableWifiPriorityList(const QStrin
QString wifiname = line.mid(indexName).trimmed();
QString wifibssid = line.mid(indexBSsid, indexPath-indexBSsid).trimmed();
QString wifiObjectPath;
if (indexCate) {
if (indexCate >= 0) {
wifiObjectPath = line.mid(indexPath,indexCate-indexPath).trimmed();
} else {
wifiObjectPath = line.mid(indexPath,indexName-indexPath).trimmed();
@ -2487,12 +2488,11 @@ void MainWindow::loadWifiListDone(QStringList slist)
wcate = line.mid(indexCate, 1).trimmed();
else
wcate = QString::number(0);
int Path = line.indexOf("/org/");
QString wDbusPath;
if (indexCate >= 0) {
wDbusPath = line.mid(Path,indexCate-Path).trimmed();
wDbusPath = line.mid(indexPath,indexCate-indexPath).trimmed();
} else {
wDbusPath = line.mid(Path,indexName-Path).trimmed();
wDbusPath = line.mid(indexPath,indexName-indexPath).trimmed();
}
QDBusInterface interface("org.freedesktop.NetworkManager",
wDbusPath,
@ -2525,13 +2525,11 @@ void MainWindow::loadWifiListDone(QStringList slist)
int max_freq = wfreq.toInt();
int min_freq = wfreq.toInt();
for (int k = i; k < slist.size(); k ++) {
QString tmpLine = slist.at(k);
int Path = tmpLine.indexOf("/org/");
QString m_DbusPath;
if (indexCate >= 0) {
m_DbusPath = line.mid(Path,indexCate-Path).trimmed();
m_DbusPath = slist.at(k).mid(indexPath,indexCate-indexPath).trimmed();
} else {
m_DbusPath = line.mid(Path,indexName-Path).trimmed();
m_DbusPath = slist.at(k).mid(indexPath,indexName-indexPath).trimmed();
}
QDBusInterface m_interface("org.freedesktop.NetworkManager",
m_DbusPath,
@ -2548,7 +2546,7 @@ void MainWindow::loadWifiListDone(QStringList slist)
}
}
}
qDebug()<<"Wifi ssid="<<wname<<". max_freq="<<max_freq<<". min_freq="<<min_freq;
int freqState = 0;
if (max_freq < 3000) {
//只有2.4GHZ
@ -2801,7 +2799,7 @@ void MainWindow::updateWifiListDone(QStringList slist)
QString wname = line.mid(indexName).trimmed();
QString wfreq = line.mid(indexFreq, 4).trimmed();
QString wpath;
if (indexCate) {
if (indexCate >= 0) {
wpath = line.mid(indexPath, indexCate - indexPath).trimmed();
} else {
wpath = line.mid(indexPath, indexName - indexPath).trimmed();
@ -2831,6 +2829,7 @@ void MainWindow::updateWifiListDone(QStringList slist)
}
}
qDebug()<<"Wifi ssid="<<wname<<". max_freq="<<max_freq<<". min_freq="<<min_freq;
int freqState = 0;
if (max_freq < 3000) {
//只有2.4GHZ
@ -3992,6 +3991,7 @@ void MainWindow::connWifiDone(int connFlag)
// Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 4密码错误 5找不到已保存的网络
if (connFlag == 0) {
syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button");
qDebug()<<"Wi-Fi connected succeed. res=0";
if (!isHuaWeiPC) {
//如果不是华为电脑,使用获取连接信号的方式更新列表
this->ksnm->execGetWifiList(this->wcardname, this->isHuaWeiPC);
@ -4005,6 +4005,7 @@ void MainWindow::connWifiDone(int connFlag)
objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 1) {
syslog(LOG_DEBUG, "Confirm your Wi-Fi password or usable of wireless card");
qWarning()<<"Wi-Fi connected failed. res=1";
//is_stop_check_net_state = 0;
is_connect_net_failed = 1;
is_connect_hide_wifi = 0;
@ -4012,20 +4013,24 @@ void MainWindow::connWifiDone(int connFlag)
objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 3) {
syslog(LOG_DEBUG, "Launch kylin-nm, Wi-Fi already connected");
qWarning()<<"Wi-Fi connected failed. res=3";
} else if (connFlag == 4) {
syslog(LOG_DEBUG, "Confirm your Wi-Fi password");
is_connect_net_failed = 1;
QString txt(tr("Confirm your Wi-Fi password"));
qWarning()<<"Wi-Fi connected failed. res=4";
objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 5) {
syslog(LOG_DEBUG, "Selected Wifi has not been scanned.");
is_connect_net_failed = 1;
QString txt(tr("Selected Wifi has not been scanned."));
qWarning()<<"Wi-Fi connected failed. res=5";
objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 6) {
syslog(LOG_DEBUG, "Connect Hidden Wifi Success.");
this->ksnm->execGetWifiList(this->wcardname, this->isHuaWeiPC);
QString txt(tr("Connect Hidden Wifi Success"));
qWarning()<<"Wi-Fi connected failed. res=6";
objKyDBus->showDesktopNotify(txt);
}

View File

@ -732,6 +732,7 @@ void OneConnForm::toConnectWirelessNetwork()
connect(t, &QThread::started, this, [ = ]() {
this->startWifiWaiting(true);
QString cmdStr = "nmcli connection up '" + wifiName + "' passwd-file " + homePath +"/.config/" + wifiName + ".psk";
qDebug()<<"Trying to connect wifi. ssid="<<wifiName;
emit this->sigConnWifiPsk(cmdStr);
});
connect(this, SIGNAL(sigConnWifiPsk(QString)), bt, SLOT(execConnWifiPsk(QString)));
@ -790,6 +791,7 @@ void OneConnForm::on_btnConnPWD_clicked()
connect(t, &QThread::started, this, [ = ]() {
this->startWifiWaiting(true);
QString cmdStr = "nmcli connection up '" + wifiName + "' passwd-file " + homePath +"/.config/" + wifiName + ".psk";
qDebug()<<"Trying to connect wifi. ssid="<<wifiName;
emit this->sigConnWifiPsk(cmdStr);
});
connect(this, SIGNAL(sigConnWifiPsk(QString)), bt, SLOT(execConnWifiPsk(QString)));
@ -983,8 +985,10 @@ void OneConnForm::slotConnWifiResult(int connFlag)
qDebug() << "实际连接的wifi的bssid是 " << wifiBSsid;
if (currConnWifiBssid != wifiBSsid && !currConnWifiBssid.isEmpty()) {
QString modityCmd = "nmcli connection modify \""+ wifiName + "\" " + "802-11-wireless.bssid " + wifiBSsid;
system(modityCmd.toUtf8().data());
int res = system(modityCmd.toUtf8().data());
qDebug()<<"Modified bssid for wifi. ssid="<<wifiName<<". bssid="<<wifiBSsid<<". res="<<res;
QString reconnectWifiCmd = "nmcli connection up \"" + wifiName + "\"";
qDebug()<<"Trying to connect wifi. ssid="<<wifiName;
system(reconnectWifiCmd.toUtf8().data());
} else {
emit requestRefreshWifiList();

View File

@ -54,6 +54,7 @@ void UpConnThread::run() {
} else {
cmdStr = "nmcli connection up '" + this->conn_name + "'";
}
qDebug()<<"Trying to connect wifi. ssid="<<this->conn_name;
qDebug()<<"qDebug: 激活连接: \n"<<
"qDebug: " + cmdStr;
int res = Utils::m_system(cmdStr.toUtf8().data());