Created
February 12, 2019 11:29
-
-
Save ccentauri/76712e1cba76743d13cf72da9d692203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <main/jni/src/log/log.h> | |
#include <main/jni/src/security/security_support.h> | |
#include "timing.h" | |
void TimingUtil::addSplit(std::string tag) { | |
_vectorTime.push_back(now()); | |
_vectorTags.push_back(tag); | |
} | |
class LogItem { | |
public: | |
std::string message; | |
long double diff; | |
bool operator()(LogItem *i, LogItem *j) { return (i->diff > j->diff); } | |
} logItem; | |
void TimingUtil::dumpToLog() { | |
std::string time, tag; | |
std::vector<LogItem *> items; | |
for (int i = 0; i < _vectorTime.size(); i++) { | |
LogItem *item = new LogItem(); | |
time = std::to_string(_vectorTime[i] - _timeStart); | |
tag = _vectorTags[i]; | |
if (i % 2 != 0) { | |
long double diff = _vectorTime[i] - _vectorTime[i - 1]; | |
item->message = std::string(SPLIT_TABS) + tag + ": " + std::to_string(diff) + "ms"; | |
item->diff = diff; | |
} | |
items.push_back(item); | |
} | |
std::sort(items.begin(), items.end(), logItem); | |
for (int i = 0; i < items.size(); i++) { | |
if (items[i]->message != "")log(std::to_string(i + 1) + ". " + items[i]->message); | |
} | |
} | |
void TimingUtil::log(std::string message) { | |
if (LOG_ENABLED)mcbp_core::Log::instance()->d(getAdditionalTabs() + message); | |
} | |
void TimingUtil::printHeader() { | |
log(_tag); | |
log(" "); | |
} | |
void TimingUtil::printFooter() { | |
long double timeTotal = _timeEnd - _timeStart; | |
log(" "); | |
log("Total running time: " + std::to_string(timeTotal) + "ms"); | |
log(SPLIT_DIVIDER); | |
log(" "); | |
} | |
void TimingUtil::start() { | |
printHeader(); | |
_timeStart = now(); | |
} | |
void TimingUtil::end() { | |
_timeEnd = now(); | |
dumpToLog(); | |
printFooter(); | |
} | |
TimingUtil::TimingUtil(const std::string &tag) : _tag(tag) {} | |
TimingUtil::TimingUtil(const std::string &tag, const int additionalTabs) { | |
this->_tag = tag; | |
this->_additionalTabs = additionalTabs; | |
} | |
std::string TimingUtil::getTagWith(std::string text) { | |
return _tag + text; | |
} | |
long double TimingUtil::now() { | |
return chrono_second(std::chrono::system_clock::now().time_since_epoch()).count(); | |
} | |
std::string TimingUtil::getAdditionalTabs() { | |
std::string result = std::string(); | |
for (int i = 0; i < _additionalTabs; i++) { | |
result.append("\t"); | |
} | |
return result; | |
} | |
std::string TimingUtil::boolToString(bool b) { | |
return b ? std::string("true") : std::string("false"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment