| 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.Map; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Iterator; |
| 24 | |
| 25 | public abstract class LogFrameworkSkeleton implements LogFramework { |
| 26 | |
| 27 | /* A map of event name and reference count */ |
| 28 | private Map<String, Integer> enabledLoggers; |
| 29 | |
| 30 | public LogFrameworkSkeleton() { |
| 31 | this.enabledLoggers = new HashMap<String, Integer>(); |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public Boolean enableLogger(String name) { |
| 36 | if (name == null) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if (enabledLoggers.containsKey(name)) { |
| 41 | /* Event is already enabled, simply increment its refcount */ |
| 42 | Integer refcount = enabledLoggers.get(name); |
| 43 | refcount++; |
| 44 | Integer oldval = enabledLoggers.put(name, refcount); |
| 45 | assert (oldval != null); |
| 46 | } else { |
| 47 | /* Event was not enabled, init refcount to 1 */ |
| 48 | Integer oldval = enabledLoggers.put(name, 1); |
| 49 | assert (oldval == null); |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Boolean disableLogger(String name) { |
| 57 | if (name == null) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | if (!enabledLoggers.containsKey(name)) { |
| 62 | /* Event was never enabled, abort */ |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /* Event was previously enabled, simply decrement its refcount */ |
| 67 | Integer refcount = enabledLoggers.get(name); |
| 68 | refcount--; |
| 69 | assert (refcount >= 0); |
| 70 | |
| 71 | if (refcount == 0) { |
| 72 | /* Event is not used anymore, remove it from the map */ |
| 73 | Integer oldval = enabledLoggers.remove(name); |
| 74 | assert (oldval != null); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public abstract Iterator<String> listLoggers(); |
| 82 | |
| 83 | @Override |
| 84 | public abstract Boolean isRoot(); |
| 85 | |
| 86 | @Override |
| 87 | public void reset() { |
| 88 | enabledLoggers.clear(); |
| 89 | } |
| 90 | |
| 91 | protected Integer getEventCount() { |
| 92 | return enabledLoggers.size(); |
| 93 | } |
| 94 | } |