1 /*
   2  * Copyright (c) 2014, 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 package jdk.test.lib.log;
  25 
  26 import java.util.Collection;
  27 import java.util.Comparator;
  28 import java.util.Map;
  29 import java.util.concurrent.ConcurrentHashMap;
  30 import java.util.concurrent.ConcurrentLinkedQueue;
  31 import java.util.concurrent.atomic.AtomicInteger;
  32 import java.util.stream.Collectors;
  33 
  34 /**
  35  * A log manager designed specifically to allow collecting ordered log messages
  36  * in a multi-threaded environment without involving any kind of locking.
  37  * <p>
  38  * It is particularly useful in situations when one needs to assert various
  39  * details about the tested thread state or the locks it hold while also wanting
  40  * to produce diagnostic log messages.
  41  * <p>
  42  * The log manager does not provide any guarantees about the completness of the
  43  * logs written from different threads - it is up to the caller to make sure
  44  * {@code toString()} method is called only when all the activity has ceased
  45  * and the per-thread logs contain all the necessary data.
  46  *
  47  * @author Jaroslav Bachorik
  48  **/
  49 public class LockFreeLogManager {
  50     private final AtomicInteger logCntr = new AtomicInteger(0);
  51     private final Collection<Map<Integer, String>> allRecords = new ConcurrentLinkedQueue<>();
  52     private final ThreadLocal<Map<Integer, String>> records = ThreadLocal.withInitial(ConcurrentHashMap::new);
  53 
  54     public LockFreeLogManager() {
  55         allRecords.add(records.get());
  56     }
  57 
  58     /**
  59      * Log a message
  60      * @param format Message format
  61      * @param params Message parameters
  62      */
  63     public void log(String format, Object ... params) {
  64         int id = logCntr.getAndIncrement();
  65         records.get().put(id, String.format(format, params));
  66     }
  67 
  68     /**
  69      * Will generate an aggregated log of chronologically ordered messages.
  70      * <p>
  71      * Make sure that you call this method only when all the related threads
  72      * have finished; otherwise you might get incomplete data.
  73      *
  74      * @return An aggregated log of chronologically ordered messages
  75      */
  76     @Override
  77     public String toString() {
  78         return allRecords.stream()
  79             .flatMap(m -> m.entrySet().stream())
  80             .sorted(Comparator.comparing(Map.Entry::getKey))
  81             .map(Map.Entry::getValue)
  82             .collect(Collectors.joining());
  83     }
  84 }