Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #ifndef TEST_LOGGING_LOG_WRITER_H_ |
| 11 | #define TEST_LOGGING_LOG_WRITER_H_ |
| 12 | |
Niels Möller | 198cf00 | 2019-05-10 12:29:13 +0200 | [diff] [blame] | 13 | #include <stdarg.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame] | 19 | #include "api/rtc_event_log_output.h" |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | template <class... Args> |
| 24 | inline void LogWriteFormat(RtcEventLogOutput* out_, const char* fmt, ...) { |
| 25 | va_list args, copy; |
| 26 | va_start(args, fmt); |
| 27 | va_copy(copy, args); |
| 28 | const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy); |
| 29 | va_end(copy); |
| 30 | |
| 31 | RTC_DCHECK_GE(predicted_length, 0); |
| 32 | std::string out_str(predicted_length, '\0'); |
| 33 | if (predicted_length > 0) { |
| 34 | // Pass "+ 1" to vsnprintf to include space for the '\0'. |
| 35 | const int actual_length = |
| 36 | std::vsnprintf(&out_str.front(), predicted_length + 1, fmt, args); |
| 37 | RTC_DCHECK_GE(actual_length, 0); |
| 38 | } |
| 39 | va_end(args); |
| 40 | out_->Write(out_str); |
| 41 | } |
| 42 | |
| 43 | class LogWriterFactoryInterface { |
| 44 | public: |
| 45 | virtual std::unique_ptr<RtcEventLogOutput> Create(std::string filename) = 0; |
| 46 | virtual ~LogWriterFactoryInterface() = default; |
| 47 | }; |
| 48 | |
| 49 | class LogWriterFactoryAddPrefix : public LogWriterFactoryInterface { |
| 50 | public: |
| 51 | LogWriterFactoryAddPrefix(LogWriterFactoryInterface* base, |
| 52 | std::string prefix); |
| 53 | std::unique_ptr<RtcEventLogOutput> Create(std::string filename) override; |
| 54 | |
| 55 | private: |
| 56 | LogWriterFactoryInterface* const base_factory_; |
| 57 | const std::string prefix_; |
| 58 | }; |
| 59 | |
| 60 | } // namespace webrtc |
| 61 | |
| 62 | #endif // TEST_LOGGING_LOG_WRITER_H_ |