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 #include "precompiled.hpp"
  25 #include "logging/logConfiguration.hpp"
  26 #include "logging/logDiagnosticCommand.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "runtime/mutexLocker.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 LogDiagnosticCommand::LogDiagnosticCommand(outputStream* output, bool heap_allocated)
  32   : DCmdWithParser(output, heap_allocated),
  33     _output("output", "The name or index (#<index>) of output to configure.", "STRING", false),
  34     _output_options("output_options", "Options for the output.", "STRING", false),
  35     _what("what", "Configures what tags to log.", "STRING", false),
  36     _decorators("decorators", "Configures which decorators to use. Use 'none' or an empty value to remove all.", "STRING", false),
  37     _disable("disable", "Turns off all logging and clears the log configuration.", "BOOLEAN", false),
  38     _list("list", "Lists current log configuration.", "BOOLEAN", false) {
  39   _dcmdparser.add_dcmd_option(&_output);
  40   _dcmdparser.add_dcmd_option(&_output_options);
  41   _dcmdparser.add_dcmd_option(&_what);
  42   _dcmdparser.add_dcmd_option(&_decorators);
  43   _dcmdparser.add_dcmd_option(&_disable);
  44   _dcmdparser.add_dcmd_option(&_list);
  45 }
  46 
  47 int LogDiagnosticCommand::num_arguments() {
  48   ResourceMark rm;
  49   LogDiagnosticCommand* dcmd = new LogDiagnosticCommand(NULL, false);
  50   if (dcmd != NULL) {
  51     DCmdMark mark(dcmd);
  52     return dcmd->_dcmdparser.num_arguments();
  53   } else {
  54     return 0;
  55   }
  56 }
  57 
  58 void LogDiagnosticCommand::registerCommand() {
  59   uint32_t full_visibility = DCmd_Source_Internal | DCmd_Source_AttachAPI | DCmd_Source_MBean;
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<LogDiagnosticCommand>(full_visibility, true, false));
  61 }
  62 
  63 void LogDiagnosticCommand::execute(DCmdSource source, TRAPS) {
  64   bool any_command = false;
  65   if (_disable.has_value()) {
  66     MutexLocker ml(LogConfiguration_lock);
  67     LogConfiguration::disable_logging();
  68     any_command = true;
  69   }
  70 
  71   if (_output.has_value() || _what.has_value() || _decorators.has_value()) {
  72     MutexLocker ml(LogConfiguration_lock);
  73     if (!LogConfiguration::parse_log_arguments(_output.value(),
  74                                                _what.value(),
  75                                                _decorators.value(),
  76                                                _output_options.value(),
  77                                                output())) {
  78       return;
  79     }
  80     any_command = true;
  81   }
  82 
  83   if (_list.has_value()) {
  84     MutexLocker ml(LogConfiguration_lock);
  85     LogConfiguration::describe(output());
  86     any_command = true;
  87   }
  88 
  89   if (!any_command) {
  90     // If no argument was provided, print usage
  91     print_help(LogDiagnosticCommand::name());
  92   }
  93 
  94 }