Skip to content
Closed
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
24 changes: 5 additions & 19 deletions source/MRMesh/MRLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,12 @@ void Logger::removeSink( const spdlog::sink_ptr& sink )

std::filesystem::path Logger::getLogFileName() const
{
if ( !logger_ )
return {};

for ( const auto& sink : logger_->sinks() )
{
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::rotating_file_sink_mt>( sink ) )
return r->filename();
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::rotating_file_sink_st>( sink ) )
return r->filename();
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::daily_file_sink_mt>( sink ) )
return r->filename();
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::daily_file_sink_st>( sink ) )
return r->filename();
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::basic_file_sink_mt>( sink ) )
return r->filename();
if ( auto r = std::dynamic_pointer_cast<spdlog::sinks::basic_file_sink_st>( sink ) )
return r->filename();
}
return logFilePath_;
}

return {};
void Logger::setLogFilePath( const std::filesystem::path& path )
{
logFilePath_ = path;
}

Logger::Logger()
Expand Down
4 changes: 4 additions & 0 deletions source/MRMesh/MRLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ class MR_BIND_IGNORE Logger

/// return filename of first found file sink, if there is no one, returns {}
MRMESH_API std::filesystem::path getLogFileName() const;

/// set the current log file path (used when file sink is created)
MRMESH_API void setLogFilePath( const std::filesystem::path& path );
private:
Logger();
std::shared_ptr<spdlog::logger> logger_;
std::filesystem::path logFilePath_;
};

/// \}
Expand Down
12 changes: 12 additions & 0 deletions source/MRMesh/MRSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,17 @@ void setupLoggerByDefault()
std::time_t t = std::chrono::system_clock::to_time_t( now );
auto fileName = GetTempDirectory();
fileName /= "Logs";
// ensure Logs directory exists (spdlog does not create parent dirs)
{
std::error_code ec;
if ( !std::filesystem::is_directory( fileName, ec ) )
{
ec.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::filesystem::create_directories clears ec if no errors occur.

std::filesystem::create_directories( fileName, ec );
if ( ec )
spdlog::error( "Failed to create logs directory {}: {}", utf8string( fileName ), systemToUtf8( ec.message() ) );
}
}
removeOldLogs( fileName );

fileName /= fmt::format( "MRLog_{:%Y-%m-%d_%H-%M-%S}_{}.txt", LocaltimeOrZero( t ),
Expand All @@ -616,6 +627,7 @@ void setupLoggerByDefault()
file_sink->set_level( minLevel );
file_sink->set_pattern( Logger::instance().getDefaultPattern() );
Logger::instance().addSink( file_sink );
Logger::instance().setLogFilePath( fileName );

#ifdef _WIN32
auto msvc_sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
Expand Down
Loading