Files
android/libwvdrmengine/cdm/util/src/string_format.cpp
John "Juce" Bruce c232299f78 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
2024-02-01 13:40:52 -08:00

48 lines
1.2 KiB
C++

// Copyright 2022 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include "string_format.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
namespace wvutil {
bool FormatString(std::string* out, const char* fmt, ...) {
if (out == nullptr || fmt == nullptr) return false;
va_list vlist;
va_start(vlist, fmt);
const bool result = VFormatString(out, fmt, vlist);
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;
const size_t buffer_size =
static_cast<size_t>(desired_size) + 1; // +1 for null
std::unique_ptr<char[]> buffer(new char[buffer_size]);
const int actual_size = vsnprintf(buffer.get(), buffer_size, fmt, vlist);
if (actual_size != desired_size) return false;
out->assign(buffer.get(), actual_size);
return true;
}
} // namespace wvutil