From 6307ec07d3ad730f21886fb484cdb4abe1e74091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Tue, 31 Mar 2026 16:18:00 +0200 Subject: [PATCH] common : cleanup logs and modernize the progress bar (#21215) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` $ build/bin/llama-server -hf unsloth/Qwen3.5-0.8B-GGUF common_download_file_single_online: HEAD failed, status: 404 no remote preset found, skipping Downloading mmproj-BF16.gguf ——————————————————————————————————————— 100% Downloading Qwen3.5-0.8B-Q4_K_M.gguf ——————————————————————————————— 100% ... ``` Signed-off-by: Adrien Gallouët --- common/download.cpp | 68 +++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/common/download.cpp b/common/download.cpp index fce5cda88e..ad720f977b 100644 --- a/common/download.cpp +++ b/common/download.cpp @@ -119,6 +119,9 @@ class ProgressBar { static inline std::map lines; static inline int max_line = 0; + std::string filename; + size_t len = 0; + static void cleanup(const ProgressBar * line) { lines.erase(line); if (lines.empty()) { @@ -135,7 +138,23 @@ class ProgressBar { } public: - ProgressBar() = default; + ProgressBar(const std::string & url = "") : filename(url) { + if (auto pos = filename.rfind('/'); pos != std::string::npos) { + filename = filename.substr(pos + 1); + } + if (auto pos = filename.find('?'); pos != std::string::npos) { + filename = filename.substr(0, pos); + } + for (size_t i = 0; i < filename.size(); ++i) { + if ((filename[i] & 0xC0) != 0x80) { + if (len++ == 39) { + filename.resize(i); + filename += "…"; + break; + } + } + } + } ~ProgressBar() { std::lock_guard lock(mutex); @@ -143,11 +162,7 @@ public: } void update(size_t current, size_t total) { - if (!is_output_a_tty()) { - return; - } - - if (!total) { + if (!total || !is_output_a_tty()) { return; } @@ -159,28 +174,27 @@ public: } int lines_up = max_line - lines[this]; - size_t width = 50; + size_t bar = 55 - len; size_t pct = (100 * current) / total; - size_t pos = (width * current) / total; - - std::cout << "\033[s"; + size_t pos = (bar * current) / total; if (lines_up > 0) { std::cout << "\033[" << lines_up << "A"; } - std::cout << "\033[2K\r[" - << std::string(pos, '=') - << (pos < width ? ">" : "") - << std::string(width - pos, ' ') - << "] " << std::setw(3) << pct << "% (" - << current / (1024 * 1024) << " MB / " - << total / (1024 * 1024) << " MB) " - << "\033[u"; + std::cout << '\r' << "Downloading " << filename << " "; - std::cout.flush(); + for (size_t i = 0; i < bar; ++i) { + std::cout << (i < pos ? "—" : " "); + } + std::cout << std::setw(4) << pct << "%\033[K"; + + if (lines_up > 0) { + std::cout << "\033[" << lines_up << "B"; + } + std::cout << '\r' << std::flush; if (current == total) { - cleanup(this); + cleanup(this); } } @@ -208,7 +222,7 @@ static bool common_pull_file(httplib::Client & cli, const char * func = __func__; // avoid __func__ inside a lambda size_t downloaded = existing_size; size_t progress_step = 0; - ProgressBar bar; + ProgressBar bar(resolve_path); auto res = cli.Get(resolve_path, headers, [&](const httplib::Response &response) { @@ -286,7 +300,7 @@ static int common_download_file_single_online(const std::string & url, const bool file_exists = std::filesystem::exists(path); if (file_exists && skip_etag) { - LOG_INF("%s: using cached file: %s\n", __func__, path.c_str()); + LOG_DBG("%s: using cached file: %s\n", __func__, path.c_str()); return 304; // 304 Not Modified - fake cached response } @@ -294,7 +308,7 @@ static int common_download_file_single_online(const std::string & url, if (file_exists) { last_etag = read_etag(path); } else { - LOG_INF("%s: no previous model file found %s\n", __func__, path.c_str()); + LOG_DBG("%s: no previous model file found %s\n", __func__, path.c_str()); } auto head = cli.Head(parts.path); @@ -328,11 +342,11 @@ static int common_download_file_single_online(const std::string & url, if (file_exists) { if (etag.empty()) { - LOG_INF("%s: using cached file (no server etag): %s\n", __func__, path.c_str()); + LOG_DBG("%s: using cached file (no server etag): %s\n", __func__, path.c_str()); return 304; // 304 Not Modified - fake cached response } if (!last_etag.empty() && last_etag == etag) { - LOG_INF("%s: using cached file (same etag): %s\n", __func__, path.c_str()); + LOG_DBG("%s: using cached file (same etag): %s\n", __func__, path.c_str()); return 304; // 304 Not Modified - fake cached response } if (remove(path.c_str()) != 0) { @@ -368,7 +382,7 @@ static int common_download_file_single_online(const std::string & url, } } - LOG_INF("%s: downloading from %s to %s (etag:%s)...\n", + LOG_DBG("%s: downloading from %s to %s (etag:%s)...\n", __func__, common_http_show_masked_url(parts).c_str(), path_temporary.c_str(), etag.c_str()); @@ -437,7 +451,7 @@ int common_download_file_single(const std::string & url, return -1; } - LOG_INF("%s: using cached file (offline mode): %s\n", __func__, path.c_str()); + LOG_DBG("%s: using cached file (offline mode): %s\n", __func__, path.c_str()); return 304; // Not Modified - fake cached response }