Path: blob/master/test/hotspot/gtest/logging/logTestUtils.inline.hpp
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 "logging/log.hpp"24#include "logging/logAsyncWriter.hpp"25#include "logging/logConfiguration.hpp"26#include "logging/logStream.hpp"27#include "memory/resourceArea.hpp"28#include "runtime/os.hpp"29#include "unittest.hpp"3031#define LOG_TEST_STRING_LITERAL "a (hopefully) unique log message for testing"3233static const char* invalid_selection_substr[] = {34"=", "+", " ", "+=", "+=*", "*+", " +", "**", "++", ".", ",", ",," ",+",35" *", "all+", "all*", "+all", "+all=Warning", "==Info", "=InfoWarning",36"BadTag+", "logging++", "logging*+", ",=", "gc+gc+"37};3839static inline bool string_contains_substring(const char* haystack, const char* needle) {40return strstr(haystack, needle) != NULL;41}4243static inline bool file_exists(const char* filename) {44struct stat st;45return os::stat(filename, &st) == 0;46}4748static inline void delete_file(const char* filename) {49AsyncLogWriter::flush();50if (!file_exists(filename)) {51return;52}53int ret = remove(filename);54EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "55<< os::strerror(errno) << " (" << errno << ")";56}5758static inline void create_directory(const char* name) {59assert(!file_exists(name), "can't create directory: %s already exists", name);60bool failed;61#ifdef _WINDOWS62failed = !CreateDirectory(name, NULL);63#else64failed = mkdir(name, 0777);65#endif66assert(!failed, "failed to create directory %s", name);67}6869static inline void delete_empty_directory(const char* name) {70#ifdef _WINDOWS71if (!file_exists(name)) {72return;73}74bool failed;75failed = !RemoveDirectory(name);76EXPECT_FALSE(failed) << "failed to remove directory '" << name77<< "': LastError = " << GetLastError();78#else79delete_file(name);80#endif81}8283static inline void init_log_file(const char* filename, const char* options = "") {84LogStreamHandle(Error, logging) stream;85bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream);86guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options);87log_debug(logging)("%s", LOG_TEST_STRING_LITERAL);88success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream);89guarantee(success, "Failed to disable logging to file '%s'", filename);90}9192static const char* tmp_dir = os::get_temp_directory();93static const char* file_sep = os::file_separator();9495// Prepend filename with the temp directory and pid and return the result as a96// resource allocated string.97static inline char* prepend_temp_dir(const char* filename) {98size_t temp_file_len = strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 28;99char* temp_file = NEW_RESOURCE_ARRAY(char, temp_file_len);100int ret = jio_snprintf(temp_file, temp_file_len, "%s%spid%d.%s",101tmp_dir, file_sep,102os::current_process_id(), filename);103return temp_file;104}105106// Prepend filename with specified prefix and the temp directory and return the107// result as a malloc allocated string. This is used by test_logFileOutput.cpp.108static inline char* prepend_prefix_temp_dir(const char* prefix, const char* filename) {109size_t temp_file_len = strlen(prefix) + strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 1;110char* temp_file = (char*)os::malloc(temp_file_len, mtLogging);111int ret = jio_snprintf(temp_file, temp_file_len, "%s%s%s%s",112prefix, tmp_dir, file_sep, filename);113return temp_file;114}115116// Read a complete line from fp and return it as a resource allocated string.117// Returns NULL on EOF.118static inline char* read_line(FILE* fp) {119assert(fp != NULL, "invalid fp");120int buflen = 512;121char* buf = NEW_RESOURCE_ARRAY(char, buflen);122long pos = ftell(fp);123if (pos < 0) return NULL;124125char* ret = fgets(buf, buflen, fp);126while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {127// retry with a larger buffer128buf = REALLOC_RESOURCE_ARRAY(char, buf, buflen, buflen * 2);129buflen *= 2;130// rewind to beginning of line131fseek(fp, pos, SEEK_SET);132// retry read with new buffer133ret = fgets(buf, buflen, fp);134}135return ret;136}137138static bool file_contains_substrings_in_order(const char* filename, const char* substrs[]) {139AsyncLogWriter::flush();140FILE* fp = fopen(filename, "r");141assert(fp != NULL, "error opening file %s: %s", filename, strerror(errno));142143size_t idx = 0;144while (substrs[idx] != NULL) {145ResourceMark rm;146char* line = read_line(fp);147if (line == NULL) {148break;149}150for (char* match = strstr(line, substrs[idx]); match != NULL;) {151size_t match_len = strlen(substrs[idx]);152idx++;153if (substrs[idx] == NULL) {154break;155}156match = strstr(match + match_len, substrs[idx]);157}158}159160fclose(fp);161return substrs[idx] == NULL;162}163164static inline bool file_contains_substring(const char* filename, const char* substr) {165const char* strs[] = {substr, NULL};166return file_contains_substrings_in_order(filename, strs);167}168169170