utils.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This file contains minor utility functions
  3. */
  4. #ifndef UTILS_H
  5. #define UTILS_H
  6. #include <QNetworkAccessManager>
  7. #include <QNetworkReply>
  8. #include <QEventLoop>
  9. #include <QFile>
  10. #include <QTimer>
  11. bool checkInternetConnection(QUrl url) {
  12. QNetworkAccessManager nam;
  13. QNetworkRequest req(url);
  14. QNetworkReply* reply = nam.get(req);
  15. QEventLoop loop;
  16. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  17. QTimer request_timeout_timer;
  18. QObject::connect(&request_timeout_timer, &QTimer::timeout, &loop, &QEventLoop::quit);
  19. request_timeout_timer.start(5000); // 5 seconds connection timeout
  20. qDebug() << "LOLOLOLOL";
  21. loop.exec();
  22. qDebug() << "END OF LOLOLOLOLO";
  23. return reply->bytesAvailable();
  24. }
  25. void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  26. {
  27. QString message_type;
  28. switch (type) {
  29. case QtDebugMsg:
  30. message_type = "[DEBG]";
  31. break;
  32. case QtInfoMsg:
  33. message_type = "[INFO]";
  34. break;
  35. case QtWarningMsg:
  36. message_type = "[WARN]";
  37. break;
  38. case QtCriticalMsg:
  39. message_type = "[CRIT]";
  40. break;
  41. case QtFatalMsg:
  42. message_type = "[FATL]";
  43. break;
  44. default:
  45. message_type = "[UNKN]";
  46. break;
  47. }
  48. QString filename = context.category;
  49. QString function = context.function;
  50. QString line_number = QString::number(context.line);
  51. QString s = QString("%1: %2 ||| %3:%4 %5\n")
  52. .arg(message_type)
  53. .arg(msg)
  54. .arg(filename)
  55. .arg(line_number)
  56. .arg(function);
  57. #ifdef QT_DEBUG
  58. QFile log_file("legacy_v2.log");
  59. log_file.open(QIODevice::ReadWrite | QIODevice::Append);
  60. QTextStream stream(&log_file);
  61. stream << s;
  62. log_file.close();
  63. #endif
  64. fprintf(stderr, "%s", qUtf8Printable(s));
  65. fflush(stderr);
  66. }
  67. #endif // UTILS_H