logDiagnosticCommand.cpp revision 9655:37a97bb8b1ca
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
31LogDiagnosticCommand::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    _rotate("rotate", "Rotates all logs.", "BOOLEAN", false) {
40  _dcmdparser.add_dcmd_option(&_output);
41  _dcmdparser.add_dcmd_option(&_output_options);
42  _dcmdparser.add_dcmd_option(&_what);
43  _dcmdparser.add_dcmd_option(&_decorators);
44  _dcmdparser.add_dcmd_option(&_disable);
45  _dcmdparser.add_dcmd_option(&_list);
46  _dcmdparser.add_dcmd_option(&_rotate);
47}
48
49int LogDiagnosticCommand::num_arguments() {
50  ResourceMark rm;
51  LogDiagnosticCommand* dcmd = new LogDiagnosticCommand(NULL, false);
52  if (dcmd != NULL) {
53    DCmdMark mark(dcmd);
54    return dcmd->_dcmdparser.num_arguments();
55  } else {
56    return 0;
57  }
58}
59
60void LogDiagnosticCommand::registerCommand() {
61  uint32_t full_visibility = DCmd_Source_Internal | DCmd_Source_AttachAPI | DCmd_Source_MBean;
62  DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<LogDiagnosticCommand>(full_visibility, true, false));
63}
64
65void LogDiagnosticCommand::execute(DCmdSource source, TRAPS) {
66  bool any_command = false;
67  if (_disable.has_value()) {
68    MutexLocker ml(LogConfiguration_lock);
69    LogConfiguration::disable_logging();
70    any_command = true;
71  }
72
73  if (_output.has_value() || _what.has_value() || _decorators.has_value()) {
74    MutexLocker ml(LogConfiguration_lock);
75    if (!LogConfiguration::parse_log_arguments(_output.value(),
76                                               _what.value(),
77                                               _decorators.value(),
78                                               _output_options.value(),
79                                               output())) {
80      return;
81    }
82    any_command = true;
83  }
84
85  if (_list.has_value()) {
86    MutexLocker ml(LogConfiguration_lock);
87    LogConfiguration::describe(output());
88    any_command = true;
89  }
90
91  if (_rotate.has_value()) {
92    LogConfiguration::rotate_all_outputs();
93    any_command = true;
94  }
95
96  if (!any_command) {
97    // If no argument was provided, print usage
98    print_help(LogDiagnosticCommand::name());
99  }
100
101}
102