GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmLogTime.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4** All rights reserved.
5**
6** This file is part of the "GeMA" software. It's use should respect
7** the terms in the license agreement that can be found together
8** with this source code.
9** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11** A PARTICULAR PURPOSE.
12**
13************************************************************************/
14
24#ifndef _GEMA_LOG_TIME_H_
25#define _GEMA_LOG_TIME_H_
26
27#include "gmLog.h"
28#include <QElapsedTimer>
29
30
37{
38public:
39 GmTimeMsg(const GmLogCategory& logger, QString msg) : _logger(logger), _msg(msg)
40 {
41 if(logger.isEnabled(GM_TIME))
42 {
43 _timer.start();
44 _nestLevel++;
45 }
46 }
47
49 {
50 if(_logger.isEnabled(GM_TIME))
51 {
52 qint64 t = _timer.elapsed();
53 double ts = t / 1000.0;
54 QString aux;
55 if(ts >= 100 || ts <= 0.01)
56 aux = QString(" (%1)").arg(formatTime(ts, _secondIndex));
57 gmTimeMsg(_logger, QObject::tr("%1 [%2] finished in %3 s%4").arg(_msg).arg(_nestLevel).arg(ts).arg(aux));
58 _nestLevel--;
59 }
60 }
61
63 qint64 elapsed() const { return _timer.elapsed(); }
64
65 static QString formatTime(double time, QString unit, bool useConstSizeUnit = false);
66 static QString formatTime(double time, int unitIndex, bool useConstSizeUnit = false);
67
69 static int unitIndex(QString unit) { return _timeUnits.indexOf(unit); }
70
72 static QString indexFmtUnit(int unitIndex) { return _fmtTimeUnits[unitIndex]; }
73
74private:
75 static int _nestLevel;
76
77 static const QStringList _timeUnits;
78 static const QStringList _fmtTimeUnits;
79 static const QVector<double> _timeFactors;
80 static const int _secondIndex;
81
85};
86
89{
90public:
91 GmTimeAdder(qint64* counter) : _counter(counter)
92 {
93 _timer.start();
94 }
96 {
98 }
99
100private:
102 qint64* _counter;
103};
104
105#endif
106
Class representing a category with multiple logging levels.
Definition gmLog.h:58
bool isEnabled(GmLogLevel level) const
Returns true if messages for this log level are enabled, false otherwise.
Definition gmLog.h:64
RAII class used to add the elapsed time to a counter received by reference. Counter is incremented in...
Definition gmLogTime.h:89
QElapsedTimer _timer
Timer used to measure elapsed time.
Definition gmLogTime.h:101
qint64 * _counter
The counter where the elapsed time will be added to.
Definition gmLogTime.h:102
RAII object used to measure the elapsed time of an operation.
Definition gmLogTime.h:37
QElapsedTimer _timer
Timer used to measure elapsed time.
Definition gmLogTime.h:82
static int unitIndex(QString unit)
Returns the index of the unit in the format time table of known units, or -1 if not found.
Definition gmLogTime.h:69
const GmLogCategory & _logger
The logger used to emmit messages.
Definition gmLogTime.h:83
static QString indexFmtUnit(int unitIndex)
Returns the formatted unit (always with 3 characters) for the given unit index returned bu unitIndex(...
Definition gmLogTime.h:72
static const int _secondIndex
The index for the second entry in _timeUnits.
Definition gmLogTime.h:80
static int _nestLevel
A global value storing the current nesting level (GmTimeMsg objects created in the scope of another o...
Definition gmLogTime.h:75
qint64 elapsed() const
Returns the elapsed tim so far in ms.
Definition gmLogTime.h:63
QString _msg
Message title.
Definition gmLogTime.h:84
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of support functions and macros for information logging.
@ GM_TIME
Reporting timing information. New category so as to be easy to disable all timing messages for tests ...
Definition gmLog.h:43
qint64 nsecsElapsed() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const