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 size_t buffer_offset = (offset == 0) ? 0 : (offset + 1); 61 int ret = ts->label(_config_string + buffer_offset, _config_string_buffer_size - buffer_offset, "+"); 62 if (ret == -1) { 63 // Double the buffer size and retry 64 _config_string_buffer_size *= 2; 65 _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging); 66 continue; 67 } 68 if (offset > 0) { 69 _config_string[offset] = ','; 70 } 71 break; 72 }; 73 74 offset = strlen(_config_string); 75 for (;;) { 76 int ret = jio_snprintf(_config_string + offset, _config_string_buffer_size - offset, "=%s", LogLevel::name(level)); 77 if (ret == -1) { 78 _config_string_buffer_size *= 2; 79 _config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging); 80 continue; 81 } 82 break; 83 } 84 }