#include "widget.h" #include "./ui_widget.h" #include #include #include #include #include #include #include #include #include "spdlog/spdlog.h" #include "LightLog.h" #include "nlohmann/json.hpp" #define nJson nlohmann::json Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); // m_curlFtp.setFtpIPAndPort("192.168.50.100", 21); m_curlFtp.setSftpIPAndPort("10.147.18.91", 22); m_curlFtp.setUsernameAndPassword("microsoft", "19980714Lq"); // m_curlFtp.setUsernameAndPassword("lh", "DWw7V9u0"); // m_curlFtp.setSftpIPAndPort("192.1.2.49", 32222); // m_curlFtp.setUsernameAndPassword("lhftp", "8tG!2fP*7bJ@5kQ"); // m_curlFtp.setIgnoreSSLCert(true); m_curlFtp.enableCurlDebug(true); SPDLOG_INFO("***** Qt Library *****"); } Widget::~Widget() { delete ui; } /* 解析JSON示例 */ void Widget::parseJSON() { SPDLOG_INFO("解析JSON"); nJson json0; json0["name"] = "microsoft"; json0["age"] = 18; json0["bool"] = true; json0["null"] = nullptr; json0["array"] = {1, 2, 3, 4, 5}; json0["object"] = {{"key1", "value1"}, {"key2", "value2"}}; /* 打印json值 */ SPDLOG_INFO("json0: {}", json0.dump()); std::string name = json0["name"]; std::string name2 = json0["name"].get(); std::string name1 = json0.at("name").get(); std::string name5 = json0.at("name"); std::string name4 = json0.value("name", "default"); int age = json0["age"].get(); bool b = json0["bool"]; nJson null = json0["null"]; nJson array = json0["array"]; nJson object = json0["object"]; /* 如果json中的值是null,在赋值给string时可能会报错,可以使用“?”运算符解决 */ std::string name3 = json0["name"].is_null() ? "" : json0["name"].get(); /* 从直接到JSON */ nJson json1 = R"( { "name": "microsoft", "age": 18, "bool": true, "null": null, "array": [1, 2, 3, 4, 5], "object": { "key1": "value1", "key2": "value2" } } )"_json; /* 或者 */ std::string str = R"( { "name": "microsoft", "age": 18, "bool": true, "null": null, "array": [1, 2, 3, 4, 5], "object": { "key1": "value1", "key2": "value2" } } )"; nJson json2 = nJson::parse(str); /* 从JSON到字符串 */ std::string str1 = json0.dump(); } void Widget::on_pBtn_connect_clicked() { SPDLOG_INFO("点击了“连接按钮”"); // std::vector list; // m_curlFtp.getFileList("/lh/Recall", list); // for(const std::string& filename : list) // { // QString qfilename = QString::fromLocal8Bit(filename.c_str()); // SPDLOG_INFO("{}", qfilename.toStdString()); // } /* 创建文件夹 */ // bool ret = m_curlFtp.createDirectories("/SSD2/lh/Recall/FlowChart/Data"); // if(ret) // { // SPDLOG_INFO("Data 文件夹创建成功"); // } else // { // SPDLOG_INFO("Data 文件夹创建失败"); // } // ret = m_curlFtp.createDirectories("/lh/Recall/FlowChart/Image"); // if(ret) // { // SPDLOG_INFO("Image 文件夹创建成功"); // } else // { // SPDLOG_INFO("Image 文件夹创建失败"); // } m_curlFtp.createDirectory("/SSD2/lh"); std::vector driList; m_curlFtp.getDirList("/SSD2", driList); for(auto& it : driList) { SPDLOG_INFO("{}", it); } } void Widget::on_pBtn_downloadFile_clicked() { SPDLOG_INFO("点击了“下载文件”"); std::vector fileList; // bool ret = m_curlFtp.downloadFile("/SSD2/Video/哪吒之魔童降世.mp4", QApplication::applicationDirPath().toStdString() + "/YPM.mp4"); bool ret = m_curlFtp.downloadFile("/SSD2/Video/Yes,Prime.Minister.rmvb", QApplication::applicationDirPath().toStdString() + "/YPM.rmvb"); if(ret) { SPDLOG_INFO("下载成功"); } else { SPDLOG_INFO("下载失败"); } } void Widget::on_pBtn_downloadVideo_clicked() { SPDLOG_INFO("点击了“下载视频”"); // std::shared_ptr ftp = std::make_shared(); // ftp->setHostAndPort("192.168.50.100"); // ftp->setUserPasswd("microsoft", "19980714Lq"); // QString localPath = QApplication::applicationDirPath() + "/v1.mp4"; // ftp->getFile(localPath, "/SSD1/Video/v1.mp4"); // ftp->waitFinished(-1); // if(ftp->getResult()) // { // SPDLOG_INFO("下载成功"); // } // else // { // SPDLOG_INFO("下载失败"); // } bool ret = m_curlFtp.createDirectories("SSD1/DOC/123/234"); if(ret) { SPDLOG_INFO("文件夹创建成功"); }else { SPDLOG_WARN("文件夹创建失败"); } } void Widget::on_pBtn_logSpeed_clicked() { SPDLOG_INFO("点击了“日志速度”"); /* 开始时间点1 */ std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); for(int i = 0; i < 100000; i++) { SPDLOG_INFO("{}", "你好,这是日志速度的对比,这个是SPDLOG在打印日志"); } /* 结束时间点1 */ std::chrono::steady_clock::time_point spdlogEnd = std::chrono::steady_clock::now(); for(int i = 0; i < 100000; i++) { // FMTLOG_INFO("{}", "你好,这是日志速度的对比,这个是FMTLOG在打印日志"); } std::chrono::steady_clock::time_point fmtlogEnd = std::chrono::steady_clock::now(); /* 使用std::cout */ for(int i = 0; i < 100000; i++) { std::cout << "你好,这是日志速度的对比," << "这个是std::cout在打印日志" << std::endl; } std::chrono::steady_clock::time_point coutEnd = std::chrono::steady_clock::now(); /* 使用printf */ for(int i = 0; i < 100000; i++) { printf("%s\n","你好,这是日志速度的对比,这个是printf在打印日志"); } std::chrono::steady_clock::time_point printfEnd = std::chrono::steady_clock::now(); /* 使用QDebug */ for(int i = 0; i < 100000; i++) { qDebug() << "你好,这是日志速度的对比," << "这个是QDebug在打印日志"; } std::chrono::steady_clock::time_point qDebugEnd = std::chrono::steady_clock::now(); /* 使用LightLog */ for(int i = 0; i < 100000; i++) { QLOG_INFO("你好,这是日志速度的对比," + "这个是LightLog在打印日志"); } std::chrono::steady_clock::time_point lightLogEnd = std::chrono::steady_clock::now(); /* 计算时间 */ std::chrono::duration spdlogTime = std::chrono::duration_cast>(spdlogEnd - start); std::chrono::duration fmtlogTime = std::chrono::duration_cast>(fmtlogEnd - spdlogEnd); std::chrono::duration coutTime = std::chrono::duration_cast>(coutEnd - fmtlogEnd); std::chrono::duration printfTime = std::chrono::duration_cast>(printfEnd - coutEnd); std::chrono::duration qDebugTime = std::chrono::duration_cast>(qDebugEnd - printfEnd); std::chrono::duration lightLogTime = std::chrono::duration_cast>(lightLogEnd - qDebugEnd); /* 打印时间 */ SPDLOG_INFO("SPDLOG打印耗时: {}", spdlogTime.count()); SPDLOG_INFO("FMTLOG打印耗时: {}", fmtlogTime.count()); SPDLOG_INFO("std::cout打印耗时: {}", coutTime.count()); SPDLOG_INFO("printf打印耗时: {}", printfTime.count()); SPDLOG_INFO("QDebug打印耗时: {}", qDebugTime.count()); SPDLOG_INFO("LightLog打印耗时: {}", lightLogTime.count()); } void Widget::on_pBtn_upload_clicked() { SPDLOG_INFO("点击了“上传”"); bool ret = m_curlFtp.uploadFile(QApplication::applicationDirPath().toStdString() + "/YPM.mp4", "/SSD2/Video/YPM.mp4"); if(ret) { SPDLOG_INFO("上传成功"); } else { SPDLOG_INFO("上传失败"); } }