Path: blob/master/test/hotspot/gtest/logging/test_log.cpp
41145 views
/*1* Copyright (c) 2015, 2017, 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*22*/23#include "precompiled.hpp"24#include "jvm.h"25#include "gc/shared/gcTraceTime.inline.hpp"26#include "logTestFixture.hpp"27#include "logTestUtils.inline.hpp"28#include "logging/log.hpp"29#include "unittest.hpp"3031class LogTest : public LogTestFixture {32};3334#define LOG_PREFIX_STR "THE_PREFIX "35#define LOG_LINE_STR "a log line"3637size_t Test_log_prefix_prefixer(char* buf, size_t len) {38int ret = jio_snprintf(buf, len, LOG_PREFIX_STR);39assert(ret > 0, "Failed to print prefix. Log buffer too small?");40return (size_t) ret;41}4243#ifdef ASSERT // 'test' tag is debug only44TEST_VM_F(LogTest, prefix) {45set_log_config(TestLogFileName, "logging+test=trace");46log_trace(logging, test)(LOG_LINE_STR);47EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_PREFIX_STR LOG_LINE_STR));48}49#endif5051TEST_VM_F(LogTest, large_message) {52char big_msg[4096] = {0};53char Xchar = '~';5455set_log_config(TestLogFileName, "logging=trace");5657memset(big_msg, Xchar, sizeof(big_msg) - 1);58log_trace(logging)("%s", big_msg);5960AsyncLogWriter::flush();61ResourceMark rm;62FILE* fp = fopen(TestLogFileName, "r");63ASSERT_NE((void*)NULL, fp);64char* output = read_line(fp);65fclose(fp);6667size_t count = 0;68for (size_t ps = 0 ; output[ps + count] != '\0'; output[ps + count] == Xchar ? count++ : ps++);69EXPECT_EQ(sizeof(big_msg) - 1, count);70}7172TEST_VM_F(LogTest, enabled_logtarget) {73set_log_config(TestLogFileName, "gc=debug");7475LogTarget(Debug, gc) log;76EXPECT_TRUE(log.is_enabled());7778// Log the line and expect it to be available in the output file.79log.print(LOG_TEST_STRING_LITERAL);8081EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL));82}8384TEST_VM_F(LogTest, disabled_logtarget) {85set_log_config(TestLogFileName, "gc=info");8687LogTarget(Debug, gc) log;88EXPECT_FALSE(log.is_enabled());8990// Try to log, but expect this to be filtered out.91log.print(LOG_TEST_STRING_LITERAL);9293// Log a dummy line so that fgets doesn't return NULL because the file is empty.94log_info(gc)("Dummy line");9596EXPECT_FALSE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL));97}9899TEST_VM_F(LogTest, enabled_loghandle) {100set_log_config(TestLogFileName, "gc=debug");101102Log(gc) log;103LogHandle log_handle(log);104105EXPECT_TRUE(log_handle.is_debug());106107// Try to log through a LogHandle.108log_handle.debug("%d workers", 3);109110EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers"));111}112113TEST_VM_F(LogTest, disabled_loghandle) {114set_log_config(TestLogFileName, "gc=info");115116Log(gc) log;117LogHandle log_handle(log);118119EXPECT_FALSE(log_handle.is_debug());120121// Try to log through a LogHandle.122log_handle.debug("%d workers", 3);123124// Log a dummy line so that fgets doesn't return NULL because the file is empty.125log_info(gc)("Dummy line");126127EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers"));128}129130TEST_VM_F(LogTest, enabled_logtargethandle) {131set_log_config(TestLogFileName, "gc=debug");132133LogTarget(Debug, gc) log;134LogTargetHandle log_handle(log);135136EXPECT_TRUE(log_handle.is_enabled());137138// Try to log through a LogHandle.139log_handle.print("%d workers", 3);140141EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers"));142}143144TEST_VM_F(LogTest, disabled_logtargethandle) {145set_log_config(TestLogFileName, "gc=info");146147LogTarget(Debug, gc) log;148LogTargetHandle log_handle(log);149150EXPECT_FALSE(log_handle.is_enabled());151152// Try to log through a LogHandle.153log_handle.print("%d workers", 3);154155// Log a dummy line so that fgets doesn't return NULL because the file is empty.156log_info(gc)("Dummy line");157158EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers"));159}160161162