| 1 | /* |
| 2 | * Copyright (C) 2014 - Christian Babeux <christian.babeux@efficios.com> |
| 3 | * |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This library 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 Lesser General Public License |
| 12 | * for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this library; if not, write to the Free Software Foundation, |
| 16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent; |
| 20 | |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | /** |
| 26 | * Basic implementation of LogFramework. |
| 27 | * |
| 28 | * @author Christian Babeux |
| 29 | */ |
| 30 | public abstract class LogFrameworkSkeleton implements LogFramework { |
| 31 | |
| 32 | /* A map of event name and reference count */ |
| 33 | private final Map<String, Integer> enabledLoggers; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | */ |
| 38 | public LogFrameworkSkeleton() { |
| 39 | this.enabledLoggers = new HashMap<String, Integer>(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Boolean enableLogger(String name) { |
| 44 | if (name == null) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if (enabledLoggers.containsKey(name)) { |
| 49 | /* Event is already enabled, simply increment its refcount */ |
| 50 | Integer refcount = enabledLoggers.get(name); |
| 51 | refcount++; |
| 52 | Integer oldval = enabledLoggers.put(name, refcount); |
| 53 | assert (oldval != null); |
| 54 | } else { |
| 55 | /* Event was not enabled, init refcount to 1 */ |
| 56 | Integer oldval = enabledLoggers.put(name, 1); |
| 57 | assert (oldval == null); |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Boolean disableLogger(String name) { |
| 65 | if (name == null) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (!enabledLoggers.containsKey(name)) { |
| 70 | /* Event was never enabled, abort */ |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /* Event was previously enabled, simply decrement its refcount */ |
| 75 | Integer refcount = enabledLoggers.get(name); |
| 76 | refcount--; |
| 77 | assert (refcount >= 0); |
| 78 | |
| 79 | if (refcount == 0) { |
| 80 | /* Event is not used anymore, remove it from the map */ |
| 81 | Integer oldval = enabledLoggers.remove(name); |
| 82 | assert (oldval != null); |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public abstract Iterator<String> listLoggers(); |
| 90 | |
| 91 | @Override |
| 92 | public abstract Boolean isRoot(); |
| 93 | |
| 94 | @Override |
| 95 | public void reset() { |
| 96 | enabledLoggers.clear(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the number of enabled events. |
| 101 | * |
| 102 | * @return The number of enabled events |
| 103 | */ |
| 104 | protected Integer getEventCount() { |
| 105 | return enabledLoggers.size(); |
| 106 | } |
| 107 | } |