1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 /////////////// Unit tests ///////////////
  28 
  29 #ifndef PRODUCT
  30 
  31 #include "logging/log.hpp"
  32 #include "logging/logConfiguration.hpp"
  33 #include "logging/logOutput.hpp"
  34 #include "memory/resourceArea.hpp"
  35 
  36 void Test_log_length() {
  37   remove("loglengthoutput.txt");
  38 
  39   // Write long message to output file
  40   ResourceMark rm;
  41   LogHandle(logging) log;
  42   bool success = LogConfiguration::parse_log_arguments("loglengthoutput.txt", "logging=trace",
  43     NULL, NULL, log.error_stream());
  44   assert(success, "test unable to configure logging");
  45   log.trace("01:1234567890-"
  46             "02:1234567890-"
  47             "03:1234567890-"
  48             "04:1234567890-"
  49             "05:1234567890-"
  50             "06:1234567890-"
  51             "07:1234567890-"
  52             "08:1234567890-"
  53             "09:1234567890-"
  54             "10:1234567890-"
  55             "11:1234567890-"
  56             "12:1234567890-"
  57             "13:1234567890-"
  58             "14:1234567890-"
  59             "15:1234567890-"
  60             "16:1234567890-"
  61             "17:1234567890-"
  62             "18:1234567890-"
  63             "19:1234567890-"
  64             "20:1234567890-"
  65             "21:1234567890-"
  66             "22:1234567890-"
  67             "23:1234567890-"
  68             "24:1234567890-"
  69             "25:1234567890-"
  70             "26:1234567890-"
  71             "27:1234567890-"
  72             "28:1234567890-"
  73             "29:1234567890-"
  74             "30:1234567890-"
  75             "31:1234567890-"
  76             "32:1234567890-"
  77             "33:1234567890-"
  78             "34:1234567890-"
  79             "35:1234567890-"
  80             "36:1234567890-"
  81             "37:1234567890-");
  82   LogConfiguration::parse_log_arguments("loglengthoutput.txt", "all=off",
  83     NULL, NULL, log.error_stream());
  84 
  85   // Look for end of message in output file
  86   FILE* fp = fopen("loglengthoutput.txt", "r");
  87   assert(fp, "File read error");
  88   char output[600];
  89   if (fgets(output, 600, fp) != NULL) {
  90     assert(strstr(output, "37:1234567890-"), "logging print size error");
  91   }
  92   fclose(fp);
  93   remove("loglengthoutput.txt");
  94 }
  95 
  96 #define assert_str_eq(s1, s2) \
  97   assert(strcmp(s1, s2) == 0, "Expected '%s' to equal '%s'", s1, s2)
  98 
  99 #define assert_char_in(c, s) \
 100   assert(strchr(s, c) != NULL, "Expected '%s' to contain character '%c'", s, c)
 101 
 102 #define assert_char_not_in(c, s) \
 103   assert(strchr(s, c) == NULL, "Expected '%s' to *not* contain character '%c'", s, c)
 104 
 105 void Test_configure_stdout() {
 106   ResourceMark rm;
 107   LogHandle(logging) log;
 108   LogOutput* stdoutput = LogOutput::Stdout;
 109 
 110   // Save current stdout config and clear it
 111   char* saved_config = os::strdup_check_oom(stdoutput->config_string());
 112   LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, log.error_stream());
 113 
 114   // Enable 'logging=info', verifying it has been set
 115   LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(logging));
 116   assert_str_eq("logging=info,", stdoutput->config_string());
 117   assert(log_is_enabled(Info, logging), "logging was not properly enabled");
 118 
 119   // Enable 'gc=debug' (no wildcard), verifying no other tags are enabled
 120   LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
 121   // No '+' character means only single tags are enabled, and no combinations
 122   assert_char_not_in('+', stdoutput->config_string());
 123   assert(log_is_enabled(Debug, gc), "logging was not properly enabled");
 124 
 125   // Enable 'gc*=trace' (with wildcard), verifying at least one tag combination is enabled (gc+...)
 126   LogConfiguration::configure_stdout(LogLevel::Trace, false, LOG_TAGS(gc));
 127   assert_char_in('+', stdoutput->config_string());
 128   assert(log_is_enabled(Trace, gc), "logging was not properly enabled");
 129 
 130   // Disable 'gc*' and 'logging', verifying all logging is properly disabled
 131   LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
 132   LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
 133   assert_str_eq("all=off", stdoutput->config_string());
 134 
 135   // Restore saved configuration
 136   LogConfiguration::parse_log_arguments("stdout", saved_config, NULL, NULL, log.error_stream());
 137   os::free(saved_config);
 138 }
 139 #endif // PRODUCT