forked from Chatterino/chatterino2
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Chatterino/chatterino2 into danke…
…rino
- Loading branch information
Showing
9 changed files
with
167 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,114 @@ | ||
#include "DebugCount.hpp" | ||
#include "util/DebugCount.hpp" | ||
|
||
#include "common/UniqueAccess.hpp" | ||
|
||
#include <QLocale> | ||
#include <QStringBuilder> | ||
|
||
#include <map> | ||
|
||
namespace { | ||
|
||
using namespace chatterino; | ||
|
||
struct Count { | ||
int64_t value = 0; | ||
DebugCount::Flags flags = DebugCount::Flag::None; | ||
}; | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
UniqueAccess<std::map<QString, Count>> COUNTS; | ||
|
||
} // namespace | ||
|
||
namespace chatterino { | ||
|
||
UniqueAccess<QMap<QString, int64_t>> DebugCount::counts_; | ||
void DebugCount::configure(const QString &name, Flags flags) | ||
{ | ||
auto counts = COUNTS.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->emplace(name, Count{.flags = flags}); | ||
} | ||
else | ||
{ | ||
it->second.flags = flags; | ||
} | ||
} | ||
|
||
void DebugCount::set(const QString &name, const int64_t &amount) | ||
{ | ||
auto counts = COUNTS.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->emplace(name, Count{amount}); | ||
} | ||
else | ||
{ | ||
it->second.value = amount; | ||
} | ||
} | ||
|
||
void DebugCount::increase(const QString &name, const int64_t &amount) | ||
{ | ||
auto counts = COUNTS.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->emplace(name, Count{amount}); | ||
} | ||
else | ||
{ | ||
it->second.value += amount; | ||
} | ||
} | ||
|
||
void DebugCount::decrease(const QString &name, const int64_t &amount) | ||
{ | ||
auto counts = COUNTS.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->emplace(name, Count{-amount}); | ||
} | ||
else | ||
{ | ||
it->second.value -= amount; | ||
} | ||
} | ||
|
||
QString DebugCount::getDebugText() | ||
{ | ||
#if QT_VERSION > QT_VERSION_CHECK(5, 13, 0) | ||
static const QLocale locale(QLocale::English); | ||
#else | ||
static QLocale locale(QLocale::English); | ||
#endif | ||
|
||
auto counts = COUNTS.access(); | ||
|
||
QString text; | ||
for (const auto &[key, count] : *counts) | ||
{ | ||
QString formatted; | ||
if (count.flags.has(Flag::DataSize)) | ||
{ | ||
formatted = locale.formattedDataSize(count.value); | ||
} | ||
else | ||
{ | ||
formatted = locale.toString(static_cast<qlonglong>(count.value)); | ||
} | ||
|
||
text += key % ": " % formatted % '\n'; | ||
} | ||
return text; | ||
} | ||
|
||
} // namespace chatterino |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,38 @@ | ||
#pragma once | ||
|
||
#include "common/UniqueAccess.hpp" | ||
#include "common/FlagsEnum.hpp" | ||
|
||
#include <QMap> | ||
#include <QString> | ||
|
||
#include <mutex> | ||
#include <typeinfo> | ||
|
||
namespace chatterino { | ||
|
||
class DebugCount | ||
{ | ||
public: | ||
static void increase(const QString &name) | ||
{ | ||
auto counts = counts_.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->insert(name, 1); | ||
} | ||
else | ||
{ | ||
reinterpret_cast<int64_t &>(it.value())++; | ||
} | ||
} | ||
enum class Flag : uint16_t { | ||
None = 0, | ||
/// The value is a data size in bytes | ||
DataSize = 1 << 0, | ||
}; | ||
using Flags = FlagsEnum<Flag>; | ||
|
||
static void set(const QString &name, const int64_t &amount) | ||
{ | ||
auto counts = counts_.access(); | ||
static void configure(const QString &name, Flags flags); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->insert(name, amount); | ||
} | ||
else | ||
{ | ||
reinterpret_cast<int64_t &>(it.value()) = amount; | ||
} | ||
} | ||
static void set(const QString &name, const int64_t &amount); | ||
|
||
static void increase(const QString &name, const int64_t &amount) | ||
static void increase(const QString &name, const int64_t &amount); | ||
static void increase(const QString &name) | ||
{ | ||
auto counts = counts_.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->insert(name, amount); | ||
} | ||
else | ||
{ | ||
reinterpret_cast<int64_t &>(it.value()) += amount; | ||
} | ||
DebugCount::increase(name, 1); | ||
} | ||
|
||
static void decrease(const QString &name, const int64_t &amount); | ||
static void decrease(const QString &name) | ||
{ | ||
auto counts = counts_.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->insert(name, -1); | ||
} | ||
else | ||
{ | ||
reinterpret_cast<int64_t &>(it.value())--; | ||
} | ||
} | ||
static void decrease(const QString &name, const int64_t &amount) | ||
{ | ||
auto counts = counts_.access(); | ||
|
||
auto it = counts->find(name); | ||
if (it == counts->end()) | ||
{ | ||
counts->insert(name, -amount); | ||
} | ||
else | ||
{ | ||
reinterpret_cast<int64_t &>(it.value()) -= amount; | ||
} | ||
} | ||
|
||
static QString getDebugText() | ||
{ | ||
auto counts = counts_.access(); | ||
|
||
QString text; | ||
for (auto it = counts->begin(); it != counts->end(); it++) | ||
{ | ||
text += it.key() + ": " + QString::number(it.value()) + "\n"; | ||
} | ||
return text; | ||
} | ||
|
||
QString toString() | ||
{ | ||
return ""; | ||
DebugCount::decrease(name, 1); | ||
} | ||
|
||
private: | ||
static UniqueAccess<QMap<QString, int64_t>> counts_; | ||
static QString getDebugText(); | ||
}; | ||
|
||
} // namespace chatterino |
This file contains 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
This file contains 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
Oops, something went wrong.