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/logDecorators.hpp"
  26 #include "runtime/os.inline.hpp"
  27 
  28 const char* LogDecorators::_name[][2] = {
  29 #define DECORATOR(n, a) {#n, #a},
  30   DECORATOR_LIST
  31 #undef DECORATOR
  32 };
  33 
  34 LogDecorators::Decorator LogDecorators::from_string(const char* str) {
  35   for (size_t i = 0; i < Count; i++) {
  36     Decorator d = static_cast<Decorator>(i);
  37     if (strcasecmp(str, name(d)) == 0 || strcasecmp(str, abbreviation(d)) == 0) {
  38       return d;
  39     }
  40   }
  41   return Invalid;
  42 }
  43 
  44 bool LogDecorators::parse(const char* decorator_args, outputStream* errstream) {
  45   if (decorator_args == NULL || strlen(decorator_args) == 0) {
  46     _decorators = DefaultDecoratorsMask;
  47     return true;
  48   }
  49 
  50   if (strcasecmp(decorator_args, "none") == 0 ) {
  51     _decorators = 0;
  52     return true;
  53   }
  54 
  55   bool result = true;
  56   uint tmp_decorators = 0;
  57   char* args_copy = os::strdup_check_oom(decorator_args);
  58   char* token = args_copy;
  59   char* comma_pos;
  60   do {
  61     comma_pos = strchr(token, ',');
  62     if (comma_pos != NULL) {
  63       *comma_pos = '\0';
  64     }
  65     Decorator d = from_string(token);
  66     if (d == Invalid) {
  67       if (errstream != NULL) {
  68         errstream->print_cr("Invalid decorator '%s'.", token);
  69       }
  70       result = false;
  71       break;
  72     }
  73     tmp_decorators |= mask(d);
  74     token = comma_pos + 1;
  75   } while (comma_pos != NULL);
  76   os::free(args_copy);
  77   if (result) {
  78     _decorators = tmp_decorators;
  79   }
  80   return result;
  81 }