Allow CE CDM integrators to specify their own logging

This patch adds a new interface that partners must provide to
Cdm::initialize(), ILogger. ILogger replaces stderr as the sink to which
logging messages are sent. For partners that still want to log to
stderr, a reference implementation that logs to stderr is provided.

As a side-effect of this, many test-related source files had to be
updated to thread the new parameter to Cdm::initialize() through them.
This also necessitated adding a new variant of FormatString() that can
be called with a va_list directly so it can be called from other
functions that take varargs.

Bug: 201446862
Merged from https://widevine-internal-review.googlesource.com/177270

Change-Id: Ie31a10162773883b337f3a6144cf180a2b100139
This commit is contained in:
John "Juce" Bruce
2023-06-16 11:18:01 -07:00
committed by Robert Shih
parent 8e48e36554
commit c232299f78
2 changed files with 20 additions and 8 deletions

View File

@@ -13,6 +13,11 @@ namespace wvutil {
#endif #endif
bool FormatString(std::string* out, const char* fmt, ...); bool FormatString(std::string* out, const char* fmt, ...);
#ifdef __GNUC__
[[gnu::format(printf, 2, 0)]]
#endif
bool VFormatString(std::string* out, const char* fmt, va_list vlist);
} // namespace wvutil } // namespace wvutil
#endif // WVCDM_UTIL_STRING_FORMAT_H_ #endif // WVCDM_UTIL_STRING_FORMAT_H_

View File

@@ -16,20 +16,27 @@ namespace wvutil {
bool FormatString(std::string* out, const char* fmt, ...) { bool FormatString(std::string* out, const char* fmt, ...) {
if (out == nullptr || fmt == nullptr) return false; if (out == nullptr || fmt == nullptr) return false;
va_list ap1; va_list vlist;
va_start(ap1, fmt); va_start(vlist, fmt);
const int desired_size = vsnprintf(nullptr, 0, fmt, ap1); const bool result = VFormatString(out, fmt, vlist);
va_end(ap1); va_end(vlist);
return result;
}
bool VFormatString(std::string* out, const char* fmt, va_list vlist) {
if (out == nullptr || fmt == nullptr) return false;
va_list vlist_copy;
va_copy(vlist_copy, vlist);
const int desired_size = vsnprintf(nullptr, 0, fmt, vlist_copy);
va_end(vlist_copy);
if (desired_size < 0) return false; if (desired_size < 0) return false;
const size_t buffer_size = const size_t buffer_size =
static_cast<size_t>(desired_size) + 1; // +1 for null static_cast<size_t>(desired_size) + 1; // +1 for null
std::unique_ptr<char[]> buffer(new char[buffer_size]); std::unique_ptr<char[]> buffer(new char[buffer_size]);
va_list ap2; const int actual_size = vsnprintf(buffer.get(), buffer_size, fmt, vlist);
va_start(ap2, fmt);
const int actual_size = vsnprintf(buffer.get(), buffer_size, fmt, ap2);
va_end(ap2);
if (actual_size != desired_size) return false; if (actual_size != desired_size) return false;