Path: blob/master/src/hotspot/share/logging/logDecorators.cpp
64440 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "logging/logDecorators.hpp"25#include "runtime/os.hpp"2627template <LogDecorators::Decorator d>28struct AllBitmask {29// Use recursive template deduction to calculate the bitmask of all decorations.30static const uint _value = (1 << d) | AllBitmask<static_cast<LogDecorators::Decorator>(d + 1)>::_value;31};3233template<>34struct AllBitmask<LogDecorators::Count> {35static const uint _value = 0;36};3738const LogDecorators LogDecorators::None = LogDecorators(0);39const LogDecorators LogDecorators::All = LogDecorators(AllBitmask<time_decorator>::_value);4041const char* LogDecorators::_name[][2] = {42#define DECORATOR(n, a) {#n, #a},43DECORATOR_LIST44#undef DECORATOR45};4647LogDecorators::Decorator LogDecorators::from_string(const char* str) {48for (size_t i = 0; i < Count; i++) {49Decorator d = static_cast<Decorator>(i);50if (strcasecmp(str, name(d)) == 0 || strcasecmp(str, abbreviation(d)) == 0) {51return d;52}53}54return Invalid;55}5657bool LogDecorators::parse(const char* decorator_args, outputStream* errstream) {58if (decorator_args == NULL || strlen(decorator_args) == 0) {59_decorators = DefaultDecoratorsMask;60return true;61}6263if (strcasecmp(decorator_args, "none") == 0 ) {64_decorators = 0;65return true;66}6768bool result = true;69uint tmp_decorators = 0;70char* args_copy = os::strdup_check_oom(decorator_args, mtLogging);71char* token = args_copy;72char* comma_pos;73do {74comma_pos = strchr(token, ',');75if (comma_pos != NULL) {76*comma_pos = '\0';77}78Decorator d = from_string(token);79if (d == Invalid) {80if (errstream != NULL) {81errstream->print_cr("Invalid decorator '%s'.", token);82}83result = false;84break;85}86tmp_decorators |= mask(d);87token = comma_pos + 1;88} while (comma_pos != NULL);89os::free(args_copy);90if (result) {91_decorators = tmp_decorators;92}93return result;94}959697