From 8acdd51bb5c0989bad0a14562bdf10e3de235891 Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:29:13 +0000 Subject: [PATCH 01/15] Make printing thread safe --- src/vcpkg/base/messages.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/vcpkg/base/messages.cpp b/src/vcpkg/base/messages.cpp index ccd10c9431..942c3d340b 100644 --- a/src/vcpkg/base/messages.cpp +++ b/src/vcpkg/base/messages.cpp @@ -307,9 +307,12 @@ namespace vcpkg::msg { if (sv.empty()) return; + static std::mutex mtx; + if (is_console) { WORD original_color = 0; + std::lock_guard lck(mtx); if (c != Color::none) { CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info{}; @@ -340,6 +343,7 @@ namespace vcpkg::msg { const char* pointer = sv.data(); ::size_t size = sv.size(); + std::lock_guard lck(mtx); while (size != 0) { @@ -365,7 +369,7 @@ namespace vcpkg::msg return write_unlocalized_text_impl(c, sv, stderr_handle, stderr_is_console); } #else - static void write_all(const char* ptr, size_t to_write, int fd) + static void write_all(const char* ptr, size_t to_write, int fd, std::unique_lock& lck) { while (to_write != 0) { @@ -373,6 +377,7 @@ namespace vcpkg::msg if (written == -1) { ::fprintf(stderr, "[DEBUG] Failed to print to stdout: %d\n", errno); + lck.unlock(); std::abort(); } ptr += written; @@ -383,23 +388,25 @@ namespace vcpkg::msg static void write_unlocalized_text_impl(Color c, StringView sv, int fd, bool is_a_tty) { static constexpr char reset_color_sequence[] = {'\033', '[', '0', 'm'}; + static std::mutex mtx; if (sv.empty()) return; + std::unique_lock lck(mtx); bool reset_color = false; if (is_a_tty && c != Color::none) { reset_color = true; const char set_color_sequence[] = {'\033', '[', '9', static_cast(c), 'm'}; - write_all(set_color_sequence, sizeof(set_color_sequence), fd); + write_all(set_color_sequence, sizeof(set_color_sequence), fd, lck); } - write_all(sv.data(), sv.size(), fd); + write_all(sv.data(), sv.size(), fd, lck); if (reset_color) { - write_all(reset_color_sequence, sizeof(reset_color_sequence), fd); + write_all(reset_color_sequence, sizeof(reset_color_sequence), fd, lck); } } From 834af9ee49de8b62f48b71d1a908a459212dfb7f Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sat, 16 Dec 2023 18:05:29 +0000 Subject: [PATCH 02/15] [WIP] Add Console class --- include/vcpkg/base/fwd/system.console.h | 8 ++++ include/vcpkg/base/messages.h | 26 +++++----- include/vcpkg/base/system.console.h | 49 +++++++++++++++++++ src/vcpkg/base/messages.cpp | 47 +----------------- src/vcpkg/base/system.console.cpp | 63 +++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 include/vcpkg/base/fwd/system.console.h create mode 100644 include/vcpkg/base/system.console.h create mode 100644 src/vcpkg/base/system.console.cpp diff --git a/include/vcpkg/base/fwd/system.console.h b/include/vcpkg/base/fwd/system.console.h new file mode 100644 index 0000000000..54ba2e11e4 --- /dev/null +++ b/include/vcpkg/base/fwd/system.console.h @@ -0,0 +1,8 @@ +#pragma once + +namespace vcpkg +{ + class Console; + extern Console& std_out; + extern Console& std_error; +} diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index a938002432..f70486e045 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -185,22 +186,17 @@ namespace vcpkg::msg return detail::format_to_impl(s, m.index, args.arg()...); } - inline void println() { msg::write_unlocalized_text_to_stdout(Color::none, "\n"); } - - inline void print(Color c, const LocalizedString& s) { msg::write_unlocalized_text_to_stdout(c, s); } - inline void print(const LocalizedString& s) { msg::write_unlocalized_text_to_stdout(Color::none, s); } - inline void println(Color c, const LocalizedString& s) + inline void print(Color c, const LocalizedString& s) { std_out.print(c, s); } + inline void print(const LocalizedString& s) { std_out.print(Color::none, s); } + inline void println(Color c, LocalizedString&& s) { - msg::write_unlocalized_text_to_stdout(c, s); - msg::write_unlocalized_text_to_stdout(Color::none, "\n"); + std_out.print(c, s.append_raw('\n')); } - inline void println(const LocalizedString& s) + inline void println(LocalizedString&& s) { - msg::write_unlocalized_text_to_stdout(Color::none, s); - msg::write_unlocalized_text_to_stdout(Color::none, "\n"); + std_out.print(Color::none, s.append_raw('\n')); } - - [[nodiscard]] LocalizedString format_error(const LocalizedString& s); + inline [[nodiscard]] LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } template [[nodiscard]] LocalizedString format_error(VCPKG_DECL_MSG_ARGS) { @@ -208,7 +204,7 @@ namespace vcpkg::msg msg::format_to(s, VCPKG_EXPAND_MSG_ARGS); return s; } - void println_error(const LocalizedString& s); + inline void println_error(const LocalizedString& s) { println(Color::error, format_error(s)); } template void println_error(VCPKG_DECL_MSG_ARGS) { @@ -217,7 +213,7 @@ namespace vcpkg::msg println(Color::error, s); } - [[nodiscard]] LocalizedString format_warning(const LocalizedString& s); + inline [[nodiscard]] LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } template [[nodiscard]] LocalizedString format_warning(VCPKG_DECL_MSG_ARGS) { @@ -225,7 +221,7 @@ namespace vcpkg::msg msg::format_to(s, VCPKG_EXPAND_MSG_ARGS); return s; } - void println_warning(const LocalizedString& s); + inline void println_warning(const LocalizedString& s) { println(Color::warning, format_warning(s)); } template void println_warning(VCPKG_DECL_MSG_ARGS) { diff --git a/include/vcpkg/base/system.console.h b/include/vcpkg/base/system.console.h new file mode 100644 index 0000000000..30604332a0 --- /dev/null +++ b/include/vcpkg/base/system.console.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +#include + +#include + +#include + +namespace vcpkg +{ + class Console + { + public: + explicit Console(int fd) noexcept : fd(fd), is_terminal(::isatty(fd) == 1) {} + ~Console() = default; + + void print(Color color, StringView text) + { + std::lock_guard lck{mtx}; + print_unlocked(color, text); + } + + void print_lines(View> lines); + void println(Color color, StringView text) + { + std::lock_guard lck{mtx}; + print_unlocked(color, text); + print_unlocked(Color::none, "\n"); + } + void println(Color color, std::string&& text) + { + text.push_back('\n'); + print(color, text); + } + + private: + void print_unlocked(Color color, StringView text) noexcept; + void write(const char* text, size_t count) noexcept; + + std::mutex mtx; + int fd; + bool is_terminal; + }; +} // namespace vcpkg + diff --git a/src/vcpkg/base/messages.cpp b/src/vcpkg/base/messages.cpp index 942c3d340b..42d9317cde 100644 --- a/src/vcpkg/base/messages.cpp +++ b/src/vcpkg/base/messages.cpp @@ -369,47 +369,7 @@ namespace vcpkg::msg return write_unlocalized_text_impl(c, sv, stderr_handle, stderr_is_console); } #else - static void write_all(const char* ptr, size_t to_write, int fd, std::unique_lock& lck) - { - while (to_write != 0) - { - auto written = ::write(fd, ptr, to_write); - if (written == -1) - { - ::fprintf(stderr, "[DEBUG] Failed to print to stdout: %d\n", errno); - lck.unlock(); - std::abort(); - } - ptr += written; - to_write -= written; - } - } - - static void write_unlocalized_text_impl(Color c, StringView sv, int fd, bool is_a_tty) - { - static constexpr char reset_color_sequence[] = {'\033', '[', '0', 'm'}; - static std::mutex mtx; - - if (sv.empty()) return; - - std::unique_lock lck(mtx); - bool reset_color = false; - if (is_a_tty && c != Color::none) - { - reset_color = true; - - const char set_color_sequence[] = {'\033', '[', '9', static_cast(c), 'm'}; - write_all(set_color_sequence, sizeof(set_color_sequence), fd, lck); - } - - write_all(sv.data(), sv.size(), fd, lck); - - if (reset_color) - { - write_all(reset_color_sequence, sizeof(reset_color_sequence), fd, lck); - } - } - + void write_unlocalized_text_to_stdout(Color c, StringView sv) { static bool is_a_tty = ::isatty(STDOUT_FILENO); @@ -502,9 +462,4 @@ namespace vcpkg::msg return nullopt; } - - LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } - void println_error(const LocalizedString& s) { println(Color::error, format_error(s)); } - LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } - void println_warning(const LocalizedString& s) { println(Color::warning, format_warning(s)); } } diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp new file mode 100644 index 0000000000..4be6529d84 --- /dev/null +++ b/src/vcpkg/base/system.console.cpp @@ -0,0 +1,63 @@ +#include +#include +#include + +namespace vcpkg +{ + void Console::print_lines(View> lines) + { + std::lock_guard lck{mtx}; + + for (auto&& line : lines) + { + print_unlocked(line.first, line.second); + } + } + + void Console::print_unlocked(Color c, StringView sv) noexcept + { + static constexpr char reset_color_sequence[] = {'\033', '[', '0', 'm'}; + + if (sv.empty()) return; + + std::lock_guard lck{mtx}; + bool reset_color = false; + // Only write color sequence if file descriptor is a terminal + if (is_terminal && c != Color::none) + { + reset_color = true; + + const char set_color_sequence[] = {'\033', '[', '9', static_cast(c), 'm'}; + write(set_color_sequence, sizeof(set_color_sequence)); + } + + write(sv.data(), sv.size()); + + if (reset_color) + { + write(reset_color_sequence, sizeof(reset_color_sequence)); + } + } + + void Console::write(const char* text, size_t count) noexcept + { + while (count != 0) + { + auto written = ::write(fd, text, count); + if (written == -1) + { + ::fprintf(stderr, "[DEBUG] Failed to print to stdout: %d\n", errno); + std::abort(); + } + text += written; + count -= written; + } + } + + Console std_out_instance(STDOUT_FILENO); + Console std_error_instance(STDERR_FILENO); + + Console& std_out = std_out_instance; + Console& std_error = std_out_instance; +} // namespace vcpkg + From bb8f6b6781cda21e860c27ac70962d4bbbcd04f5 Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 20:15:33 +0200 Subject: [PATCH 03/15] Finish Console implementation --- include/vcpkg/base/system.console.h | 27 ++++++-- src/vcpkg/base/messages.cpp | 100 +--------------------------- src/vcpkg/base/system.console.cpp | 92 +++++++++++++++++++++++-- 3 files changed, 113 insertions(+), 106 deletions(-) diff --git a/include/vcpkg/base/system.console.h b/include/vcpkg/base/system.console.h index 30604332a0..cfafae8bdc 100644 --- a/include/vcpkg/base/system.console.h +++ b/include/vcpkg/base/system.console.h @@ -8,29 +8,43 @@ #include -#include - namespace vcpkg { class Console { public: - explicit Console(int fd) noexcept : fd(fd), is_terminal(::isatty(fd) == 1) {} +#ifdef _WIN32 + explicit Console(unsigned long std_device) noexcept; +#else + explicit Console(int fd) noexcept; +#endif ~Console() = default; + // This function is safe to call from multiple threads. + // When called from multiple threads, calls are atomic with respect to other callers void print(Color color, StringView text) { std::lock_guard lck{mtx}; print_unlocked(color, text); + flush(); } + // This function is safe to call from multiple threads. + // When called from multiple threads, calls are atomic with respect to other callers void print_lines(View> lines); + + // This function is safe to call from multiple threads. + // When called from multiple threads, calls are atomic with respect to other callers void println(Color color, StringView text) { std::lock_guard lck{mtx}; print_unlocked(color, text); print_unlocked(Color::none, "\n"); + flush(); } + + // This function is safe to call from multiple threads. + // When called from multiple threads, calls are atomic with respect to other callers void println(Color color, std::string&& text) { text.push_back('\n'); @@ -40,10 +54,15 @@ namespace vcpkg private: void print_unlocked(Color color, StringView text) noexcept; void write(const char* text, size_t count) noexcept; + void flush() noexcept; + bool check_is_terminal() noexcept; std::mutex mtx; +#if _WIN32 + void* fd; +#else int fd; +#endif bool is_terminal; }; } // namespace vcpkg - diff --git a/src/vcpkg/base/messages.cpp b/src/vcpkg/base/messages.cpp index b4dee56957..53b342f951 100644 --- a/src/vcpkg/base/messages.cpp +++ b/src/vcpkg/base/messages.cpp @@ -284,105 +284,9 @@ namespace vcpkg } namespace vcpkg::msg { - // basic implementation - the write_unlocalized_text_to_stdout -#if defined(_WIN32) - static bool is_console(HANDLE h) - { - DWORD mode = 0; - // GetConsoleMode succeeds iff `h` is a console - // we do not actually care about the mode of the console - return GetConsoleMode(h, &mode); - } - - static void check_write(BOOL success) - { - if (!success) - { - ::fwprintf(stderr, L"[DEBUG] Failed to write to stdout: %lu\n", GetLastError()); - std::abort(); - } - } - static DWORD size_to_write(::size_t size) { return size > MAXDWORD ? MAXDWORD : static_cast(size); } - - static void write_unlocalized_text_impl(Color c, StringView sv, HANDLE the_handle, bool is_console) - { - if (sv.empty()) return; - - static std::mutex mtx; + void write_unlocalized_text_to_stdout(Color c, StringView sv) { return std_out.print(c, sv); } - if (is_console) - { - WORD original_color = 0; - std::lock_guard lck(mtx); - if (c != Color::none) - { - CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info{}; - ::GetConsoleScreenBufferInfo(the_handle, &console_screen_buffer_info); - original_color = console_screen_buffer_info.wAttributes; - ::SetConsoleTextAttribute(the_handle, static_cast(c) | (original_color & 0xF0)); - } - - auto as_wstr = Strings::to_utf16(sv); - - const wchar_t* pointer = as_wstr.data(); - ::size_t size = as_wstr.size(); - - while (size != 0) - { - DWORD written = 0; - check_write(::WriteConsoleW(the_handle, pointer, size_to_write(size), &written, nullptr)); - pointer += written; - size -= written; - } - - if (c != Color::none) - { - ::SetConsoleTextAttribute(the_handle, original_color); - } - } - else - { - const char* pointer = sv.data(); - ::size_t size = sv.size(); - std::lock_guard lck(mtx); - - while (size != 0) - { - DWORD written = 0; - check_write(::WriteFile(the_handle, pointer, size_to_write(size), &written, nullptr)); - pointer += written; - size -= written; - } - } - } - - void write_unlocalized_text_to_stdout(Color c, StringView sv) - { - static const HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); - static const bool stdout_is_console = is_console(stdout_handle); - return write_unlocalized_text_impl(c, sv, stdout_handle, stdout_is_console); - } - - void write_unlocalized_text_to_stderr(Color c, StringView sv) - { - static const HANDLE stderr_handle = ::GetStdHandle(STD_ERROR_HANDLE); - static const bool stderr_is_console = is_console(stderr_handle); - return write_unlocalized_text_impl(c, sv, stderr_handle, stderr_is_console); - } -#else - - void write_unlocalized_text_to_stdout(Color c, StringView sv) - { - static bool is_a_tty = ::isatty(STDOUT_FILENO); - return write_unlocalized_text_impl(c, sv, STDOUT_FILENO, is_a_tty); - } - - void write_unlocalized_text_to_stderr(Color c, StringView sv) - { - static bool is_a_tty = ::isatty(STDERR_FILENO); - return write_unlocalized_text_impl(c, sv, STDERR_FILENO, is_a_tty); - } -#endif + void write_unlocalized_text_to_stderr(Color c, StringView sv) { return std_error.print(c, sv); } OutputStream default_output_stream = OutputStream::StdOut; diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 4be6529d84..5e5b103554 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -1,9 +1,45 @@ +#include #include #include -#include + +#ifdef _WIN32 +#include +#else +#include +#endif namespace vcpkg { +#ifdef _WIN32 + Console::Console(unsigned long std_device) noexcept + : fd(::GetStdHandle(std_device)), is_terminal(this->check_is_terminal()) + { + } +#else + Console::Console(int fd) noexcept : fd(fd), is_terminal(this->check_is_terminal()) { } +#endif + + void Console::flush() noexcept + { +#ifdef _WIN32 + ::FlushFileBuffers(fd); +#else + ::fflush(fd); +#endif + } + + bool Console::check_is_terminal() noexcept + { +#ifdef _WIN32 + DWORD mode = 0; + // GetConsoleMode succeeds if `h` is a console + // we do not actually care about the mode of the console + return GetConsoleMode(fd, &mode); +#else + return ::isatty(fd) == 1; +#endif + } + void Console::print_lines(View> lines) { std::lock_guard lck{mtx}; @@ -14,13 +50,57 @@ namespace vcpkg } } +#ifdef _WIN32 + void Console::print_unlocked(Color c, StringView sv) noexcept + { + if (sv.empty()) return; + + WORD original_color = 0; + + if (is_terminal && c != Color::none) + { + CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info{}; + ::GetConsoleScreenBufferInfo(fd, &console_screen_buffer_info); + original_color = console_screen_buffer_info.wAttributes; + ::SetConsoleTextAttribute(fd, static_cast(c) | (original_color & 0xF0)); + } + + write(sv.data(), sv.size()); + + if (is_terminal && c != Color::none) + { + ::SetConsoleTextAttribute(fd, original_color); + } + } + + static constexpr DWORD size_to_write(size_t size) noexcept + { + return size > MAXDWORD ? MAXDWORD : static_cast(size); + } + + void Console::write(const char* text, size_t count) noexcept + { + while (count != 0) + { + DWORD written = 0; + if (::WriteFile(fd, text, size_to_write(count), &written, nullptr)) + { + ::fwprintf(stderr, L"[DEBUG] Failed to write to stdout: %lu\n", GetLastError()); + std::abort(); + } + text += written; + count -= written; + } + } + +#else + void Console::print_unlocked(Color c, StringView sv) noexcept { static constexpr char reset_color_sequence[] = {'\033', '[', '0', 'm'}; if (sv.empty()) return; - std::lock_guard lck{mtx}; bool reset_color = false; // Only write color sequence if file descriptor is a terminal if (is_terminal && c != Color::none) @@ -53,11 +133,15 @@ namespace vcpkg count -= written; } } - +#endif +#ifdef _WIN32 + Console std_out_instance(STD_OUTPUT_HANDLE); + Console std_error_instance(STD_ERROR_HANDLE); +#else Console std_out_instance(STDOUT_FILENO); Console std_error_instance(STDERR_FILENO); +#endif Console& std_out = std_out_instance; Console& std_error = std_out_instance; } // namespace vcpkg - From 30958be2bf48d14dcdd9675c24a88bfcc6be520b Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:18:51 +0200 Subject: [PATCH 04/15] Fix include --- include/vcpkg/base/messages.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index e723063de4..af9c363586 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -1,11 +1,11 @@ #pragma once #include -#include #include #include #include +#include #include #include From 6c9b436277c0e02b923d103aec69ce0f073ef2da Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:23:05 +0200 Subject: [PATCH 05/15] fix msg::println --- include/vcpkg/base/messages.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index af9c363586..6183cf5df6 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -190,11 +190,13 @@ namespace vcpkg::msg inline void print(Color c, const LocalizedString& s) { std_out.print(c, s); } inline void print(const LocalizedString& s) { std_out.print(Color::none, s); } - inline void println(Color c, LocalizedString&& s) + template + inline void println(Color c, T s) { std_out.print(c, s.append_raw('\n')); } - inline void println(LocalizedString&& s) + template + inline void println(T s) { std_out.print(Color::none, s.append_raw('\n')); } From f7a13656c728f49c17923923733975b42d2d6fee Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:34:21 +0200 Subject: [PATCH 06/15] Fixes --- include/vcpkg/base/messages.h | 1 + include/vcpkg/base/system.console.h | 3 ++- src/vcpkg/base/system.console.cpp | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index 6183cf5df6..efc9d8b72d 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -190,6 +190,7 @@ namespace vcpkg::msg inline void print(Color c, const LocalizedString& s) { std_out.print(c, s); } inline void print(const LocalizedString& s) { std_out.print(Color::none, s); } + inline void println() { std_out.print(Color::none, "\n"); } template inline void println(Color c, T s) { diff --git a/include/vcpkg/base/system.console.h b/include/vcpkg/base/system.console.h index cfafae8bdc..5f1480ca92 100644 --- a/include/vcpkg/base/system.console.h +++ b/include/vcpkg/base/system.console.h @@ -31,6 +31,7 @@ namespace vcpkg // This function is safe to call from multiple threads. // When called from multiple threads, calls are atomic with respect to other callers + // If a line doesn't end with \n, a \n is automatically printed. void print_lines(View> lines); // This function is safe to call from multiple threads. @@ -58,7 +59,7 @@ namespace vcpkg bool check_is_terminal() noexcept; std::mutex mtx; -#if _WIN32 +#ifdef _WIN32 void* fd; #else int fd; diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 5e5b103554..21d6665ed1 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -47,6 +47,11 @@ namespace vcpkg for (auto&& line : lines) { print_unlocked(line.first, line.second); + + if (line.second.back() != '\n') + { + print_unlocked(Color::none, "\n"); + } } } From b4164ededdaf24e2c64ff3113ce46c53c1ea00ee Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:39:03 +0200 Subject: [PATCH 07/15] format --- src/vcpkg/base/system.console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 21d6665ed1..158cca43a2 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -47,7 +47,7 @@ namespace vcpkg for (auto&& line : lines) { print_unlocked(line.first, line.second); - + if (line.second.back() != '\n') { print_unlocked(Color::none, "\n"); From 36610e35d3563dbdc9c76a0e15e18c7b01360fbc Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:41:54 +0200 Subject: [PATCH 08/15] fix attribute error --- include/vcpkg/base/messages.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index efc9d8b72d..1da459d9c1 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -201,7 +201,7 @@ namespace vcpkg::msg { std_out.print(Color::none, s.append_raw('\n')); } - inline [[nodiscard]] LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } + [[nodiscard]] inline LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } template [[nodiscard]] LocalizedString format_error(VCPKG_DECL_MSG_ARGS) { @@ -218,7 +218,7 @@ namespace vcpkg::msg println(Color::error, s); } - inline [[nodiscard]] LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } + [[nodiscard]] inline LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } template [[nodiscard]] LocalizedString format_warning(VCPKG_DECL_MSG_ARGS) { From 3995a516d1caa2b66ca47e40a85da4bf69cfebf0 Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:49:50 +0200 Subject: [PATCH 09/15] use fdatasync --- src/vcpkg/base/system.console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 158cca43a2..a693b65d10 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -24,7 +24,7 @@ namespace vcpkg #ifdef _WIN32 ::FlushFileBuffers(fd); #else - ::fflush(fd); + ::fdatasync(fd); #endif } From faaf02fb6e015e49b64c56bfb8a1d0c65c885cd5 Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:03:06 +0200 Subject: [PATCH 10/15] add missing ! --- src/vcpkg/base/system.console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index a693b65d10..ddb3091d16 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -88,7 +88,7 @@ namespace vcpkg while (count != 0) { DWORD written = 0; - if (::WriteFile(fd, text, size_to_write(count), &written, nullptr)) + if (!::WriteFile(fd, text, size_to_write(count), &written, nullptr)) { ::fwprintf(stderr, L"[DEBUG] Failed to write to stdout: %lu\n", GetLastError()); std::abort(); From 0cb5e682206d53179461a823c59b8806c0d47c4a Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:10:22 +0200 Subject: [PATCH 11/15] use fsync --- src/vcpkg/base/system.console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index ddb3091d16..373d710171 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -24,7 +24,7 @@ namespace vcpkg #ifdef _WIN32 ::FlushFileBuffers(fd); #else - ::fdatasync(fd); + ::fsync(fd); #endif } From 362dd6b5c01b6b035a31596f650c6ee3c4e977ce Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:32:12 +0200 Subject: [PATCH 12/15] Fix print to stderr --- src/vcpkg/base/system.console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 373d710171..867a0f72be 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -148,5 +148,5 @@ namespace vcpkg #endif Console& std_out = std_out_instance; - Console& std_error = std_out_instance; + Console& std_error = std_error_instance; } // namespace vcpkg From 415fbc7edba77b492d3de79ccae5b1da601c7dbe Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Mon, 29 Apr 2024 00:09:03 +0200 Subject: [PATCH 13/15] revert unneeded changes --- include/vcpkg/base/messages.h | 26 +++++++++++++------------- src/vcpkg/base/messages.cpp | 5 +++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index 1da459d9c1..c12ab71031 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -188,20 +188,20 @@ namespace vcpkg::msg return detail::format_to_impl(s, m.index, args.arg()...); } - inline void print(Color c, const LocalizedString& s) { std_out.print(c, s); } - inline void print(const LocalizedString& s) { std_out.print(Color::none, s); } - inline void println() { std_out.print(Color::none, "\n"); } - template - inline void println(Color c, T s) + inline void println() { msg::write_unlocalized_text(Color::none, "\n"); } + + inline void print(Color c, const LocalizedString& s) { msg::write_unlocalized_text(c, s); } + inline void print(const LocalizedString& s) { msg::write_unlocalized_text(Color::none, s); } + inline void println(Color c, LocalizedString s) { - std_out.print(c, s.append_raw('\n')); + msg::write_unlocalized_text(c, s.append_raw('\n')); } - template - inline void println(T s) + inline void println(LocalizedString s) { - std_out.print(Color::none, s.append_raw('\n')); + msg::write_unlocalized_text(Color::none, s.append_raw('\n')); } - [[nodiscard]] inline LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } + + [[nodiscard]] LocalizedString format_error(const LocalizedString& s); template [[nodiscard]] LocalizedString format_error(VCPKG_DECL_MSG_ARGS) { @@ -209,7 +209,7 @@ namespace vcpkg::msg msg::format_to(s, VCPKG_EXPAND_MSG_ARGS); return s; } - inline void println_error(const LocalizedString& s) { println(Color::error, format_error(s)); } + void println_error(const LocalizedString& s); template void println_error(VCPKG_DECL_MSG_ARGS) { @@ -218,7 +218,7 @@ namespace vcpkg::msg println(Color::error, s); } - [[nodiscard]] inline LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } + [[nodiscard]] LocalizedString format_warning(const LocalizedString& s); template [[nodiscard]] LocalizedString format_warning(VCPKG_DECL_MSG_ARGS) { @@ -226,7 +226,7 @@ namespace vcpkg::msg msg::format_to(s, VCPKG_EXPAND_MSG_ARGS); return s; } - inline void println_warning(const LocalizedString& s) { println(Color::warning, format_warning(s)); } + void println_warning(const LocalizedString& s); template void println_warning(VCPKG_DECL_MSG_ARGS) { diff --git a/src/vcpkg/base/messages.cpp b/src/vcpkg/base/messages.cpp index 53b342f951..f8ef95324a 100644 --- a/src/vcpkg/base/messages.cpp +++ b/src/vcpkg/base/messages.cpp @@ -381,4 +381,9 @@ namespace vcpkg::msg return nullopt; } + + LocalizedString format_error(const LocalizedString& s) { return error_prefix().append(s); } + void println_error(const LocalizedString& s) { println(Color::error, format_error(s)); } + LocalizedString format_warning(const LocalizedString& s) { return warning_prefix().append(s); } + void println_warning(const LocalizedString& s) { println(Color::warning, format_warning(s)); } } From 2af3640d0558c3f117adb149202978ada3d4a22b Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Mon, 29 Apr 2024 00:49:42 +0200 Subject: [PATCH 14/15] format --- include/vcpkg/base/messages.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/include/vcpkg/base/messages.h b/include/vcpkg/base/messages.h index c12ab71031..80375952de 100644 --- a/include/vcpkg/base/messages.h +++ b/include/vcpkg/base/messages.h @@ -192,14 +192,8 @@ namespace vcpkg::msg inline void print(Color c, const LocalizedString& s) { msg::write_unlocalized_text(c, s); } inline void print(const LocalizedString& s) { msg::write_unlocalized_text(Color::none, s); } - inline void println(Color c, LocalizedString s) - { - msg::write_unlocalized_text(c, s.append_raw('\n')); - } - inline void println(LocalizedString s) - { - msg::write_unlocalized_text(Color::none, s.append_raw('\n')); - } + inline void println(Color c, LocalizedString s) { msg::write_unlocalized_text(c, s.append_raw('\n')); } + inline void println(LocalizedString s) { msg::write_unlocalized_text(Color::none, s.append_raw('\n')); } [[nodiscard]] LocalizedString format_error(const LocalizedString& s); template From b1a4a0ddee5c68227f499ff8b940b30f39169e63 Mon Sep 17 00:00:00 2001 From: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> Date: Tue, 30 Apr 2024 14:12:55 +0200 Subject: [PATCH 15/15] improve clarity --- src/vcpkg/base/system.console.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vcpkg/base/system.console.cpp b/src/vcpkg/base/system.console.cpp index 867a0f72be..8fed810d90 100644 --- a/src/vcpkg/base/system.console.cpp +++ b/src/vcpkg/base/system.console.cpp @@ -44,11 +44,11 @@ namespace vcpkg { std::lock_guard lck{mtx}; - for (auto&& line : lines) + for (auto&& [color, text] : lines) { - print_unlocked(line.first, line.second); + print_unlocked(color, text); - if (line.second.back() != '\n') + if (text.back() != '\n') { print_unlocked(Color::none, "\n"); }