Skip to content

Commit 17dd399

Browse files
authored
Make httpserver::error_log consume and apply va_list. (#298)
* Make httpserver::error_log consume an apply va_list ap so that it generates full log messages. There are a small number of places where error_log is called where it is expected to apply printf style formatting to it's extra arguments. In most of them, the extra arguments are the most important. * Try to clean up va_list_usedBeforeStarted, va_start_subsequentCalls and va_end_missing. (I don't have a cppcheck setup and I can't seem to make anything fail so I'm kinda gussing here.) * Try to clean up va_list_usedBeforeStarted & va_start_subsequentCalls. (I don't have a cppcheck setup and I can't seem to make anything fail so I'm kinda gussing here.)
1 parent 7ecd30e commit 17dd399

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/webserver.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,25 @@ void* uri_log(void* cls, const char* uri) {
429429
}
430430

431431
void error_log(void* cls, const char* fmt, va_list ap) {
432-
// Parameter needed to respect MHD interface, but not needed here.
433-
std::ignore = ap;
434-
435432
webserver* dws = static_cast<webserver*>(cls);
436-
if (dws->log_error != nullptr) dws->log_error(fmt);
433+
434+
std::string msg;
435+
msg.resize(80); // Asssume one line will be enought most of the time.
436+
437+
va_list va;
438+
va_copy(va, ap); // Stash a copy in case we need to try again.
439+
440+
size_t r = vsnprintf(&*msg.begin(), msg.size(), fmt, ap);
441+
va_end(ap);
442+
443+
if (msg.size() < r) {
444+
msg.resize(r);
445+
r = vsnprintf(&*msg.begin(), msg.size(), fmt, va);
446+
}
447+
va_end(va);
448+
msg.resize(r);
449+
450+
if (dws->log_error != nullptr) dws->log_error(msg);
437451
}
438452

439453
void access_log(webserver* dws, string uri) {

0 commit comments

Comments
 (0)