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