Skip to content

Commit

Permalink
logger: Use casting to seconds when logging time
Browse files Browse the repository at this point in the history
Using fmt 10, `time_point<system_clock>` contains seconds represented as a floating-point number. In order to drop the decimal places, we need to explicitly do the conversion. This is also conformant with the currently used fmt-9.1 docs: https://fmt.dev/9.1.0/syntax.html#chrono-format-specifications.
  • Loading branch information
jan-kolarik authored and kontura committed Jun 28, 2023
1 parent ae868a9 commit 5af8dd2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions libdnf5/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5 {

using namespace std::chrono;

void Logger::log_line(Level level, const std::string & message) noexcept {
write(std::chrono::system_clock::now(), getpid(), level, message);
write(system_clock::now(), getpid(), level, message);
}


void StringLogger::write(
const std::chrono::time_point<std::chrono::system_clock> & time,
pid_t pid,
Level level,
const std::string & message) noexcept {
const time_point<system_clock> & time, pid_t pid, Level level, const std::string & message) noexcept {
try {
write(fmt::format("{:%FT%T%z} [{}] {} {}\n", time, pid, level_to_cstr(level), message).c_str());
write(fmt::format("{:%FT%T%z} [{}] {} {}\n", time_point_cast<seconds>(time), pid, level_to_cstr(level), message)
.c_str());
} catch (const std::exception & e) {
write("Failed to format: ");
write(message.c_str());
Expand Down

0 comments on commit 5af8dd2

Please sign in to comment.