1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 #include "precompiled.hpp" 25 #include "gc/shared/gcTraceTime.inline.hpp" 26 #include "logTestFixture.hpp" 27 #include "logTestUtils.inline.hpp" 28 #include "logging/log.hpp" 29 #include "prims/jvm.h" 30 #include "unittest.hpp" 31 32 class LogTest : public LogTestFixture { 33 }; 34 35 #define LOG_PREFIX_STR "THE_PREFIX " 36 #define LOG_LINE_STR "a log line" 37 38 size_t Test_log_prefix_prefixer(char* buf, size_t len) { 39 int ret = jio_snprintf(buf, len, LOG_PREFIX_STR); 40 assert(ret > 0, "Failed to print prefix. Log buffer too small?"); 41 return (size_t) ret; 42 } 43 44 #ifdef ASSERT // 'test' tag is debug only 45 TEST_VM_F(LogTest, prefix) { 46 set_log_config(TestLogFileName, "logging+test=trace"); 47 log_trace(logging, test)(LOG_LINE_STR); 48 EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_PREFIX_STR LOG_LINE_STR)); 49 } 50 #endif 51 52 TEST_VM_F(LogTest, large_message) { 53 char big_msg[4096] = {0}; 54 char Xchar = '~'; 55 56 set_log_config(TestLogFileName, "logging=trace"); 57 58 memset(big_msg, Xchar, sizeof(big_msg) - 1); 59 log_trace(logging)("%s", big_msg); 60 61 ResourceMark rm; 62 FILE* fp = fopen(TestLogFileName, "r"); 63 ASSERT_NE((void*)NULL, fp); 64 char* output = read_line(fp); 65 fclose(fp); 66 67 size_t count = 0; 68 for (size_t ps = 0 ; output[ps + count] != '\0'; output[ps + count] == Xchar ? count++ : ps++); 69 EXPECT_EQ(sizeof(big_msg) - 1, count); 70 } 71 72 TEST_VM_F(LogTest, enabled_logtarget) { 73 set_log_config(TestLogFileName, "gc=debug"); 74 75 LogTarget(Debug, gc) log; 76 EXPECT_TRUE(log.is_enabled()); 77 78 // Log the line and expect it to be available in the output file. 79 log.print(LOG_TEST_STRING_LITERAL); 80 81 EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL)); 82 } 83 84 TEST_VM_F(LogTest, disabled_logtarget) { 85 set_log_config(TestLogFileName, "gc=info"); 86 87 LogTarget(Debug, gc) log; 88 EXPECT_FALSE(log.is_enabled()); 89 90 // Try to log, but expect this to be filtered out. 91 log.print(LOG_TEST_STRING_LITERAL); 92 93 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 94 log_info(gc)("Dummy line"); 95 96 EXPECT_FALSE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL)); 97 } 98 99 TEST_VM_F(LogTest, enabled_loghandle) { 100 set_log_config(TestLogFileName, "gc=debug"); 101 102 Log(gc) log; 103 LogHandle log_handle(log); 104 105 EXPECT_TRUE(log_handle.is_debug()); 106 107 // Try to log through a LogHandle. 108 log_handle.debug("%d workers", 3); 109 110 EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers")); 111 } 112 113 TEST_VM_F(LogTest, disabled_loghandle) { 114 set_log_config(TestLogFileName, "gc=info"); 115 116 Log(gc) log; 117 LogHandle log_handle(log); 118 119 EXPECT_FALSE(log_handle.is_debug()); 120 121 // Try to log through a LogHandle. 122 log_handle.debug("%d workers", 3); 123 124 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 125 log_info(gc)("Dummy line"); 126 127 EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers")); 128 } 129 130 TEST_VM_F(LogTest, enabled_logtargethandle) { 131 set_log_config(TestLogFileName, "gc=debug"); 132 133 LogTarget(Debug, gc) log; 134 LogTargetHandle log_handle(log); 135 136 EXPECT_TRUE(log_handle.is_enabled()); 137 138 // Try to log through a LogHandle. 139 log_handle.print("%d workers", 3); 140 141 EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers")); 142 } 143 144 TEST_VM_F(LogTest, disabled_logtargethandle) { 145 set_log_config(TestLogFileName, "gc=info"); 146 147 LogTarget(Debug, gc) log; 148 LogTargetHandle log_handle(log); 149 150 EXPECT_FALSE(log_handle.is_enabled()); 151 152 // Try to log through a LogHandle. 153 log_handle.print("%d workers", 3); 154 155 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 156 log_info(gc)("Dummy line"); 157 158 EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers")); 159 }