Skip to content

common : add fs::path arg handler, fix --log-file on Windows - #30417

Closed
angt wants to merge 1 commit into
ggml-org:masterfrom
angt:common-add-fs-path-arg-handler-fix-log-file-on-windows
Closed

angt wants to merge 1 commit into
ggml-org:masterfrom
angt:common-add-fs-path-arg-handler-fix-log-file-on-windows

Conversation

@angt

@angt angt commented Oct 12, 2026

Copy link
Copy Markdown
Member

Overview

  • Add a generic path handler (env without any conversion, args with u8path()).
  • Fix --log-file on Windows (as a first iteration).

Additional information

Part of the ongoing fs::path modernization series

Requirements

@angt
angt requested a review from a team as a code owner October 12, 2026 20:39
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
@angt
angt force-pushed the common-add-fs-path-arg-handler-fix-log-file-on-windows branch from 8011b02 to 34477a5 Compare October 13, 2026 16:57
Comment thread common/log.h
void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
void common_log_flush (struct common_log * log); // flush all pending log messages
void common_log_set_file (struct common_log * log, const std::filesystem::path & file); // not thread-safe

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The other changes are OK, but here I don't see a reason to change to std::filesystem::path - it results in <filesystem> getting included in a lot of files for no benefit (in the end we still end up using path.c_str() in set_file).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The point of fs::path is that path.c_str() returns the native path (const wchar_t* on Windows), so set_file can pass it directly to _wfopen without any conversion at all.

Also, when the path comes from the env (not from args for now), we keep the native utf16 string as is. With std::string we would have to go utf16 -> utf8 -> utf16, which can lose information on Windows (not common at all, but yes).

So after many iterations I ended up using fs::path to "hide" the special Windows case and avoid these errors in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The point of fs::path is that path.c_str() returns the native path (const wchar_t* on Windows), so set_file can pass it directly to _wfopen without any conversion at all.

Is there a problem to make the conversion in set_file? We would contain the <filesystem> header to the log.cpp file which is OK.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes we can, so we go for u8path() everywhere ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In that case, I can make a new PR, just converting all env to utf8, forgetting about the fs::path direction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I think it will be better.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

or maybe add our own wrapper common_fs ?

I feel like fs-related ops like read/write files, as well as utf8 path handling is currently not very well organized in the code base

@angt angt Oct 14, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think a big part of the issue comes down to using std::string for paths on Windows, they're two different beasts and we end up using fs::path repeatedly and internally just to convert them.

Also, moving entirely to utf8 has proven to be error-prone in my experience. People (myself included) will forget to use u8path() when interacting with the fs.

In modern C++, using fs::path is the way to go to avoid that, no need to convert anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants