blob: 270a48aa9507dc14f1abfd2313638e293267e34a [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/test/wpt/cwt_stderr_logger.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
CWTStderrLogger* CWTStderrLogger::GetInstance() {
return base::Singleton<CWTStderrLogger>::get();
}
void CWTStderrLogger::StartRedirectingToFile(const base::FilePath& file_path) {
base::File destination_file(file_path,
base::File::FLAG_OPEN | base::File::FLAG_APPEND);
DCHECK_EQ(saved_stderr_file_descriptor_, -1);
saved_stderr_file_descriptor_ = dup(STDERR_FILENO);
redirection_destination_file_descriptor_ =
destination_file.TakePlatformFile();
dup2(redirection_destination_file_descriptor_, STDERR_FILENO);
}
void CWTStderrLogger::StopRedirectingToFile() {
DCHECK_NE(saved_stderr_file_descriptor_, -1);
close(redirection_destination_file_descriptor_);
redirection_destination_file_descriptor_ = -1;
// Reset stderr to its previous destination, before redirection began.
dup2(saved_stderr_file_descriptor_, STDERR_FILENO);
close(saved_stderr_file_descriptor_);
saved_stderr_file_descriptor_ = -1;
}