001/** 002 * Copyright (c) 2004-2016 QOS.ch 003 * All rights reserved. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining 006 * a copy of this software and associated documentation files (the 007 * "Software"), to deal in the Software without restriction, including 008 * without limitation the rights to use, copy, modify, merge, publish, 009 * distribute, sublicense, and/or sell copies of the Software, and to 010 * permit persons to whom the Software is furnished to do so, subject to 011 * the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be 014 * included in all copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 023 * 024 */ 025package org.slf4j.helpers; 026 027import java.io.PrintStream; 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.List; 031 032import org.junit.After; 033import org.junit.Before; 034import org.slf4j.LoggerFactoryFriend; 035import org.slf4j.impl.SimpleLogger; 036 037public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedInitializationTest { 038 // final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2; 039 // private final List<Logger> createdLoggers = Collections.synchronizedList(new ArrayList<Logger>()); 040 // private final AtomicLong eventCount = new AtomicLong(0); 041 // 042 // private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1); 043 // 044 // final int diff = new Random().nextInt(10000); 045 static int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3; 046 private final PrintStream oldErr = System.err; 047 final String loggerName = this.getClass().getName(); 048 StringPrintStream sps = new StringPrintStream(oldErr, true); 049 050 @Before 051 public void setup() { 052 System.out.println("THREAD_COUNT=" + THREAD_COUNT); 053 System.setErr(sps); 054 System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.err"); 055 LoggerFactoryFriend.reset(); 056 } 057 058 @After 059 public void tearDown() throws Exception { 060 LoggerFactoryFriend.reset(); 061 System.clearProperty(SimpleLogger.LOG_FILE_KEY); 062 System.setErr(oldErr); 063 } 064 065 @Override 066 protected long getRecordedEventCount() { 067 return sps.stringList.size(); 068 }; 069 070 @Override 071 protected int extraLogEvents() { 072 return NUM_LINES_IN_SLF4J_REPLAY_WARNING; 073 } 074 075 static class StringPrintStream extends PrintStream { 076 077 public static final String LINE_SEP = System.getProperty("line.separator"); 078 PrintStream other; 079 boolean duplicate = false; 080 081 List<String> stringList = Collections.synchronizedList(new ArrayList<String>()); 082 083 public StringPrintStream(PrintStream ps, boolean duplicate) { 084 super(ps); 085 other = ps; 086 this.duplicate = duplicate; 087 } 088 089 public StringPrintStream(PrintStream ps) { 090 this(ps, false); 091 } 092 093 public void print(String s) { 094 if (duplicate) 095 other.print(s); 096 stringList.add(s); 097 } 098 099 public void println(String s) { 100 if (duplicate) 101 other.println(s); 102 stringList.add(s); 103 } 104 105 public void println(Object o) { 106 if (duplicate) 107 other.println(o); 108 stringList.add(o.toString()); 109 } 110 } 111 112}