Path: blob/master/test/hotspot/gtest/logging/logTestFixture.cpp
41145 views
/*1* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "jvm.h"25#include "logTestFixture.hpp"26#include "logTestUtils.inline.hpp"27#include "logging/logConfiguration.hpp"28#include "logging/logOutput.hpp"29#include "memory/allocation.inline.hpp"30#include "memory/resourceArea.hpp"31#include "unittest.hpp"32#include "utilities/ostream.hpp"3334LogTestFixture::LogTestFixture() : _n_snapshots(0), _configuration_snapshot(NULL) {3536// Set up TestLogFileName to include temp_dir, PID, testcase name and test name.37const testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();38int ret = jio_snprintf(_filename, sizeof(_filename), "%s%stestlog.pid%d.%s.%s.log",39os::get_temp_directory(), os::file_separator(), os::current_process_id(),40test_info->test_case_name(), test_info->name());41EXPECT_GT(ret, 0) << "_filename buffer issue";42TestLogFileName = _filename;4344snapshot_config();45}4647LogTestFixture::~LogTestFixture() {48AsyncLogWriter::flush();49restore_config();50clear_snapshot();51delete_file(TestLogFileName);52}5354bool LogTestFixture::set_log_config(const char* output,55const char* what,56const char* decorators,57const char* options,58bool allow_failure) {59ResourceMark rm;60stringStream stream;61bool success = LogConfiguration::parse_log_arguments(output, what, decorators, options, &stream);62if (!allow_failure) {63const char* errmsg = stream.as_string();64EXPECT_STREQ("", errmsg) << "Unexpected error reported";65EXPECT_TRUE(success) << "Shouldn't cause errors";66}67return success;68}6970void LogTestFixture::snapshot_config() {71clear_snapshot();72_n_snapshots = LogConfiguration::_n_outputs;73_configuration_snapshot = NEW_C_HEAP_ARRAY(char*, _n_snapshots, mtLogging);74for (size_t i = 0; i < _n_snapshots; i++) {75ResourceMark rm;76stringStream ss;77LogConfiguration::_outputs[i]->describe(&ss);78_configuration_snapshot[i] = os::strdup_check_oom(ss.as_string(), mtLogging);79}80}8182void LogTestFixture::restore_config() {83LogConfiguration::disable_logging();84for (size_t i = 0; i < _n_snapshots; i++) {85// Restore the config based on the saved output description string.86// The string has the following format: '<name> <selection> <decorators>[ <options>]'87// Extract the different parameters by replacing the spaces with NULLs.88char* str = _configuration_snapshot[i];8990char* name = str;91str = strchr(str, ' ');92*str++ = '\0';9394char* selection = str;95str = strchr(str, ' ');96*str++ = '\0';9798char* decorators = str;99100char* options = NULL;101str = strchr(str, ' ');102if (str != NULL) {103*str++ = '\0';104options = str;105}106107set_log_config(name, selection, decorators, options != NULL ? options : "");108}109}110111void LogTestFixture::clear_snapshot() {112if (_configuration_snapshot == NULL) {113return;114}115assert(_n_snapshots > 0, "non-null array should have at least 1 element");116for (size_t i = 0; i < _n_snapshots; i++) {117os::free(_configuration_snapshot[i]);118}119FREE_C_HEAP_ARRAY(char*, _configuration_snapshot);120_configuration_snapshot = NULL;121_n_snapshots = 0;122}123124125