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.testlibrary;
  25 
  26 import java.util.Collection;
  27 import java.util.Formatter;
  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 = new ThreadLocal<Map<Integer, String>>() {
  53         @Override
  54         protected Map<Integer, String> initialValue() {
  55             Map<Integer, String> m = new ConcurrentHashMap<>();
  56             allRecords.add(m);
  57             return m;
  58         }
  59 
  60     };
  61 
  62     /**
  63      * Log a message
  64      * @param format Message format
  65      * @param params Message parameters
  66      */
  67     public void log(String format, Object ... params) {
  68         int id = logCntr.getAndIncrement();
  69         try (Formatter formatter = new Formatter()) {
  70             records.get().put(id, formatter.format(format, params).toString());
  71         }
  72     }
  73 
  74     /**
  75      * Will generate an aggregated log of chronologically ordered messages.
  76      * <p>
  77      * Make sure that you call this method only when all the related threads
  78      * have finished; otherwise you might get incomplete data.
  79      *
  80      * @return An aggregated log of chronologically ordered messages
  81      */
  82     @Override
  83     public String toString() {
  84         return allRecords.stream()
  85             .flatMap(m->m.entrySet().stream())
  86             .sorted((l, r)->l.getKey().compareTo(r.getKey()))
  87             .map(e->e.getValue())
  88             .collect(Collectors.joining());
  89     }
  90 }