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 import java.util.Arrays; 25 import java.util.Objects; 26 import java.util.Queue; 27 import java.util.ResourceBundle; 28 import java.util.concurrent.ArrayBlockingQueue; 29 import java.util.concurrent.ConcurrentHashMap; 30 import java.util.concurrent.atomic.AtomicLong; 31 import java.util.function.Supplier; 32 import java.lang.System.Logger; 33 import java.lang.reflect.Module; 34 35 /** 36 * What our test provider needs to implement. 37 * @author danielfuchs 38 */ 39 public interface TestLoggerFinder { 40 public final static AtomicLong sequencer = new AtomicLong(); 41 public final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>(); 42 public final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>(); 43 public final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128); 44 45 public static final class LogEvent { 46 47 public LogEvent() { 48 this(sequencer.getAndIncrement()); 49 } 50 51 LogEvent(long sequenceNumber) { 52 this.sequenceNumber = sequenceNumber; 53 } 54 55 long sequenceNumber; 56 boolean isLoggable; 57 String loggerName; 58 Logger.Level level; 59 ResourceBundle bundle; 60 Throwable thrown; 61 Object[] args; 62 Supplier<String> supplier; 63 String msg; 64 65 Object[] toArray() { 66 return new Object[] { 67 sequenceNumber, 68 isLoggable, 69 loggerName, 70 level, 71 bundle, 72 thrown, 73 args, 74 supplier, 75 msg, 76 }; 77 } 78 79 @Override 80 public String toString() { 81 return Arrays.deepToString(toArray()); 82 } 83 84 85 86 @Override 87 public boolean equals(Object obj) { 88 return obj instanceof LogEvent 89 && Objects.deepEquals(this.toArray(), ((LogEvent)obj).toArray()); 90 } 91 92 @Override 93 public int hashCode() { 94 return Objects.hash(toArray()); 95 } 96 97 98 public static LogEvent of(boolean isLoggable, String name, 99 Logger.Level level, ResourceBundle bundle, 100 String key, Throwable thrown) { 101 LogEvent evt = new LogEvent(); 102 evt.isLoggable = isLoggable; 103 evt.loggerName = name; 104 evt.level = level; 105 evt.args = null; 106 evt.bundle = bundle; 107 evt.thrown = thrown; 108 evt.supplier = null; 109 evt.msg = key; 110 return evt; 111 } 112 113 public static LogEvent of(boolean isLoggable, String name, 114 Logger.Level level, ResourceBundle bundle, 115 String key, Object... params) { 116 LogEvent evt = new LogEvent(); 117 evt.isLoggable = isLoggable; 118 evt.loggerName = name; 119 evt.level = level; 120 evt.args = params; 121 evt.bundle = bundle; 122 evt.thrown = null; 123 evt.supplier = null; 124 evt.msg = key; 125 return evt; 126 } 127 128 public static LogEvent of(long sequenceNumber, 129 boolean isLoggable, String name, 130 Logger.Level level, ResourceBundle bundle, 131 String key, Supplier<String> supplier, 132 Throwable thrown, Object... params) { 133 LogEvent evt = new LogEvent(sequenceNumber); 134 evt.loggerName = name; 135 evt.level = level; 136 evt.args = params; 137 evt.bundle = bundle; 138 evt.thrown = thrown; 139 evt.supplier = supplier; 140 evt.msg = key; 141 evt.isLoggable = isLoggable; 142 return evt; 143 } 144 145 } 146 147 public class LoggerImpl implements Logger { 148 final String name; 149 Logger.Level level = Logger.Level.INFO; 150 151 public LoggerImpl(String name) { 152 this.name = name; 153 } 154 155 @Override 156 public String getName() { 157 return name; 158 } 159 160 @Override 161 public boolean isLoggable(Logger.Level level) { 162 return this.level != Logger.Level.OFF && this.level.getSeverity() <= level.getSeverity(); 163 } 164 165 @Override 166 public void log(Logger.Level level, ResourceBundle bundle, String key, Throwable thrown) { 167 log(LogEvent.of(isLoggable(level), this.name, level, bundle, key, thrown)); 168 } 169 170 @Override 171 public void log(Logger.Level level, ResourceBundle bundle, String format, Object... params) { 172 log(LogEvent.of(isLoggable(level), name, level, bundle, format, params)); 173 } 174 175 void log(LogEvent event) { 176 eventQueue.add(event); 177 } 178 } 179 180 public Logger getLogger(String name, Module caller); 181 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller); 182 }