Skip to content

Commit

Permalink
common: Added ScopyBenchmark class.
Browse files Browse the repository at this point in the history
Signed-off-by: andreidanila1 <andrei.danila@analog.com>
  • Loading branch information
andreidanila1 committed Jan 9, 2025
1 parent 72ef3a8 commit f74da34
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
51 changes: 51 additions & 0 deletions common/include/common/scopybenchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2025 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#ifndef SCOPYBENCHMARK_H
#define SCOPYBENCHMARK_H

#include <QElapsedTimer>
#include <QString>
#include "scopy-common_export.h"

#define CONSOLE_LOG(logger, msg) logger.log(msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)
#define FILE_LOG(logger, msg, path) logger.log(path, msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)

namespace scopy {
class SCOPY_COMMON_EXPORT ScopyBenchmark
{
public:
ScopyBenchmark();
~ScopyBenchmark();

void startTimer();
void restartTimer();

void log(const QString &msg, const char *function, const char *file, int line);
void log(const QString &filePath, const QString &msg, const char *function, const char *file, int line);

private:
QElapsedTimer m_timer;
};

} // namespace scopy

#endif // SCOPYBENCHMARK_H
53 changes: 53 additions & 0 deletions common/src/scopybenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2025 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#include "scopybenchmark.h"
#include <QDate>
#include <QFile>
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(CAT_BENCHMARK, "Benchmark")
using namespace scopy;

ScopyBenchmark::ScopyBenchmark() {}

ScopyBenchmark::~ScopyBenchmark() {}

void ScopyBenchmark::startTimer() { m_timer.start(); }

void ScopyBenchmark::restartTimer() { m_timer.restart(); }

void ScopyBenchmark::log(const QString &msg, const char *function, const char *file, int line)
{
QMessageLogger(file, line, function).info(CAT_BENCHMARK) << function << msg << m_timer.elapsed() << "ms";
}

void ScopyBenchmark::log(const QString &filePath, const QString &msg, const char *function, const char *file, int line)
{
QFile f(filePath);
if(f.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream stream(&f);
stream << QDateTime::currentDateTime().toString("dd:MM:yyyy hh:mm:ss.zzz") << "\t" << file << ":"
<< line << "\t" << function << "\t" << msg << "\t" << m_timer.elapsed() << "\t"
<< "ms"
<< "\n";
}
}

0 comments on commit f74da34

Please sign in to comment.