@@ -338,16 +338,39 @@ namespace logit {
338338
339339 const std::string base = time_shield::to_iso8601_date (m_current_date_ts);
340340 const std::string dir = get_directory_path ();
341- # if defined(_WIN32)
342- const std::string cur = dir + " \\ " + base + " .log" ;
341+ std::string rotated_str;
342+ # if __cplusplus >= 201703L
343+ # if defined(_WIN32)
344+ fs::path cur = (fs::u8path (dir) / (base + " .log" )).lexically_normal ();
345+ fs::path rotated = fs::u8path (make_rotated_name (base, dir)).lexically_normal ();
346+ # else
347+ fs::path cur = (fs::path (dir) / (base + " .log" )).lexically_normal ();
348+ fs::path rotated = fs::path (make_rotated_name (base, dir)).lexically_normal ();
349+ # endif
350+ std::error_code ec;
351+ fs::rename (cur, rotated, ec);
352+ if (ec) {
353+ throw std::runtime_error (" Failed to rename log file: " + ec.message ());
354+ }
355+ # if defined(_WIN32)
356+ rotated_str = rotated.u8string ();
357+ # else
358+ rotated_str = rotated.string ();
359+ # endif
343360# else
361+ # if defined(_WIN32)
362+ const std::string cur = dir + " \\ " + base + " .log" ;
363+ # else
344364 const std::string cur = dir + " /" + base + " .log" ;
345- # endif
346- std::string rotated = make_rotated_name (base, dir);
347- # if defined(_WIN32)
348- std::rename (utf8_to_ansi (cur).c_str (), utf8_to_ansi (rotated).c_str ());
349- # else
350- std::rename (cur.c_str (), rotated.c_str ());
365+ # endif
366+ rotated_str = make_rotated_name (base, dir);
367+ # if defined(_WIN32)
368+ if (std::rename (utf8_to_ansi (cur).c_str (), utf8_to_ansi (rotated_str).c_str ()) != 0 ) {
369+ # else
370+ if (std::rename (cur.c_str (), rotated_str.c_str ()) != 0 ) {
371+ # endif
372+ throw std::runtime_error (" Failed to rename log file" );
373+ }
351374# endif
352375
353376 open_log_file (m_current_date_ts);
@@ -359,9 +382,9 @@ namespace logit {
359382 m_compressor.reset (new detail::CompressionWorker (
360383 m_config.compress , m_config.compress_level , m_config.external_cmd ));
361384 }
362- m_compressor->enqueue (rotated );
385+ m_compressor->enqueue (rotated_str );
363386 } else {
364- detail::compress_file (m_config.compress , rotated , m_config.compress_level , m_config.external_cmd );
387+ detail::compress_file (m_config.compress , rotated_str , m_config.compress_level , m_config.external_cmd );
365388 }
366389 }
367390
0 commit comments