Telegram Group & Telegram Channel
Как упростить дебаг через std::format и std::source_location

Когда вы отлаживаете сложный баг, бывает сложно быстро понять, где и почему произошла ошибка. С C++20 стало гораздо проще автоматизировать логирование и сделать его по-настоящему информативным.

Вот пример:


#include <iostream>
#include <format>
#include <source_location>

void log(const std::string& message,
const std::source_location location = std::source_location::current())
{
std::cout << std::format("[{}:{} - {}] {}\n",
location.file_name(),
location.line(),
location.function_name(),
message);
}

int divide(int a, int b)
{
if (b == 0) {
log("Попытка деления на ноль");
return 0;
}
return a / b;
}


📌 Этот код выведет:


[main.cpp:16 - divide] Попытка деления на ноль


Вы больше не пишете руками __FILE__, __LINE__ и __func__. Всё это делает std::source_location. А с std::format — красиво и читаемо.

➡️ @cpp_geek



tg-me.com/cpp_geek/293
Create:
Last Update:

Как упростить дебаг через std::format и std::source_location

Когда вы отлаживаете сложный баг, бывает сложно быстро понять, где и почему произошла ошибка. С C++20 стало гораздо проще автоматизировать логирование и сделать его по-настоящему информативным.

Вот пример:


#include <iostream>
#include <format>
#include <source_location>

void log(const std::string& message,
const std::source_location location = std::source_location::current())
{
std::cout << std::format("[{}:{} - {}] {}\n",
location.file_name(),
location.line(),
location.function_name(),
message);
}

int divide(int a, int b)
{
if (b == 0) {
log("Попытка деления на ноль");
return 0;
}
return a / b;
}


📌 Этот код выведет:


[main.cpp:16 - divide] Попытка деления на ноль


Вы больше не пишете руками __FILE__, __LINE__ и __func__. Всё это делает std::source_location. А с std::format — красиво и читаемо.

➡️ @cpp_geek

BY C++ geek




Share with your friend now:
tg-me.com/cpp_geek/293

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

That growth environment will include rising inflation and interest rates. Those upward shifts naturally accompany healthy growth periods as the demand for resources, products and services rise. Importantly, the Federal Reserve has laid out the rationale for not interfering with that natural growth transition.It's not exactly a fad, but there is a widespread willingness to pay up for a growth story. Classic fundamental analysis takes a back seat. Even negative earnings are ignored. In fact, positive earnings seem to be a limiting measure, producing the question, "Is that all you've got?" The preference is a vision of untold riches when the exciting story plays out as expected.

telegram from us


Telegram C++ geek
FROM USA