Skip to content

Commit 6307ec0

Browse files
authored
common : cleanup logs and modernize the progress bar (#21215)
``` $ 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 <angt@huggingface.co>
1 parent 632219a commit 6307ec0

1 file changed

Lines changed: 41 additions & 27 deletions

File tree

common/download.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ class ProgressBar {
119119
static inline std::map<const ProgressBar *, int> lines;
120120
static inline int max_line = 0;
121121

122+
std::string filename;
123+
size_t len = 0;
124+
122125
static void cleanup(const ProgressBar * line) {
123126
lines.erase(line);
124127
if (lines.empty()) {
@@ -135,19 +138,31 @@ class ProgressBar {
135138
}
136139

137140
public:
138-
ProgressBar() = default;
141+
ProgressBar(const std::string & url = "") : filename(url) {
142+
if (auto pos = filename.rfind('/'); pos != std::string::npos) {
143+
filename = filename.substr(pos + 1);
144+
}
145+
if (auto pos = filename.find('?'); pos != std::string::npos) {
146+
filename = filename.substr(0, pos);
147+
}
148+
for (size_t i = 0; i < filename.size(); ++i) {
149+
if ((filename[i] & 0xC0) != 0x80) {
150+
if (len++ == 39) {
151+
filename.resize(i);
152+
filename += "";
153+
break;
154+
}
155+
}
156+
}
157+
}
139158

140159
~ProgressBar() {
141160
std::lock_guard<std::mutex> lock(mutex);
142161
cleanup(this);
143162
}
144163

145164
void update(size_t current, size_t total) {
146-
if (!is_output_a_tty()) {
147-
return;
148-
}
149-
150-
if (!total) {
165+
if (!total || !is_output_a_tty()) {
151166
return;
152167
}
153168

@@ -159,28 +174,27 @@ class ProgressBar {
159174
}
160175
int lines_up = max_line - lines[this];
161176

162-
size_t width = 50;
177+
size_t bar = 55 - len;
163178
size_t pct = (100 * current) / total;
164-
size_t pos = (width * current) / total;
165-
166-
std::cout << "\033[s";
179+
size_t pos = (bar * current) / total;
167180

168181
if (lines_up > 0) {
169182
std::cout << "\033[" << lines_up << "A";
170183
}
171-
std::cout << "\033[2K\r["
172-
<< std::string(pos, '=')
173-
<< (pos < width ? ">" : "")
174-
<< std::string(width - pos, ' ')
175-
<< "] " << std::setw(3) << pct << "% ("
176-
<< current / (1024 * 1024) << " MB / "
177-
<< total / (1024 * 1024) << " MB) "
178-
<< "\033[u";
184+
std::cout << '\r' << "Downloading " << filename << " ";
185+
186+
for (size_t i = 0; i < bar; ++i) {
187+
std::cout << (i < pos ? "" : " ");
188+
}
189+
std::cout << std::setw(4) << pct << "%\033[K";
179190

180-
std::cout.flush();
191+
if (lines_up > 0) {
192+
std::cout << "\033[" << lines_up << "B";
193+
}
194+
std::cout << '\r' << std::flush;
181195

182196
if (current == total) {
183-
cleanup(this);
197+
cleanup(this);
184198
}
185199
}
186200

@@ -208,7 +222,7 @@ static bool common_pull_file(httplib::Client & cli,
208222
const char * func = __func__; // avoid __func__ inside a lambda
209223
size_t downloaded = existing_size;
210224
size_t progress_step = 0;
211-
ProgressBar bar;
225+
ProgressBar bar(resolve_path);
212226

213227
auto res = cli.Get(resolve_path, headers,
214228
[&](const httplib::Response &response) {
@@ -286,15 +300,15 @@ static int common_download_file_single_online(const std::string & url,
286300
const bool file_exists = std::filesystem::exists(path);
287301

288302
if (file_exists && skip_etag) {
289-
LOG_INF("%s: using cached file: %s\n", __func__, path.c_str());
303+
LOG_DBG("%s: using cached file: %s\n", __func__, path.c_str());
290304
return 304; // 304 Not Modified - fake cached response
291305
}
292306

293307
std::string last_etag;
294308
if (file_exists) {
295309
last_etag = read_etag(path);
296310
} else {
297-
LOG_INF("%s: no previous model file found %s\n", __func__, path.c_str());
311+
LOG_DBG("%s: no previous model file found %s\n", __func__, path.c_str());
298312
}
299313

300314
auto head = cli.Head(parts.path);
@@ -328,11 +342,11 @@ static int common_download_file_single_online(const std::string & url,
328342

329343
if (file_exists) {
330344
if (etag.empty()) {
331-
LOG_INF("%s: using cached file (no server etag): %s\n", __func__, path.c_str());
345+
LOG_DBG("%s: using cached file (no server etag): %s\n", __func__, path.c_str());
332346
return 304; // 304 Not Modified - fake cached response
333347
}
334348
if (!last_etag.empty() && last_etag == etag) {
335-
LOG_INF("%s: using cached file (same etag): %s\n", __func__, path.c_str());
349+
LOG_DBG("%s: using cached file (same etag): %s\n", __func__, path.c_str());
336350
return 304; // 304 Not Modified - fake cached response
337351
}
338352
if (remove(path.c_str()) != 0) {
@@ -368,7 +382,7 @@ static int common_download_file_single_online(const std::string & url,
368382
}
369383
}
370384

371-
LOG_INF("%s: downloading from %s to %s (etag:%s)...\n",
385+
LOG_DBG("%s: downloading from %s to %s (etag:%s)...\n",
372386
__func__, common_http_show_masked_url(parts).c_str(),
373387
path_temporary.c_str(), etag.c_str());
374388

@@ -437,7 +451,7 @@ int common_download_file_single(const std::string & url,
437451
return -1;
438452
}
439453

440-
LOG_INF("%s: using cached file (offline mode): %s\n", __func__, path.c_str());
454+
LOG_DBG("%s: using cached file (offline mode): %s\n", __func__, path.c_str());
441455
return 304; // Not Modified - fake cached response
442456
}
443457

0 commit comments

Comments
 (0)