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