utils.cpp 4.53 KB
Newer Older
1
#include "utils.h"
Thiago Santini's avatar
Thiago Santini committed
2

Thiago Santini's avatar
Thiago Santini committed
3 4
#include <QElapsedTimer>

5 6 7 8
const char Token::Delimiter = '\t';
const char Token::Newline = '\n';
const char Token::JournalEntry = 'J';
const char Token::HeaderEntry = 'H';
Thiago Santini's avatar
Thiago Santini committed
9 10 11
const char Token::MarkerIdDelimiter = ':';
const char Token::MarkerValueDelimiter = 'x';
const char Token::MarkerEnd = ';';
12
const int Token::Precision = 4;
13

14
const Timestamp maxTimestamp = std::numeric_limits<Timestamp>::max();
Thiago Santini's avatar
Thiago Santini committed
15

16
static QTextStream logStream;
17

Thiago Santini's avatar
Thiago Santini committed
18 19 20 21
/*
 * Utility functions
 */

22
QDebug operator<<(QDebug dbg, const QCameraViewfinderSettings& p)
Thiago Santini's avatar
Thiago Santini committed
23 24 25 26 27 28 29
{
    dbg.nospace() << toQString(p);
    return dbg.space();
}

void logInitBanner()
{
30 31 32 33 34
    static QFile logFile;
    if (!logFile.isOpen()) {
        logFile.setFileName("EyeRecToo.log");
        logFile.open(QIODevice::WriteOnly | QIODevice::Append);
        logStream.setDevice(&logFile);
Thiago Santini's avatar
Thiago Santini committed
35 36 37
    }

    QDateTime utc = QDateTime::currentDateTimeUtc();
38 39 40 41 42
    qInfo() << "Starting\n######################################################################"
            << "\n# " << QString("EyeRecToo v%1").arg(VERSION)
            << "\n# UTC:      " << utc.toString()
            << "\n# Local:    " << utc.toLocalTime().toString()
            << "\n######################################################################";
Thiago Santini's avatar
Thiago Santini committed
43 44 45 46 47
}

void logExitBanner()
{
    QDateTime utc = QDateTime::currentDateTimeUtc();
48 49 50 51
    qInfo() << "Exiting\n######################################################################"
            << "\n# UTC:      " << utc.toString()
            << "\n# Local:    " << utc.toLocalTime().toString()
            << "\n######################################################################\n";
Thiago Santini's avatar
Thiago Santini committed
52 53

    std::cout.flush();
54
    logStream.flush();
Thiago Santini's avatar
Thiago Santini committed
55 56
}

Thiago Santini's avatar
Thiago Santini committed
57
extern ReferenceClock gTimer;
58 59
extern LogWidget* gLogWidget;
void logMessages(QtMsgType type, const QMessageLogContext& context, const QString& msg)
Thiago Santini's avatar
Thiago Santini committed
60
{
61 62
    static QMutex logMutex;
    static std::vector<QString> logBuffer;
Thiago Santini's avatar
Thiago Santini committed
63

64
    QMutexLocker mutex(&logMutex);
65

66
    std::string str = qFormatLogMessage(type, context, msg).prepend(QString("[%1] ").arg(gTimer.elapsed(), 9, 'f', 0, ' ')).append("\n").toStdString();
Thiago Santini's avatar
Thiago Santini committed
67 68

    std::cout << str.c_str();
69 70
    if (logStream.status() == QTextStream::Ok)
        logStream << str.c_str();
Thiago Santini's avatar
Thiago Santini committed
71

Thiago Santini's avatar
Thiago Santini committed
72
    if (gLogWidget) {
73 74
        if (logBuffer.size() > 0) {
            for (auto s = logBuffer.begin(); s != logBuffer.end(); s++)
Thiago Santini's avatar
Thiago Santini committed
75
                QMetaObject::invokeMethod(gLogWidget, "appendMessage", Q_ARG(QString, *s));
76
            logBuffer.clear();
Thiago Santini's avatar
Thiago Santini committed
77
        }
Thiago Santini's avatar
Thiago Santini committed
78
        QMetaObject::invokeMethod(gLogWidget, "appendMessage", Q_ARG(QString, QString(str.c_str())));
Thiago Santini's avatar
Thiago Santini committed
79
    } else
80
        logBuffer.push_back(QString(str.c_str()));
Thiago Santini's avatar
Thiago Santini committed
81

Thiago Santini's avatar
Thiago Santini committed
82
    std::cout.flush();
83
    logStream.flush();
Thiago Santini's avatar
Thiago Santini committed
84 85
}

86 87
QString toQString(QVideoFrame::PixelFormat format)
{
Thiago Santini's avatar
Thiago Santini committed
88
    switch (format) {
89 90 91 92 93 94 95 96 97 98 99 100
    case QVideoFrame::Format_Jpeg:
        return "JPEG";
    case QVideoFrame::Format_YUYV:
        return "YUYV";
    case QVideoFrame::Format_RGB32:
        return "RGB32";
    case QVideoFrame::Format_RGB24:
        return "RGB24";
    case QVideoFrame::Format_Invalid:
        return "Invalid";
    default:
        return "Unsupported";
Thiago Santini's avatar
Thiago Santini committed
101 102 103
    }
}

104 105
QString toQString(QCameraViewfinderSettings setting)
{
Thiago Santini's avatar
Thiago Santini committed
106
    return QString::number(setting.resolution().width())
107 108 109 110 111
        + " x "
        + QString::number(setting.resolution().height())
        + " "
        + toQString(setting.pixelFormat())
        + " @ "
Thiago Santini's avatar
Thiago Santini committed
112
        + QString::number(static_cast<int>(setting.maximumFrameRate())) + " FPS";
Thiago Santini's avatar
Thiago Santini committed
113 114
}

Thiago Santini's avatar
Thiago Santini committed
115 116 117 118 119
QString format(const double v)
{
    return QString("%1").arg(v, 0, 'f', Token::Precision);
}

Thiago Santini's avatar
Thiago Santini committed
120 121 122 123 124 125
QString iniStr(QString str)
{
    return str.remove(QRegExp("[^a-zA-Z\\d\\s]"));
}

// CPU hoggers for testing
126 127 128 129 130 131 132 133
void delay(const int& thMs)
{
    QElapsedTimer timer;
    Timestamp cur = timer.elapsed();
    volatile int a = 0;
    while (timer.elapsed() - cur < thMs)
        a++;
}
Thiago Santini's avatar
Thiago Santini committed
134

135
void loadSoundEffect(const QString& fileName, const QString& exeDir, QSoundEffect& effect)
136 137 138 139 140 141
{
    // TODO: clean this up; QSoundEffect doesn't work with resources currently so
    // we search manually in case this is a release or a development environment.
    // It assumes the build location for the development environment!

    QStringList searchPaths;
142 143 144
    searchPaths << exeDir + "/../EyeRecToo/effects" << exeDir + "/effects";
    bool loaded = false;
    for (int i = 0; i < searchPaths.size(); i++) {
145
        QString target = searchPaths[i] + "/" + fileName;
146 147
        if (QFile(target).exists()) {
            loaded = true;
148 149
            effect.setSource(QUrl::fromLocalFile(target));
            break;
150 151 152 153
        }
    }
    if (!loaded)
        qWarning() << "Failed to load" << fileName;
154
}