1 /*
   2  * Copyright (c) 2015, 2016, 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 #ifndef SHARE_VM_LOGGING_LOGDECORATORS_HPP
  25 #define SHARE_VM_LOGGING_LOGDECORATORS_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 // The list of available decorators:
  31 // time         - Current time and date in ISO-8601 format
  32 // uptime       - Time since the start of the JVM in seconds and milliseconds (e.g., 6.567s)
  33 // timemillis   - The same value as generated by System.currentTimeMillis()
  34 // uptimemillis - Milliseconds since the JVM started
  35 // timenanos    - The same value as generated by System.nanoTime()
  36 // uptimenanos  - Nanoseconds since the JVM started
  37 // hostname     - The hostname
  38 // pid          - The process identifier
  39 // tid          - The thread identifier
  40 // level        - The level associated with the log message
  41 // tags         - The tag-set associated with the log message
  42 #define DECORATOR_LIST          \
  43   DECORATOR(time,         t)    \
  44   DECORATOR(utctime,      utc)  \
  45   DECORATOR(uptime,       u)    \
  46   DECORATOR(timemillis,   tm)   \
  47   DECORATOR(uptimemillis, um)   \
  48   DECORATOR(timenanos,    tn)   \
  49   DECORATOR(uptimenanos,  un)   \
  50   DECORATOR(hostname,     hn)   \
  51   DECORATOR(pid,          p)    \
  52   DECORATOR(tid,          ti)   \
  53   DECORATOR(level,        l)    \
  54   DECORATOR(tags,         tg)
  55 
  56 // LogDecorators represents a selection of decorators that should be prepended to
  57 // each log message for a given output. Decorators are always prepended in the order
  58 // declared above. For example, logging with 'uptime, level, tags' decorators results in:
  59 // [0,943s][info   ][logging] message.
  60 class LogDecorators VALUE_OBJ_CLASS_SPEC {
  61  public:
  62   enum Decorator {
  63 #define DECORATOR(name, abbr) name##_decorator,
  64     DECORATOR_LIST
  65 #undef DECORATOR
  66     Count,
  67     Invalid
  68   };
  69 
  70  private:
  71   uint _decorators;
  72   static const char* _name[][2];
  73   static const uint DefaultDecoratorsMask = (1 << uptime_decorator) | (1 << level_decorator) | (1 << tags_decorator);
  74 
  75   static uint mask(LogDecorators::Decorator decorator) {
  76     return 1 << decorator;
  77   }
  78 
  79   LogDecorators(uint mask) : _decorators(mask) {
  80   }
  81 
  82  public:
  83   static const LogDecorators None;
  84 
  85   LogDecorators() : _decorators(DefaultDecoratorsMask) {
  86   };
  87 
  88   void clear() {
  89     _decorators = 0;
  90   }
  91 
  92   static const char* name(LogDecorators::Decorator decorator) {
  93     return _name[decorator][0];
  94   }
  95 
  96   static const char* abbreviation(LogDecorators::Decorator decorator) {
  97     return _name[decorator][1];
  98   }
  99 
 100   static LogDecorators::Decorator from_string(const char* str);
 101 
 102   void combine_with(const LogDecorators &source) {
 103     _decorators |= source._decorators;
 104   }
 105 
 106   bool is_empty() const {
 107     return _decorators == 0;
 108   }
 109 
 110   bool is_decorator(LogDecorators::Decorator decorator) const {
 111     return (_decorators & mask(decorator)) != 0;
 112   }
 113 
 114   bool parse(const char* decorator_args, outputStream* errstream = NULL);
 115 };
 116 
 117 #endif // SHARE_VM_LOGGING_LOGDECORATORS_HPP