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/logFileStreamOutput.hpp"
  26 #include "logging/logOutput.hpp"
  27 #include "logging/logTagSet.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "runtime/os.inline.hpp"
  31 
  32 LogOutput* const LogOutput::Stdout = &LogStdoutOutput::_instance;
  33 LogOutput* const LogOutput::Stderr = &LogStderrOutput::_instance;
  34 
  35 LogOutput::~LogOutput() {
  36   os::free(_config_string);
  37 }
  38 
  39 void LogOutput::clear_config_string() {
  40   os::free(_config_string);
  41   _config_string_buffer_size = InitialConfigBufferSize;
  42   _config_string = NEW_C_HEAP_ARRAY(char, _config_string_buffer_size, mtLogging);
  43   _config_string[0] = '\0';
  44 }
  45 
  46 void LogOutput::set_config_string(const char* string) {
  47   os::free(_config_string);
  48   _config_string = os::strdup(string, mtLogging);
  49   _config_string_buffer_size = strlen(_config_string) + 1;
  50 }
  51 
  52 void LogOutput::add_to_config_string(const LogTagSet* ts, LogLevelType level) {
  53   if (_config_string_buffer_size < InitialConfigBufferSize) {
  54     _config_string_buffer_size = InitialConfigBufferSize;
  55     _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
  56   }
  57 
  58   size_t offset = strlen(_config_string);
  59   for (;;) {
  60     int ret = ts->label(_config_string + offset, _config_string_buffer_size - offset, "+");
  61     if (ret == -1) {
  62       // Double the buffer size and retry
  63       _config_string_buffer_size *= 2;
  64       _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
  65       continue;
  66     }
  67     break;
  68   };
  69 
  70   offset = strlen(_config_string);
  71   for (;;) {
  72     int ret = jio_snprintf(_config_string + offset, _config_string_buffer_size - offset, "=%s,", LogLevel::name(level));
  73     if (ret == -1) {
  74       _config_string_buffer_size *= 2;
  75       _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
  76       continue;
  77     }
  78     break;
  79   }
  80 }