| 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 | /* |
| 31 | * If the following attribute is false, the internal ref count is |
| 32 | * never decremented when disabling a logger. This was the original |
| 33 | * behaviour of this agent, and this bug worked in concert with a |
| 34 | * bug in the session daemon which would send multiple disable |
| 35 | * commands for the same event name (manual disable + another |
| 36 | * disable on session destroy). The following attribute is needed |
| 37 | * because this version of the agent could be connected to a |
| 38 | * fixed session daemon, or a non-fixed session daemon, and it needs |
| 39 | * to work in both situations. |
| 40 | */ |
| 41 | private boolean enableRefCountDecrement = false; |
| 42 | |
| 43 | public LogFrameworkSkeleton() { |
| 44 | this.enabledLoggers = new HashMap<String, Integer>(); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Boolean enableLogger(String name) { |
| 49 | if (name == null) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if (enabledLoggers.containsKey(name)) { |
| 54 | /* Event is already enabled, simply increment its refcount */ |
| 55 | Integer refcount = enabledLoggers.get(name); |
| 56 | refcount++; |
| 57 | Integer oldval = enabledLoggers.put(name, refcount); |
| 58 | assert (oldval != null); |
| 59 | } else { |
| 60 | /* Event was not enabled, init refcount to 1 */ |
| 61 | Integer oldval = enabledLoggers.put(name, 1); |
| 62 | assert (oldval == null); |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Boolean disableLogger(String name) { |
| 70 | if (name == null) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (!enabledLoggers.containsKey(name)) { |
| 75 | /* Event was never enabled, abort */ |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | /* Event was previously enabled, simply decrement its refcount */ |
| 80 | Integer refcount = enabledLoggers.get(name); |
| 81 | refcount--; |
| 82 | assert (refcount >= 0); |
| 83 | |
| 84 | if (enableRefCountDecrement) { |
| 85 | /* Effectively decrement reference count. */ |
| 86 | enabledLoggers.put(name, refcount); |
| 87 | } |
| 88 | |
| 89 | if (refcount == 0) { |
| 90 | /* Event is not used anymore, remove it from the map */ |
| 91 | Integer oldval = enabledLoggers.remove(name); |
| 92 | assert (oldval != null); |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public abstract Iterator<String> listLoggers(); |
| 100 | |
| 101 | @Override |
| 102 | public abstract Boolean isRoot(); |
| 103 | |
| 104 | @Override |
| 105 | public void reset() { |
| 106 | enabledLoggers.clear(); |
| 107 | } |
| 108 | |
| 109 | protected Integer getEventCount() { |
| 110 | return enabledLoggers.size(); |
| 111 | } |
| 112 | |
| 113 | public void setEnableRefCountDecrement(boolean enableRefCountDecrement) { |
| 114 | this.enableRefCountDecrement = enableRefCountDecrement; |
| 115 | } |
| 116 | } |