1 /*
2  *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <cstdarg>
12 
13 #include "rtc_base/strings/string_format.h"
14 
15 #include "rtc_base/checks.h"
16 
17 namespace rtc {
18 
19 namespace {
20 
21 // This is an arbitrary limitation that can be changed if necessary, or removed
22 // if someone has the time and inclination to replicate the fancy logic from
23 // Chromium's base::StringPrinf().
24 constexpr int kMaxSize = 512;
25 
26 }  // namespace
27 
StringFormat(const char * fmt,...)28 std::string StringFormat(const char* fmt, ...) {
29   char buffer[kMaxSize];
30   va_list args;
31   va_start(args, fmt);
32   int result = vsnprintf(buffer, kMaxSize, fmt, args);
33   va_end(args);
34   RTC_DCHECK_GE(result, 0) << "ERROR: vsnprintf() failed with error " << result;
35   RTC_DCHECK_LT(result, kMaxSize)
36       << "WARNING: string was truncated from " << result << " to "
37       << (kMaxSize - 1) << " characters";
38   return std::string(buffer);
39 }
40 
41 }  // namespace rtc
42