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/logDecorations.hpp"
  26 #include "logging/logLevel.hpp"
  27 #include "logging/logOutput.hpp"
  28 #include "logging/logTag.hpp"
  29 #include "logging/logTagSet.hpp"
  30 
  31 LogTagSet*  LogTagSet::_list      = NULL;
  32 size_t      LogTagSet::_ntagsets  = 0;
  33 
  34 LogTagSet::LogTagSet(LogTagType t0, LogTagType t1, LogTagType t2, LogTagType t3, LogTagType t4)
  35     : _next(_list) {
  36   _tag[0] = t0;
  37   _tag[1] = t1;
  38   _tag[2] = t2;
  39   _tag[3] = t3;
  40   _tag[4] = t4;
  41   for (_ntags = 0; _ntags < LogTag::MaxTags && _tag[_ntags] != LogTag::__NO_TAG; _ntags++) {
  42   }
  43   _list = this;
  44   _ntagsets++;
  45 
  46   // Set the default output to warning and error level for all new tagsets.
  47   _output_list.set_output_level(LogOutput::Stderr, LogLevel::Default);
  48 }
  49 
  50 bool LogTagSet::is_level(LogLevelType level) {
  51   return _output_list.is_level(level);
  52 }
  53 
  54 void LogTagSet::update_decorators(const LogDecorators& decorator) {
  55   LogDecorators new_decorators = decorator;
  56   for (LogOutputList::Iterator it = _output_list.iterator(); it != _output_list.end(); it++) {
  57     new_decorators.combine_with((*it)->decorators());
  58   }
  59   _decorators = new_decorators;
  60 }
  61 
  62 bool LogTagSet::has_output(const LogOutput* output) {
  63   for (LogOutputList::Iterator it = _output_list.iterator(); it != _output_list.end(); it++) {
  64     if (*it == output) {
  65       return true;
  66     }
  67   }
  68   return false;
  69 }
  70 
  71 void LogTagSet::log(LogLevelType level, const char* msg) {
  72   LogDecorations decorations(level, *this, _decorators);
  73   for (LogOutputList::Iterator it = _output_list.iterator(level); it != _output_list.end(); it++) {
  74     (*it)->write(decorations, msg);
  75   }
  76 }
  77 
  78 int LogTagSet::label(char* buf, size_t len)  {
  79   int tot_written = 0;
  80   for (size_t i = 0; i < _ntags; i++) {
  81     int written = jio_snprintf(buf + tot_written, len - tot_written, "%s%s",
  82                                (i == 0 ? "" : ","),
  83                                LogTag::name(_tag[i]));
  84     if (written < 0) {
  85       return -1;
  86     }
  87     tot_written += written;
  88   }
  89   return tot_written;
  90 }