1 /* 2 * Copyright (c) 2015, 2016, 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 "unittest.hpp" 30 31 class LogTest : public LogTestFixture { 32 }; 33 34 #define LOG_PREFIX_STR "THE_PREFIX " 35 #define LOG_LINE_STR "a log line" 36 37 size_t Test_log_prefix_prefixer(char* buf, size_t len) { 38 int ret = jio_snprintf(buf, len, LOG_PREFIX_STR); 39 assert(ret > 0, "Failed to print prefix. Log buffer too small?"); 40 return (size_t) ret; 41 } 42 43 #ifdef ASSERT // 'test' tag is debug only 44 TEST_VM_F(LogTest, prefix) { 45 set_log_config(TestLogFileName, "logging+test=trace"); 46 log_trace(logging, test)(LOG_LINE_STR); 47 EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_PREFIX_STR LOG_LINE_STR)); 48 } 49 #endif 50 51 TEST_VM_F(LogTest, large_message) { 52 char big_msg[4096] = {0}; 53 char Xchar = '~'; 54 55 set_log_config(TestLogFileName, "logging=trace"); 56 57 memset(big_msg, Xchar, sizeof(big_msg) - 1); 58 log_trace(logging)("%s", big_msg); 59 60 ResourceMark rm; 61 FILE* fp = fopen(TestLogFileName, "r"); 62 ASSERT_NE((void*)NULL, fp); 63 char* output = read_line(fp); 64 fclose(fp); 65 66 size_t count = 0; 67 for (size_t ps = 0 ; output[ps + count] != '\0'; output[ps + count] == Xchar ? count++ : ps++); 68 EXPECT_EQ(sizeof(big_msg) - 1, count); 69 } 70 71 TEST_VM_F(LogTest, enabled_logtarget) { 72 set_log_config(TestLogFileName, "gc=debug"); 73 74 LogTarget(Debug, gc) log; 75 EXPECT_TRUE(log.is_enabled()); 76 77 // Log the line and expect it to be available in the output file. 78 log.print(LOG_TEST_STRING_LITERAL); 79 80 EXPECT_TRUE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL)); 81 } 82 83 TEST_VM_F(LogTest, disabled_logtarget) { 84 set_log_config(TestLogFileName, "gc=info"); 85 86 LogTarget(Debug, gc) log; 87 EXPECT_FALSE(log.is_enabled()); 88 89 // Try to log, but expect this to be filtered out. 90 log.print(LOG_TEST_STRING_LITERAL); 91 92 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 93 log_info(gc)("Dummy line"); 94 95 EXPECT_FALSE(file_contains_substring(TestLogFileName, LOG_TEST_STRING_LITERAL)); 96 } 97 98 TEST_VM_F(LogTest, enabled_loghandle) { 99 set_log_config(TestLogFileName, "gc=debug"); 100 101 Log(gc) log; 102 LogHandle log_handle(log); 103 104 EXPECT_TRUE(log_handle.is_debug()); 105 106 // Try to log through a LogHandle. 107 log_handle.debug("%d workers", 3); 108 109 EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers")); 110 } 111 112 TEST_VM_F(LogTest, disabled_loghandle) { 113 set_log_config(TestLogFileName, "gc=info"); 114 115 Log(gc) log; 116 LogHandle log_handle(log); 117 118 EXPECT_FALSE(log_handle.is_debug()); 119 120 // Try to log through a LogHandle. 121 log_handle.debug("%d workers", 3); 122 123 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 124 log_info(gc)("Dummy line"); 125 126 EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers")); 127 } 128 129 TEST_VM_F(LogTest, enabled_logtargethandle) { 130 set_log_config(TestLogFileName, "gc=debug"); 131 132 LogTarget(Debug, gc) log; 133 LogTargetHandle log_handle(log); 134 135 EXPECT_TRUE(log_handle.is_enabled()); 136 137 // Try to log through a LogHandle. 138 log_handle.print("%d workers", 3); 139 140 EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers")); 141 } 142 143 TEST_VM_F(LogTest, disabled_logtargethandle) { 144 set_log_config(TestLogFileName, "gc=info"); 145 146 LogTarget(Debug, gc) log; 147 LogTargetHandle log_handle(log); 148 149 EXPECT_FALSE(log_handle.is_enabled()); 150 151 // Try to log through a LogHandle. 152 log_handle.print("%d workers", 3); 153 154 // Log a dummy line so that fgets doesn't return NULL because the file is empty. 155 log_info(gc)("Dummy line"); 156 157 EXPECT_FALSE(file_contains_substring(TestLogFileName, "3 workers")); 158 }