hotspot/src/share/vm/logging/log.cpp
author mlarsson
Mon, 11 Apr 2016 12:22:09 +0200
changeset 37465 1d5551f466ee
parent 37463 a4581cbe32cd
child 38263 a7488329ad27
permissions -rw-r--r--
8146879: Add option for handling existing log files in UL Reviewed-by: dsamersoff, gziemski, rehn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33766
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     1
/*
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
33766
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     4
 *
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     8
 *
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    13
 * accompanied this code).
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    14
 *
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    18
 *
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    21
 * questions.
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    22
 *
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    23
 */
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    24
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    25
#include "precompiled.hpp"
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    26
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    27
/////////////// Unit tests ///////////////
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    28
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    29
#ifndef PRODUCT
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    30
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
    31
#include "gc/shared/gcTraceTime.inline.hpp"
33766
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    32
#include "logging/log.hpp"
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    33
#include "logging/logConfiguration.hpp"
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
    34
#include "logging/logFileOutput.hpp"
35950
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    35
#include "logging/logOutput.hpp"
37463
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    36
#include "logging/logTagLevelExpression.hpp"
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    37
#include "logging/logTagSet.hpp"
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
    38
#include "logging/logStream.inline.hpp"
33766
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    39
#include "memory/resourceArea.hpp"
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
    40
35950
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    41
#define assert_str_eq(s1, s2) \
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    42
  assert(strcmp(s1, s2) == 0, "Expected '%s' to equal '%s'", s1, s2)
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    43
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    44
#define assert_char_in(c, s) \
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    45
  assert(strchr(s, c) != NULL, "Expected '%s' to contain character '%c'", s, c)
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    46
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    47
#define assert_char_not_in(c, s) \
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    48
  assert(strchr(s, c) == NULL, "Expected '%s' to *not* contain character '%c'", s, c)
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
    49
37463
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    50
void Test_log_tag_combinations_limit() {
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    51
  assert(LogTagLevelExpression::MaxCombinations > LogTagSet::ntagsets(),
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    52
      "Combination limit (" SIZE_FORMAT ") not sufficient "
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    53
      "for configuring all available tag sets (" SIZE_FORMAT ")",
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    54
      LogTagLevelExpression::MaxCombinations, LogTagSet::ntagsets());
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    55
}
a4581cbe32cd 8153731: Increase max tag combinations for UL expression (config)
rehn
parents: 37461
diff changeset
    56
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    57
class TestLogFile {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    58
 private:
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    59
  char file_name[256];
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    60
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    61
  void set_name(const char* test_name) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    62
    const char* tmpdir = os::get_temp_directory();
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    63
    int pos = jio_snprintf(file_name, sizeof(file_name), "%s%svmtest.%s.%d.log", tmpdir, os::file_separator(), test_name, os::current_process_id());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    64
    assert(pos > 0, "too small log file name buffer");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    65
    assert((size_t)pos < sizeof(file_name), "too small log file name buffer");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    66
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    67
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    68
 public:
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    69
  TestLogFile(const char* test_name) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    70
    set_name(test_name);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    71
    remove(name());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    72
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    73
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    74
  ~TestLogFile() {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    75
    remove(name());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    76
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    77
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    78
  const char* name() {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    79
    return file_name;
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    80
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    81
};
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    82
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    83
class TestLogSavedConfig {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    84
 private:
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    85
  char* _saved_config;
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    86
  char* _new_output;
37242
91e5f98fff6f 8152632: Rename LogHandle(...) to Log(...)
stefank
parents: 37201
diff changeset
    87
  Log(logging) _log;
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    88
 public:
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    89
  TestLogSavedConfig(const char* apply_output = NULL, const char* apply_setting = NULL) : _new_output(0) {
37245
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
    90
    ResourceMark rm;
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    91
    _saved_config = os::strdup_check_oom(LogOutput::Stdout->config_string());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    92
    bool success = LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, _log.error_stream());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    93
    assert(success, "test unable to turn all off");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    94
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    95
    if (apply_output) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    96
      _new_output = os::strdup_check_oom(apply_output);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    97
      bool success = LogConfiguration::parse_log_arguments(_new_output, apply_setting,  NULL, NULL, _log.error_stream());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    98
      assert(success, "test unable to apply test log configuration");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
    99
    }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   100
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   101
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   102
  ~TestLogSavedConfig() {
37245
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   103
    ResourceMark rm;
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   104
    if (_new_output) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   105
      bool success = LogConfiguration::parse_log_arguments(_new_output, "all=off", NULL, NULL, _log.error_stream());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   106
      assert(success, "test unable to turn all off");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   107
      os::free(_new_output);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   108
    }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   109
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   110
    bool success = LogConfiguration::parse_log_arguments("stdout", _saved_config, NULL, NULL, _log.error_stream());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   111
    assert(success, "test unable to restore log configuration");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   112
    os::free(_saved_config);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   113
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   114
};
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   115
35852
15ca44d4ae0c 8147348: LogTagLevelExpression not properly initialized in configure_stdout
mlarsson
parents: 35228
diff changeset
   116
void Test_configure_stdout() {
35950
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   117
  LogOutput* stdoutput = LogOutput::Stdout;
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   118
  TestLogSavedConfig tlsc;
35950
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   119
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   120
  // Enable 'logging=info', verifying it has been set
35852
15ca44d4ae0c 8147348: LogTagLevelExpression not properly initialized in configure_stdout
mlarsson
parents: 35228
diff changeset
   121
  LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(logging));
37239
a6cda2376a19 8153174: Remove trailing comma from log configuration listings
mlarsson
parents: 37201
diff changeset
   122
  assert_str_eq("logging=info", stdoutput->config_string());
35950
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   123
  assert(log_is_enabled(Info, logging), "logging was not properly enabled");
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   124
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   125
  // Enable 'gc=debug' (no wildcard), verifying no other tags are enabled
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   126
  LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   127
  // No '+' character means only single tags are enabled, and no combinations
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   128
  assert_char_not_in('+', stdoutput->config_string());
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   129
  assert(log_is_enabled(Debug, gc), "logging was not properly enabled");
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   130
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   131
  // Enable 'gc*=trace' (with wildcard), verifying at least one tag combination is enabled (gc+...)
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   132
  LogConfiguration::configure_stdout(LogLevel::Trace, false, LOG_TAGS(gc));
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   133
  assert_char_in('+', stdoutput->config_string());
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   134
  assert(log_is_enabled(Trace, gc), "logging was not properly enabled");
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   135
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   136
  // Disable 'gc*' and 'logging', verifying all logging is properly disabled
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   137
  LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   138
  LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
c6142cb6bc4a 8149112: configure_stdout test depends on VM arguments
mlarsson
parents: 35852
diff changeset
   139
  assert_str_eq("all=off", stdoutput->config_string());
35852
15ca44d4ae0c 8147348: LogTagLevelExpression not properly initialized in configure_stdout
mlarsson
parents: 35228
diff changeset
   140
}
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   141
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   142
static size_t number_of_lines_with_substring_in_file(const char* filename,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   143
                                                     const char* substr) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   144
  ResourceMark rm;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   145
  size_t ret = 0;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   146
  FILE* fp = fopen(filename, "r");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   147
  assert(fp != NULL, "error opening file %s: %s", filename, strerror(errno));
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   148
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   149
  int buflen = 512;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   150
  char* buf = NEW_RESOURCE_ARRAY(char, buflen);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   151
  long pos = 0;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   152
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   153
  while (fgets(buf, buflen, fp) != NULL) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   154
    if (buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   155
      // retry with a larger buffer
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   156
      buf = REALLOC_RESOURCE_ARRAY(char, buf, buflen, buflen * 2);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   157
      buflen *= 2;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   158
      // rewind to beginning of line
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   159
      fseek(fp, pos, SEEK_SET);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   160
      continue;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   161
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   162
    pos = ftell(fp);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   163
    if (strstr(buf, substr) != NULL) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   164
      ret++;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   165
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   166
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   167
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   168
  fclose(fp);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   169
  return ret;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   170
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   171
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   172
static bool file_exists(const char* filename) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   173
  struct stat st;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   174
  return os::stat(filename, &st) == 0;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   175
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   176
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   177
static void delete_file(const char* filename) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   178
  if (!file_exists(filename)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   179
    return;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   180
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   181
  int ret = remove(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   182
  assert(ret == 0, "failed to remove file '%s': %s", filename, strerror(errno));
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   183
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   184
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   185
static void create_directory(const char* name) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   186
  assert(!file_exists(name), "can't create directory: %s already exists", name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   187
  bool failed;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   188
#ifdef _WINDOWS
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   189
  failed = !CreateDirectory(name, NULL);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   190
#else
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   191
  failed = mkdir(name, 0777);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   192
#endif
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   193
  assert(!failed, "failed to create directory %s", name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   194
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   195
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   196
static const char* ExpectedLine = "a (hopefully) unique log line for testing";
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   197
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   198
static void init_file(const char* filename, const char* options = "") {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   199
  LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   200
                                        Log(logging)::error_stream());
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   201
  log_debug(logging)("%s", ExpectedLine);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   202
  LogConfiguration::parse_log_arguments(filename, "all=off", "", "",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   203
                                        Log(logging)::error_stream());
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   204
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   205
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   206
void Test_log_file_startup_rotation() {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   207
  ResourceMark rm;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   208
  const size_t rotations = 5;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   209
  const char* filename = "start-rotate-test";
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   210
  char* rotated_file[rotations];
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   211
  for (size_t i = 0; i < rotations; i++) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   212
    size_t len = strlen(filename) + 3;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   213
    rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   214
    jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   215
    delete_file(rotated_file[i]);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   216
  };
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   217
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   218
  delete_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   219
  init_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   220
  assert(file_exists(filename),
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   221
         "configured logging to file '%s' but file was not found", filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   222
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   223
  // Initialize the same file a bunch more times to trigger rotations
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   224
  for (size_t i = 0; i < rotations; i++) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   225
    init_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   226
    assert(file_exists(rotated_file[i]), "existing file was not rotated");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   227
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   228
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   229
  // Remove a file and expect its slot to be re-used
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   230
  delete_file(rotated_file[1]);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   231
  init_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   232
  assert(file_exists(rotated_file[1]), "log file not properly rotated");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   233
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   234
  // Clean up after test
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   235
  delete_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   236
  for (size_t i = 0; i < rotations; i++) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   237
    delete_file(rotated_file[i]);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   238
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   239
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   240
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   241
void Test_log_file_startup_truncation() {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   242
  ResourceMark rm;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   243
  const char* filename = "start-truncate-test";
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   244
  const char* archived_filename = "start-truncate-test.0";
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   245
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   246
  delete_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   247
  delete_file(archived_filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   248
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   249
  // Use the same log file twice and expect it to be overwritten/truncated
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   250
  init_file(filename, "filecount=0");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   251
  assert(file_exists(filename), "couldn't find log file: %s", filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   252
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   253
  init_file(filename, "filecount=0");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   254
  assert(file_exists(filename), "couldn't find log file: %s", filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   255
  assert(!file_exists(archived_filename),
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   256
         "existing log file %s was not properly truncated when filecount was 0",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   257
         filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   258
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   259
  // Verify that the file was really truncated and not just appended
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   260
  assert(number_of_lines_with_substring_in_file(filename, ExpectedLine) == 1,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   261
         "log file %s appended rather than truncated", filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   262
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   263
  delete_file(filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   264
  delete_file(archived_filename);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   265
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   266
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   267
static int Test_logconfiguration_subscribe_triggered = 0;
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   268
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   269
static void Test_logconfiguration_subscribe_helper() {
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   270
  Test_logconfiguration_subscribe_triggered++;
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   271
}
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   272
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   273
void Test_logconfiguration_subscribe() {
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   274
  ResourceMark rm;
37242
91e5f98fff6f 8152632: Rename LogHandle(...) to Log(...)
stefank
parents: 37201
diff changeset
   275
  Log(logging) log;
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   276
37245
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   277
  TestLogSavedConfig log_cfg("stdout", "logging*=trace");
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   278
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   279
  LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   280
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   281
  LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   282
  assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   283
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   284
  LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   285
  assert(Test_logconfiguration_subscribe_triggered == 2, "subscription not triggered (2)");
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   286
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   287
  LogConfiguration::disable_logging();
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   288
  assert(Test_logconfiguration_subscribe_triggered == 3, "subscription not triggered (3)");
37201
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   289
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   290
  // We need to renable stderr error logging since "disable_logging" disable it all.
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   291
  // TestLogSavedConfig log_cfg will only renable stdout for us.
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   292
  LogConfiguration::parse_log_arguments("stderr", "all=warning", NULL, NULL, log.error_stream());
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   293
  assert(Test_logconfiguration_subscribe_triggered == 4, "subscription not triggered (3)");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   294
}
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   295
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   296
#define LOG_PREFIX_STR "THE_PREFIX "
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   297
#define LOG_LINE_STR "a log line"
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   298
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   299
size_t Test_log_prefix_prefixer(char* buf, size_t len) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   300
  int ret = jio_snprintf(buf, len, LOG_PREFIX_STR);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   301
  assert(ret > 0, "Failed to print prefix. Log buffer too small?");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   302
  return (size_t) ret;
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   303
}
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   304
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   305
void Test_log_prefix() {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   306
  TestLogFile log_file("log_prefix");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   307
  TestLogSavedConfig log_cfg(log_file.name(), "logging+test=trace");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   308
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   309
  log_trace(logging, test)(LOG_LINE_STR);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   310
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   311
  FILE* fp = fopen(log_file.name(), "r");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   312
  assert(fp, "File read error");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   313
  char output[1024];
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   314
  if (fgets(output, 1024, fp) != NULL) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   315
    assert(strstr(output, LOG_PREFIX_STR LOG_LINE_STR), "logging prefix error");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   316
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   317
  fclose(fp);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   318
}
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   319
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   320
void Test_log_big() {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   321
  char big_msg[4096] = {0};
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   322
  char Xchar = '~';
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   323
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   324
  TestLogFile log_file("log_big");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   325
  TestLogSavedConfig log_cfg(log_file.name(), "logging+test=trace");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   326
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   327
  memset(big_msg, Xchar, sizeof(big_msg) - 1);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   328
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   329
  log_trace(logging, test)("%s", big_msg);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   330
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   331
  FILE* fp = fopen(log_file.name(), "r");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   332
  assert(fp, "File read error");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   333
  char output[sizeof(big_msg)+128 /*decorators*/ ];
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   334
  if (fgets(output, sizeof(output), fp) != NULL) {
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   335
    assert(strstr(output, LOG_PREFIX_STR), "logging prefix error");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   336
    size_t count = 0;
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   337
    for (size_t ps = 0 ; output[ps + count] != '\0'; output[ps + count] == Xchar ? count++ : ps++);
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   338
    assert(count == (sizeof(big_msg) - 1) , "logging msg error");
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   339
  }
928cf689af1a 8151993: Remove inclusion of inline.hpp in log.hpp
mlarsson
parents: 37200
diff changeset
   340
  fclose(fp);
37108
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   341
}
fe800ec86077 8151264: Add a notification mechanism for UL configuration changes.
rehn
parents: 35950
diff changeset
   342
37200
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   343
void Test_logtagset_duplicates() {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   344
  for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   345
    char ts_name[512];
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   346
    ts->label(ts_name, sizeof(ts_name), ",");
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   347
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   348
    // verify that NO_TAG is never followed by a real tag
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   349
    for (size_t i = 0; i < LogTag::MaxTags; i++) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   350
      if (ts->tag(i) == LogTag::__NO_TAG) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   351
        for (i++; i < LogTag::MaxTags; i++) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   352
          assert(ts->tag(i) == LogTag::__NO_TAG,
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   353
                 "NO_TAG was followed by a real tag (%s) in tagset %s",
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   354
                 LogTag::name(ts->tag(i)), ts_name);
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   355
        }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   356
      }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   357
    }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   358
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   359
    // verify that there are no duplicate tagsets (same tags in different order)
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   360
    for (LogTagSet* other = ts->next(); other != NULL; other = other->next()) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   361
      if (ts->ntags() != other->ntags()) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   362
        continue;
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   363
      }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   364
      bool equal = true;
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   365
      for (size_t i = 0; i < ts->ntags(); i++) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   366
        LogTagType tag = ts->tag(i);
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   367
        if (!other->contains(tag)) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   368
          equal = false;
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   369
          break;
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   370
        }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   371
      }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   372
      // Since tagsets are implemented using template arguments, using both of
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   373
      // the (logically equivalent) tagsets (t1, t2) and (t2, t1) somewhere will
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   374
      // instantiate two different LogTagSetMappings. This causes multiple
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   375
      // tagset instances to be created for the same logical set. We want to
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   376
      // avoid this to save time, memory and prevent any confusion around it.
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   377
      if (equal) {
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   378
        char other_name[512];
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   379
        other->label(other_name, sizeof(other_name), ",");
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   380
        assert(false, "duplicate LogTagSets found: '%s' vs '%s' "
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   381
               "(tags must always be specified in the same order for each tagset)",
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   382
               ts_name, other_name);
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   383
      }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   384
    }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   385
  }
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   386
}
e601cf49b2d8 8151438: UL instantiates duplicate tag sets
mlarsson
parents: 37108
diff changeset
   387
37245
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   388
#define Test_logtarget_string_literal "First line"
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   389
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   390
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   391
static void Test_logtarget_on() {
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   392
  TestLogFile log_file("log_target");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   393
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   394
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   395
  LogTarget(Debug, gc) log;
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   396
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   397
  assert(log.is_enabled(), "assert");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   398
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   399
  // Log the line and expect it to be available in the output file.
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   400
  log.print(Test_logtarget_string_literal);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   401
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   402
  FILE* fp = fopen(log_file.name(), "r");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   403
  assert(fp != NULL, "File read error");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   404
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   405
  char output[256 /* Large enough buffer */];
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   406
  char* res = fgets(output, sizeof(output), fp);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   407
  assert(res != NULL, "assert");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   408
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   409
  assert(strstr(output, Test_logtarget_string_literal) != NULL, "log line missing");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   410
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   411
  fclose(fp);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   412
}
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   413
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   414
static void Test_logtarget_off() {
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   415
  TestLogFile log_file("log_target");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   416
  TestLogSavedConfig tlsc(log_file.name(), "gc=info");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   417
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   418
  LogTarget(Debug, gc) log;
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   419
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   420
  if (log.is_enabled()) {
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   421
    // The log config could have been redirected gc=debug to a file. If gc=debug
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   422
    // is enabled, we can only test that the LogTarget returns the same value
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   423
    // as the log_is_enabled function. The rest of the test will be ignored.
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   424
    assert(log.is_enabled() == log_is_enabled(Debug, gc), "assert");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   425
    log_warning(logging)("This test doesn't support runs with -Xlog");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   426
    return;
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   427
  }
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   428
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   429
  // Try to log, but expect this to be filtered out.
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   430
  log.print(Test_logtarget_string_literal);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   431
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   432
  // Log a dummy line so that fgets doesn't return NULL because the file is empty.
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   433
  log_info(gc)("Dummy line");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   434
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   435
  FILE* fp = fopen(log_file.name(), "r");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   436
  assert(fp != NULL, "File read error");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   437
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   438
  char output[256 /* Large enough buffer */];
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   439
  char* res = fgets(output, sizeof(output), fp);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   440
  assert(res != NULL, "assert");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   441
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   442
  assert(strstr(output, Test_logtarget_string_literal) == NULL, "log line not missing");
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   443
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   444
  fclose(fp);
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   445
}
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   446
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   447
void Test_logtarget() {
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   448
  Test_logtarget_on();
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   449
  Test_logtarget_off();
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   450
}
70413a0266d4 8152635: Create a UL class to represent a Log + Level combination
stefank
parents: 37243
diff changeset
   451
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   452
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   453
static void Test_logstream_helper(outputStream* stream) {
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   454
  TestLogFile log_file("log_stream");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   455
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   456
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   457
  // Try to log, but expect this to be filtered out.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   458
  stream->print("%d ", 3); stream->print("workers"); stream->cr();
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   459
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   460
  FILE* fp = fopen(log_file.name(), "r");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   461
  assert(fp != NULL, "File read error");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   462
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   463
  char output[256 /* Large enough buffer */];
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   464
  char* res = fgets(output, sizeof(output), fp);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   465
  assert(res != NULL, "assert");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   466
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   467
  assert(strstr(output, "3 workers") != NULL, "log line missing");
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   468
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   469
  fclose(fp);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   470
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   471
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   472
static void Test_logstream_log() {
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   473
  Log(gc) log;
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   474
  LogStream stream(log.debug());
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   475
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   476
  Test_logstream_helper(&stream);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   477
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   478
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   479
static void Test_logstream_logtarget() {
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   480
  LogTarget(Debug, gc) log;
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   481
  LogStream stream(log);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   482
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   483
  Test_logstream_helper(&stream);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   484
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   485
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   486
static void Test_logstream_logstreamhandle() {
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   487
  LogStreamHandle(Debug, gc) stream;
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   488
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   489
  Test_logstream_helper(&stream);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   490
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   491
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   492
static void Test_logstream_no_rm() {
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   493
  ResourceMark rm;
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   494
  outputStream* stream = LogTarget(Debug, gc)::stream();
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   495
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   496
  Test_logstream_helper(stream);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   497
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   498
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   499
static void Test_logstreamcheap_log() {
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   500
  Log(gc) log;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   501
  LogStreamCHeap stream(log.debug());
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   502
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   503
  Test_logstream_helper(&stream);
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   504
}
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   505
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   506
static void Test_logstreamcheap_logtarget() {
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   507
  LogTarget(Debug, gc) log;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   508
  LogStreamCHeap stream(log);
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   509
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   510
  Test_logstream_helper(&stream);
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   511
}
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   512
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   513
void Test_logstream() {
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   514
  // Test LogStreams with embedded ResourceMark.
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   515
  Test_logstream_log();
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   516
  Test_logstream_logtarget();
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   517
  Test_logstream_logstreamhandle();
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   518
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   519
  // Test LogStreams without embedded ResourceMark.
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   520
  Test_logstream_no_rm();
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   521
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   522
  // Test LogStreams backed by CHeap memory.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   523
  Test_logstreamcheap_log();
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37430
diff changeset
   524
  Test_logstreamcheap_logtarget();
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   525
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents: 37247
diff changeset
   526
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   527
void Test_loghandle_on() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   528
  TestLogFile log_file("log_handle");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   529
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   530
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   531
  Log(gc) log;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   532
  LogHandle log_handle(log);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   533
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   534
  assert(log_handle.is_debug(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   535
37430
fd743dadef12 8151939: VM_Version_init() print buffer is too small
coleenp
parents: 37256
diff changeset
   536
  // Try to log through a LogHandle.
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   537
  log_handle.debug("%d workers", 3);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   538
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   539
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   540
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   541
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   542
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   543
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   544
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   545
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   546
  assert(strstr(output, "3 workers") != NULL, "log line missing");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   547
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   548
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   549
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   550
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   551
void Test_loghandle_off() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   552
  TestLogFile log_file("log_handle");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   553
  TestLogSavedConfig tlsc(log_file.name(), "gc=info");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   554
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   555
  Log(gc) log;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   556
  LogHandle log_handle(log);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   557
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   558
  if (log_handle.is_debug()) {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   559
    // The log config could have been redirected gc=debug to a file. If gc=debug
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   560
    // is enabled, we can only test that the LogTarget returns the same value
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   561
    // as the log_is_enabled function. The rest of the test will be ignored.
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   562
    assert(log_handle.is_debug() == log_is_enabled(Debug, gc), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   563
    log_warning(logging)("This test doesn't support runs with -Xlog");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   564
    return;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   565
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   566
37430
fd743dadef12 8151939: VM_Version_init() print buffer is too small
coleenp
parents: 37256
diff changeset
   567
  // Try to log through a LogHandle. Should fail, since only info is turned on.
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   568
  log_handle.debug("%d workers", 3);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   569
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   570
  // Log a dummy line so that fgets doesn't return NULL because the file is empty.
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   571
  log_info(gc)("Dummy line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   572
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   573
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   574
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   575
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   576
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   577
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   578
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   579
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   580
  assert(strstr(output, "3 workers") == NULL, "log line missing");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   581
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   582
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   583
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   584
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   585
void Test_loghandle() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   586
  Test_loghandle_on();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   587
  Test_loghandle_off();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   588
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   589
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   590
static void Test_logtargethandle_on() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   591
  TestLogFile log_file("log_handle");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   592
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   593
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   594
  LogTarget(Debug, gc) log;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   595
  LogTargetHandle log_handle(log);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   596
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   597
  assert(log_handle.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   598
37430
fd743dadef12 8151939: VM_Version_init() print buffer is too small
coleenp
parents: 37256
diff changeset
   599
  // Try to log through a LogHandle.
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   600
  log_handle.print("%d workers", 3);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   601
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   602
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   603
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   604
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   605
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   606
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   607
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   608
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   609
  assert(strstr(output, "3 workers") != NULL, "log line missing");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   610
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   611
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   612
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   613
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   614
static void Test_logtargethandle_off() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   615
  TestLogFile log_file("log_handle");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   616
  TestLogSavedConfig tlsc(log_file.name(), "gc=info");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   617
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   618
  LogTarget(Debug, gc) log;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   619
  LogTargetHandle log_handle(log);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   620
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   621
  if (log_handle.is_enabled()) {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   622
    // The log config could have been redirected gc=debug to a file. If gc=debug
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   623
    // is enabled, we can only test that the LogTarget returns the same value
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   624
    // as the log_is_enabled function. The rest of the test will be ignored.
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   625
    assert(log_handle.is_enabled() == log_is_enabled(Debug, gc), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   626
    log_warning(logging)("This test doesn't support runs with -Xlog");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   627
    return;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   628
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   629
37430
fd743dadef12 8151939: VM_Version_init() print buffer is too small
coleenp
parents: 37256
diff changeset
   630
  // Try to log through a LogHandle. Should fail, since only info is turned on.
37255
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   631
  log_handle.print("%d workers", 3);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   632
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   633
  // Log a dummy line so that fgets doesn't return NULL because the file is empty.
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   634
  log_info(gc)("Dummy line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   635
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   636
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   637
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   638
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   639
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   640
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   641
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   642
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   643
  assert(strstr(output, "3 workers") == NULL, "log line missing");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   644
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   645
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   646
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   647
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   648
void Test_logtargethandle() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   649
  Test_logtargethandle_on();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   650
  Test_logtargethandle_off();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   651
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   652
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   653
static void Test_log_gctracetime_full() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   654
  TestLogFile log_file("log_gctracetime");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   655
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug,gc+start=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   656
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   657
  LogTarget(Debug, gc) gc_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   658
  LogTarget(Debug, gc, start) gc_start_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   659
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   660
  assert(gc_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   661
  assert(gc_start_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   662
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   663
  {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   664
    MutexLocker lock(Heap_lock); // Needed to read heap usage
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   665
    GCTraceTime(Debug, gc) timer("Test GC", NULL, GCCause::_allocation_failure, true);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   666
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   667
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   668
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   669
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   670
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   671
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   672
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   673
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   674
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   675
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   676
  // [2.975s][debug][gc,start] Test GC (Allocation Failure) (2.975s)
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   677
  assert(strstr(output, "[gc,start") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   678
  assert(strstr(output, "] Test GC (Allocation Failure) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   679
  assert(strstr(output, "s)") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   680
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   681
  res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   682
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   683
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   684
  // [2.975s][debug][gc      ] Test GC (Allocation Failure) 59M->59M(502M) (2.975s, 2.975s) 0.026ms
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   685
  assert(strstr(output, "[gc ") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   686
  assert(strstr(output, "] Test GC (Allocation Failure) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   687
  assert(strstr(output, "M) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   688
  assert(strstr(output, "s, ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   689
  assert(strstr(output, "s) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   690
  assert(strstr(output, "ms") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   691
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   692
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   693
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   694
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   695
static void Test_log_gctracetime_full_multitag() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   696
  TestLogFile log_file("log_gctracetime");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   697
  TestLogSavedConfig tlsc(log_file.name(), "gc+ref=debug,gc+ref+start=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   698
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   699
  LogTarget(Debug, gc, ref) gc_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   700
  LogTarget(Debug, gc, ref, start) gc_start_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   701
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   702
  assert(gc_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   703
  assert(gc_start_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   704
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   705
  {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   706
    MutexLocker lock(Heap_lock); // Needed to read heap usage
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   707
    GCTraceTime(Debug, gc, ref) timer("Test GC", NULL, GCCause::_allocation_failure, true);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   708
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   709
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   710
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   711
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   712
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   713
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   714
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   715
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   716
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   717
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   718
  // [2.975s][debug][gc,start] Test GC (Allocation Failure) (2.975s)
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   719
  assert(strstr(output, "[gc,ref,start") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   720
  assert(strstr(output, "] Test GC (Allocation Failure) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   721
  assert(strstr(output, "s)") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   722
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   723
  res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   724
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   725
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   726
  // [2.975s][debug][gc      ] Test GC (Allocation Failure) 59M->59M(502M) (2.975s, 2.975s) 0.026ms
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   727
  assert(strstr(output, "[gc,ref ") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   728
  assert(strstr(output, "] Test GC (Allocation Failure) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   729
  assert(strstr(output, "M) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   730
  assert(strstr(output, "s, ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   731
  assert(strstr(output, "s) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   732
  assert(strstr(output, "ms") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   733
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   734
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   735
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   736
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   737
static void Test_log_gctracetime_no_heap() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   738
  TestLogFile log_file("log_gctracetime");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   739
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug,gc+start=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   740
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   741
  LogTarget(Debug, gc) gc_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   742
  LogTarget(Debug, gc, start) gc_start_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   743
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   744
  assert(gc_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   745
  assert(gc_start_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   746
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   747
  {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   748
    GCTraceTime(Debug, gc) timer("Test GC", NULL, GCCause::_allocation_failure, false);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   749
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   750
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   751
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   752
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   753
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   754
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   755
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   756
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   757
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   758
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   759
  // [2.975s][debug][gc,start] Test GC (Allocation Failure) (2.975s)
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   760
  assert(strstr(output, "[gc,start") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   761
  assert(strstr(output, "] Test GC (Allocation Failure) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   762
  assert(strstr(output, "s)") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   763
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   764
  res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   765
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   766
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   767
  // [2.975s][debug][gc      ] Test GC (Allocation Failure) (2.975s, 2.975s) 0.026ms
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   768
  assert(strstr(output, "[gc ") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   769
  assert(strstr(output, "] Test GC (Allocation Failure) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   770
  assert(strstr(output, "M) (") == NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   771
  assert(strstr(output, "s, ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   772
  assert(strstr(output, "s) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   773
  assert(strstr(output, "ms") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   774
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   775
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   776
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   777
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   778
static void Test_log_gctracetime_no_cause() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   779
  TestLogFile log_file("log_gctracetime");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   780
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug,gc+start=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   781
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   782
  LogTarget(Debug, gc) gc_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   783
  LogTarget(Debug, gc, start) gc_start_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   784
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   785
  assert(gc_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   786
  assert(gc_start_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   787
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   788
  {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   789
    MutexLocker lock(Heap_lock); // Needed to read heap usage
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   790
    GCTraceTime(Debug, gc) timer("Test GC", NULL, GCCause::_no_gc, true);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   791
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   792
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   793
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   794
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   795
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   796
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   797
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   798
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   799
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   800
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   801
  // [2.975s][debug][gc,start] Test GC (2.975s)
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   802
  assert(strstr(output, "[gc,start") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   803
  assert(strstr(output, "] Test GC (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   804
  assert(strstr(output, "s)") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   805
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   806
  res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   807
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   808
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   809
  // [2.975s][debug][gc      ] Test GC 59M->59M(502M) (2.975s, 2.975s) 0.026ms
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   810
  assert(strstr(output, "[gc ") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   811
  assert(strstr(output, "] Test GC ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   812
  assert(strstr(output, "M) (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   813
  assert(strstr(output, "s, ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   814
  assert(strstr(output, "s) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   815
  assert(strstr(output, "ms") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   816
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   817
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   818
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   819
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   820
static void Test_log_gctracetime_no_heap_no_cause() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   821
  TestLogFile log_file("log_gctracetime");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   822
  TestLogSavedConfig tlsc(log_file.name(), "gc=debug,gc+start=debug");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   823
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   824
  LogTarget(Debug, gc) gc_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   825
  LogTarget(Debug, gc, start) gc_start_debug;
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   826
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   827
  assert(gc_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   828
  assert(gc_start_debug.is_enabled(), "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   829
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   830
  {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   831
    MutexLocker lock(Heap_lock); // Needed to read heap usage
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   832
    GCTraceTime(Debug, gc) timer("Test GC", NULL, GCCause::_no_gc, false);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   833
  }
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   834
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   835
  FILE* fp = fopen(log_file.name(), "r");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   836
  assert(fp, "File read error");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   837
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   838
  char output[256 /* Large enough buffer */];
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   839
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   840
  char* res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   841
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   842
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   843
  // [2.975s][debug][gc,start] Test GC (2.975s)
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   844
  assert(strstr(output, "[gc,start") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   845
  assert(strstr(output, "] Test GC (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   846
  assert(strstr(output, "s)") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   847
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   848
  res = fgets(output, sizeof(output), fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   849
  assert(res != NULL, "assert");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   850
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   851
  // [2.975s][debug][gc      ] Test GC (2.975s, 2.975s) 0.026ms
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   852
  assert(strstr(output, "[gc ") != NULL, "Incorrect tag set");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   853
  assert(strstr(output, "] Test GC (") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   854
  assert(strstr(output, "M) (") == NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   855
  assert(strstr(output, "s, ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   856
  assert(strstr(output, "s) ") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   857
  assert(strstr(output, "ms") != NULL, "Incorrect log line");
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   858
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   859
  fclose(fp);
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   860
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   861
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   862
void Test_log_gctracetime() {
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   863
  Test_log_gctracetime_full();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   864
  Test_log_gctracetime_full_multitag();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   865
  Test_log_gctracetime_no_heap();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   866
  Test_log_gctracetime_no_cause();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   867
  Test_log_gctracetime_no_heap_no_cause();
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   868
}
100d4a515df0 8152711: Create a non-template Log wrapper class
stefank
parents: 37254
diff changeset
   869
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   870
void Test_invalid_log_file() {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   871
  ResourceMark rm;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   872
  stringStream ss;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   873
  const char* target_name = "tmplogdir";
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   874
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   875
  // Attempt to log to a directory (existing log not a regular file)
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   876
  create_directory(target_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   877
  LogFileOutput bad_file("tmplogdir");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   878
  assert(bad_file.initialize("", &ss) == false, "file was initialized "
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   879
         "when there was an existing directory with the same name");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   880
  assert(strstr(ss.as_string(), "tmplogdir is not a regular file") != NULL,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   881
         "missing expected error message, received msg: %s", ss.as_string());
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   882
  ss.reset();
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   883
  remove(target_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   884
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37463
diff changeset
   885
33766
3290cae587f9 8138916: Logging write function does not allow for long enough messages
rprotacio
parents:
diff changeset
   886
#endif // PRODUCT