ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "webrtc/test/testsupport/test_output.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 15 | #include "webrtc/rtc_base/flags.h" |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 16 | #include "webrtc/rtc_base/file.h" |
| 17 | #include "webrtc/rtc_base/logging.h" |
| 18 | #include "webrtc/rtc_base/pathutils.h" |
| 19 | #include "webrtc/test/testsupport/fileutils.h" |
| 20 | |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 21 | namespace { |
| 22 | const std::string kDefaultOutputPath = webrtc::test::OutputPath(); |
| 23 | } |
| 24 | |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 25 | DEFINE_string(test_output_dir, |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 26 | kDefaultOutputPath.c_str(), |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 27 | "The output folder where test output should be saved."); |
| 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
| 32 | bool GetTestOutputDir(std::string* out_dir) { |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 33 | if (strlen(FLAG_test_output_dir) == 0) { |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 34 | LOG(LS_WARNING) << "No test_out_dir defined."; |
| 35 | return false; |
| 36 | } |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 37 | *out_dir = FLAG_test_output_dir; |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 38 | return true; |
| 39 | } |
| 40 | |
| 41 | bool WriteToTestOutput(const char* filename, |
| 42 | const uint8_t* buffer, |
| 43 | size_t length) { |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 44 | if (strlen(FLAG_test_output_dir) == 0) { |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 45 | LOG(LS_WARNING) << "No test_out_dir defined."; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | if (filename == nullptr || strlen(filename) == 0) { |
| 50 | LOG(LS_WARNING) << "filename must be provided."; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | rtc::File output = |
ilnik | 26e5cbd | 2017-08-18 09:00:04 -0700 | [diff] [blame] | 55 | rtc::File::Create(rtc::Pathname(FLAG_test_output_dir, filename)); |
ilnik | e264a9e | 2017-07-25 07:31:18 -0700 | [diff] [blame] | 56 | |
| 57 | return output.IsOpen() && output.Write(buffer, length) == length; |
| 58 | } |
| 59 | |
| 60 | bool WriteToTestOutput(const char* filename, const std::string& content) { |
| 61 | return WriteToTestOutput(filename, |
| 62 | reinterpret_cast<const uint8_t*>(content.c_str()), |
| 63 | content.length()); |
| 64 | } |
| 65 | |
| 66 | } // namespace test |
| 67 | } // namespace webrtc |