Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Common/Cpp/Options/FloatingPointOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ JsonValue FloatingPointCell::to_json() const{
std::string FloatingPointCell::check_validity(double x) const{
const Data& data = *m_data;
if (x < data.m_min_value){
std::ostringstream ss;
return "Value too small: min = " + tostr_default(data.m_min_value) + ", value = " + tostr_default(x);
return std::format("Value too small: min = {}, value = {}", data.m_min_value, x);
}
if (x > data.m_max_value){
std::ostringstream ss;
return "Value too large: max = " + tostr_default(data.m_max_value) + ", value = " + tostr_default(x);
return std::format("Value too large: max = {}, value = {}", data.m_max_value, x);
}
if (std::isnan(x)){
return "Value is NaN.";
Expand Down
2 changes: 1 addition & 1 deletion Common/Cpp/Options/TimeDurationOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ std::string TimeDurationCell<Type>::time_string(const std::string& text) const{
if constexpr (self >= milliseconds){
return duration_to_string(std::chrono::duration_cast<Milliseconds>(value));
}else{
return tostr_u_commas(value.count()) + " " + m_data->m_units;
return std::format("{:L} {}", value.count(), m_data->m_units);
}
}

Expand Down
14 changes: 5 additions & 9 deletions Common/Cpp/Options/TimeExpressionOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ std::string ticks_to_time(double ticks_per_second, int64_t ticks){
const double DAY = HOUR * 24;

std::string str;
str += tostr_u_commas(ticks);
str += std::format("{:L}", ticks);
str += " tick";
// Compute absolute value of the ticks:
uint64_t abs_ticks = 0;
Expand All @@ -44,17 +44,13 @@ std::string ticks_to_time(double ticks_per_second, int64_t ticks){
str += "-";
}
if (abs_ticks < MINUTE * 2){
str += tostr_fixed((double)abs_ticks / SECOND, 3);
str += " seconds";
str += std::format("{:.3f} seconds", (double)abs_ticks / SECOND);
}else if (abs_ticks < HOUR * 2){
str += tostr_fixed((double)abs_ticks / MINUTE, 3);
str += " minutes";
str += std::format("{:.3f} minutes", (double)abs_ticks / MINUTE);
}else if (abs_ticks < DAY * 2){
str += tostr_fixed((double)abs_ticks / HOUR, 3);
str += " hours";
str += std::format("{:.3f} hours", (double)abs_ticks / HOUR);
}else{
str += tostr_fixed((double)abs_ticks / DAY, 3);
str += " days";
str += std::format("{:.3f} days", (double)abs_ticks / DAY);
}
str += ")";
return str;
Expand Down
66 changes: 7 additions & 59 deletions Common/Cpp/PrettyPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@ std::string tostr_padded(size_t digits, uint64_t x){
}
return std::string(digits - str.size(), '0') + str;
}
std::string tostr_u_commas(int64_t x){
// Prints out x with comma separators.

std::string str = std::to_string(x);
std::string out;

const char* ptr = str.c_str();
// len: how many digits, don't count "-" in the negative numbers
size_t len = str.size() - (x < 0);

size_t commas = (len + 2) / 3 - 1;
size_t shift = len - commas * 3 + (x < 0);

while (true){
char ch = *ptr++;
if (ch == '\0')
break;
if (shift == 0){
out += ',';
shift = 3;
}
out += ch;
shift--;
}

return out;
}


inline std::string byte_prefix(size_t index){
Expand Down Expand Up @@ -164,22 +137,6 @@ std::string tostr_bytes(uint64_t bytes){
return tostr_ui_bytes(bytes);
}



std::string tostr_default(double x){
std::ostringstream ss;
ss << x;
return ss.str();
}
std::string tostr_fixed(double x, int precision){
std::ostringstream out;
out << std::setprecision(precision);
out << std::fixed;
out << x;
return out.str();
}


std::string now_to_filestring(){
#if _WIN32 && _MSC_VER
#pragma warning(disable:4996)
Expand Down Expand Up @@ -244,30 +201,21 @@ std::string duration_to_string(std::chrono::milliseconds milliseconds){

uint64_t ticks = milliseconds.count();

std::string str;
if (ticks < MINUTE * 2){
str += tostr_fixed((double)ticks / SECOND, 3);
str += " seconds";
return std::format("{:.3f} seconds", (double)ticks / SECOND);
}else if (ticks < HOUR * 2){
str += tostr_fixed((double)ticks / MINUTE, 3);
str += " minutes";
return std::format("{:.3f} minutes", (double)ticks / MINUTE);
}else if (ticks < DAY * 2){
str += tostr_fixed((double)ticks / HOUR, 3);
str += " hours";
return std::format("{:.3f} hours", (double)ticks / HOUR);
}else if (ticks < WEEK * 2){
str += tostr_fixed((double)ticks / DAY, 3);
str += " days";
return std::format("{:.3f} days", (double)ticks / DAY);
}else if (ticks < YEARS * 2){
str += tostr_fixed((double)ticks / WEEK, 3);
str += " weeks";
return std::format("{:.3f} weeks", (double)ticks / WEEK);
}else if (ticks < MILLION_YEARS){
str += tostr_fixed((double)ticks / YEARS, 3);
str += " years";
return std::format("{:.3f} years", (double)ticks / YEARS);
}else{
str += tostr_fixed((double)ticks / MILLION_YEARS, 3);
str += " million years";
return std::format("{:.3f} million years", (double)ticks / MILLION_YEARS);
}
return str;
}


Expand Down
8 changes: 1 addition & 7 deletions Common/Cpp/PrettyPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@
#include <string>
#include <set>
#include <chrono>
#include <format>

namespace PokemonAutomation{

std::string tostr_padded(size_t digits, uint64_t x);
std::string tostr_u_commas(int64_t x);
std::string tostr_bytes(uint64_t bytes);

// Convert double to string using the default precision on ostream.
std::string tostr_default(double x);
// Convert double to string with fixed precision.
// The precision specifies the number of digits after the decimal point.
std::string tostr_fixed(double x, int precision);

// Format current time to a string to be used as filenames.
// e.g. "20220320-044444408355"
std::string now_to_filestring();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ std::vector<AudioDeviceInfo> AudioDeviceInfo::all_input_devices(){

WallClock end = current_time();
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
global_logger_tagged().log("Done querying audio inputs... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
global_logger_tagged().log(std::format("Done querying audio inputs... {:.3f} seconds", seconds), COLOR_CYAN);

bool show_all_devices = GlobalSettings::instance().AUDIO_PIPELINE->SHOW_ALL_DEVICES;
if (show_all_devices){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AudioOutputDevice : public AudioFloatToStream, private ObjectStreamListene
void set_volume(double volume){
auto scope_check = m_sanitizer.check_scope();
double absolute = convertAudioVolumeFromSlider(volume);
m_logger.log("Volume set to: Slider = " + tostr_default(volume) + " -> Absolute = " + tostr_default(absolute));
m_logger.log(std::format("Volume set to: Slider = {} -> Absolute = {}", volume, absolute));
m_sink.setVolume(absolute);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AudioInputDevice final : public QIODevice{
m_source->start(this);
WallClock end = current_time();
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
logger.log("Done starting audio... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
logger.log(std::format("Done starting audio... {:.3f} seconds", seconds), COLOR_CYAN);
}
~AudioInputDevice(){
if (m_source){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool check_hardware(){
str += "You can continue, but the program may not work correctly.<br>";
str += "(i.e. Increased error rate. Fail to reliably detect shinies, etc...)<br><br>";
str += "(Reason: Base frequency measured at ";
str += QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3));
str += QString::fromStdString(std::format("{:.3f}", specs.base_frequency / 1e9));
str += " GHz which is very slow.)<br><br>";
str += "Recommendation: Use a more powerful computer.";
box.warning(nullptr, "Warning", str);
Expand All @@ -82,10 +82,10 @@ bool check_hardware(){
str += "Threads: " + QString::number(specs.threads) + "<br>";
str += "Sockets: " + QString::number(specs.sockets) + "<br>";
str += "Numa Nodes: " + QString::number(specs.numa_nodes) + "<br>";
str += "Base Frequency: " + QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3)) + " GHz<br>";
str += QString::fromStdString(std::format("Base Frequency: {:.3f} GHz<br>", specs.base_frequency / 1e9));
str += "<br>";
str += "(p-cores + 0.2 * v-cores) * base-frequency = ";
str += QString::fromStdString(tostr_fixed(efreq / 1000000000., 3));
str += QString::fromStdString(std::format("{:.3f}", efreq / 1e9));
str += " GHz<br><br>";
str += "Recommendation: Use a more powerful computer.";
box.warning(nullptr, "Warning", str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,15 @@ std::unique_ptr<AsyncTask> send_all_unsent_reports(Logger& logger, bool allow_pr
QMessageBox::StandardButton button = box.information(
nullptr,
"Error Reporting",
QString::fromStdString(
(reports.size() == 1
? "There is " + tostr_u_commas(reports.size()) + " error report.<br><br>"
"Would you like to help make this program better by sending it to the developers?<br><br>"
: "There are " + tostr_u_commas(reports.size()) + " error reports.<br><br>"
"Would you like to help make this program better by sending them to the developers?<br><br>"
) +
make_text_url(ERROR_PATH_UNSENT, "View unsent error reports.")// + "<br><br>"
// "(You can change )"
),
QString::fromStdString(std::format(
"There {} {:L} error report{}.<br><br>"
"Would you like to help make this program better by sending {} to the developers?<br><br>{}",
(reports.size() == 1 ? "is" : "are"),
reports.size(),
(reports.size() == 1 ? "" : "s"),
(reports.size() == 1 ? "it" : "them"),
make_text_url(ERROR_PATH_UNSENT, "View unsent error reports.")
)),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::StandardButton::Yes
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ std::string StatsTracker::to_str(PrintMode mode) const{
if (!str.empty()){
str += " - ";
}
str += stat.label;
str += ": ";
str += tostr_u_commas(count);
str += std::format("{}: {:L}", stat.label, count);
}
return str;
}
Expand Down
13 changes: 6 additions & 7 deletions SerialPrograms/Source/CommonFramework/Tools/StatAccumulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ double StatAccumulatorI32::stddev() const{

std::string StatAccumulatorI32::dump(const char* units, double divider) const{
divider = 1. / divider;
std::string str;
str += "Count = " + tostr_u_commas(m_count);
str += ", Mean = " + tostr_default(mean() * divider) + units;
str += ", Stddev = " + tostr_default(stddev() * divider) + units;
str += ", Min = " + tostr_default(min() * divider) + units;
str += ", Max = " + tostr_default(max() * divider) + units;
return str;
return std::format("Count = {:L}, Mean = {}{}, Stddev = {}{}, Min = {}{}, Max = {}{}",
m_count,
mean() * divider, units,
stddev() * divider, units,
min() * divider, units,
max() * divider, units);
}
void StatAccumulatorI32::log(Logger& logger, const std::string& label, const char* units, double divider) const{
logger.log(label + ": " + dump(units, divider), COLOR_MAGENTA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,12 @@ const CameraBackend& get_camera_backend(){

std::vector<CameraInfo> get_all_cameras(){
const CameraBackend& backend = get_camera_backend();
// global_logger_tagged().log("Start loading camera list...");
// WallClock start = current_time();
std::vector<CameraInfo> ret = backend.get_all_cameras();
// WallClock end = current_time();
// double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
// global_logger_tagged().log("Done loading camera list... " + tostr_fixed(seconds, 3) + " seconds");
return ret;
}
std::string get_camera_name(const CameraInfo& info){
const CameraBackend& backend = get_camera_backend();
// global_logger_tagged().log("Start reading camera name...");
// WallClock start = current_time();
std::string ret = backend.get_camera_name(info);
// WallClock end = current_time();
// double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
// global_logger_tagged().log("Done reading camera name... " + tostr_fixed(seconds, 3) + " seconds");
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ void GlobalMediaServices::refresh_cameras(){

WallClock end = current_time();
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
global_logger_tagged().log("Done refreshing camera list... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
global_logger_tagged().log(std::format("Done refreshing camera list... {:.3f} seconds", seconds), COLOR_CYAN);
}catch (std::exception& e){
global_logger_tagged().log(
std::string("Refreshing camera list returned exception: ") + e.what(),
std::format("Refreshing camera list returned exception: {}", e.what()),
COLOR_RED
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ bool MemoryUtilizationStats::get_stat(

if (current != 0 && total != 0){
usage = (double)current / total;
stat_text += " (";
stat_text += tostr_fixed(usage * 100, 1);
stat_text += "%)";
stat_text += std::format(" ({:.1f}%)", usage * 100);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ void VideoDisplayWidget::on_key_release(QKeyEvent* event){
OverlayStatSnapshot VideoSourceFPS::get_current(){
double fps = m_parent.m_video_session.fps_source();
return OverlayStatSnapshot{
"Video Source FPS: " + tostr_fixed(fps, 2),
std::format("Video Source FPS: {:.2f}", fps),
fps < 20 ? COLOR_RED : COLOR_WHITE
};
}
OverlayStatSnapshot VideoDisplayFPS::get_current(){
double fps = m_parent.m_video_session.fps_display();
return OverlayStatSnapshot{
"Video Display FPS: " + (fps < 0 ? "???" : tostr_fixed(fps, 2)),
fps >= 0 && fps < 20 ? COLOR_RED : COLOR_WHITE
fps < 0 ? "Video Display FPS: ???" : std::format("Video Display FPS: {:.2f}", fps),
(fps >= 0 && fps < 20) ? COLOR_RED : COLOR_WHITE
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ OverlayStatSnapshot OverlayStatUtilizationPrinter::get_snapshot(const std::strin
color = COLOR_YELLOW;
}
return OverlayStatSnapshot{
label + " " + tostr_fixed(utilization * 100, 2) + " %",
std::format("{} {:.2f} %", label, utilization * 100),
color
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ void AudioPerSpectrumDetectorBase::throw_if_no_sound(std::chrono::milliseconds m

void AudioPerSpectrumDetectorBase::log_results(){
if (m_last_timestamp != WallClock::min()){
m_logger.log(m_audio_name + " detected! Error Coefficient = " + tostr_default(m_lowest_error), COLOR_BLUE);
m_logger.log(std::format("{} detected! Error Coefficient = {}", m_audio_name, m_lowest_error), COLOR_BLUE);
}else{
m_logger.log(m_audio_name + " not detected. Error Coefficient = " + tostr_default(m_lowest_error), COLOR_PURPLE);
m_logger.log(std::format("{} not detected. Error Coefficient = {}", m_audio_name, m_lowest_error), COLOR_PURPLE);
}

#if 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void StringMatchResult::log(Logger& logger, double max_log10p, const std::string
if (results.size() == 1){
auto iter = results.begin();
str += iter->second.to_str();
str += " (log10p = " + tostr_default(iter->first) +")";
str += std::format(" (log10p = {})", iter->first);
}else{
str += "Multiple Candidates =>\n";
size_t printed = 0;
Expand All @@ -62,7 +62,7 @@ void StringMatchResult::log(Logger& logger, double max_log10p, const std::string
str += " (" + std::to_string(results.size() - 10) + " more...)\n";
break;
}
str += " " + tostr_default(item.first) + " : " + item.second.to_str() + "\n";
str += std::format(" {} : {}\n", item.first, item.second.to_str());
printed++;
}
}
Expand Down
Loading