Path: blob/master/test/hotspot/gtest/logging/logTestFixture.cpp
64438 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() {48restore_config();49clear_snapshot();50delete_file(TestLogFileName);51}5253bool LogTestFixture::set_log_config(const char* output,54const char* what,55const char* decorators,56const char* options,57bool allow_failure) {58ResourceMark rm;59stringStream stream;60bool success = LogConfiguration::parse_log_arguments(output, what, decorators, options, &stream);61if (!allow_failure) {62const char* errmsg = stream.as_string();63EXPECT_STREQ("", errmsg) << "Unexpected error reported";64EXPECT_TRUE(success) << "Shouldn't cause errors";65}66return success;67}6869void LogTestFixture::snapshot_config() {70clear_snapshot();71_n_snapshots = LogConfiguration::_n_outputs;72_configuration_snapshot = NEW_C_HEAP_ARRAY(char*, _n_snapshots, mtLogging);73for (size_t i = 0; i < _n_snapshots; i++) {74ResourceMark rm;75stringStream ss;76LogConfiguration::_outputs[i]->describe(&ss);77_configuration_snapshot[i] = os::strdup_check_oom(ss.as_string(), mtLogging);78}79}8081void LogTestFixture::restore_config() {82LogConfiguration::disable_logging();83for (size_t i = 0; i < _n_snapshots; i++) {84// Restore the config based on the saved output description string.85// The string has the following format: '<name> <selection> <decorators>[ <options>]'86// Extract the different parameters by replacing the spaces with NULLs.87char* str = _configuration_snapshot[i];8889char* name = str;90str = strchr(str, ' ');91*str++ = '\0';9293char* selection = str;94str = strchr(str, ' ');95*str++ = '\0';9697char* decorators = str;9899char* options = NULL;100str = strchr(str, ' ');101if (str != NULL) {102*str++ = '\0';103options = str;104}105106set_log_config(name, selection, decorators, options != NULL ? options : "");107}108}109110void LogTestFixture::clear_snapshot() {111if (_configuration_snapshot == NULL) {112return;113}114assert(_n_snapshots > 0, "non-null array should have at least 1 element");115for (size_t i = 0; i < _n_snapshots; i++) {116os::free(_configuration_snapshot[i]);117}118FREE_C_HEAP_ARRAY(char*, _configuration_snapshot);119_configuration_snapshot = NULL;120_n_snapshots = 0;121}122123124