Ceki Gulcu | 054ed39 | 2011-10-16 22:53:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001-2004 The Apache Software Foundation. |
Ceki Gulcu | 7ba0605 | 2011-10-16 10:28:44 +0200 | [diff] [blame] | 3 | * |
Ceki Gulcu | 054ed39 | 2011-10-16 22:53:17 +0200 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Ceki Gulcu | 7ba0605 | 2011-10-16 10:28:44 +0200 | [diff] [blame] | 7 | * |
Ceki Gulcu | 054ed39 | 2011-10-16 22:53:17 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ceki Gulcu | 7ba0605 | 2011-10-16 10:28:44 +0200 | [diff] [blame] | 9 | * |
Ceki Gulcu | 054ed39 | 2011-10-16 22:53:17 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 15 | */ |
Ceki Gulcu | 054ed39 | 2011-10-16 22:53:17 +0200 | [diff] [blame] | 16 | |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 17 | package org.apache.log4j; |
| 18 | |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 19 | import org.slf4j.MDC; |
| 20 | |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 21 | import java.util.Stack; |
| 22 | |
| 23 | /** |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 24 | * A log4j's NDC implemented in terms of SLF4J MDC primitives. |
| 25 | * |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 26 | * @since SLF4J 1.6.0 |
| 27 | */ |
| 28 | |
| 29 | public class NDC { |
| 30 | |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 31 | public final static String PREFIX = "NDC"; |
| 32 | |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 33 | public static void clear() { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 34 | int depth = getDepth(); |
| 35 | for (int i = 0; i < depth; i++) { |
| 36 | String key = PREFIX + i; |
| 37 | MDC.remove(key); |
| 38 | } |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Ceki Gulcu | e14afa6 | 2014-12-14 22:12:23 +0100 | [diff] [blame^] | 41 | @SuppressWarnings("rawtypes") |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 42 | public static Stack cloneStack() { |
| 43 | return null; |
| 44 | } |
| 45 | |
Ceki Gulcu | e14afa6 | 2014-12-14 22:12:23 +0100 | [diff] [blame^] | 46 | @SuppressWarnings("rawtypes") |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 47 | public static void inherit(Stack stack) { |
| 48 | } |
| 49 | |
| 50 | static public String get() { |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | public static int getDepth() { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 55 | int i = 0; |
| 56 | while (true) { |
| 57 | String val = MDC.get(PREFIX + i); |
| 58 | if (val != null) { |
| 59 | i++; |
| 60 | } else { |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | return i; |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public static String pop() { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 68 | int next = getDepth(); |
| 69 | if (next == 0) { |
| 70 | return ""; |
| 71 | } |
| 72 | int last = next - 1; |
| 73 | String key = PREFIX + last; |
| 74 | String val = MDC.get(key); |
| 75 | MDC.remove(key); |
| 76 | return val; |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public static String peek() { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 80 | int next = getDepth(); |
| 81 | if (next == 0) { |
| 82 | return ""; |
| 83 | } |
| 84 | int last = next - 1; |
| 85 | String key = PREFIX + last; |
| 86 | String val = MDC.get(key); |
| 87 | return val; |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public static void push(String message) { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 91 | int next = getDepth(); |
| 92 | MDC.put(PREFIX + next, message); |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static public void remove() { |
Ceki Gulcu | 8a32939 | 2011-07-07 22:27:13 +0200 | [diff] [blame] | 96 | clear(); |
Ceki Gulcu | e2f99d4 | 2010-04-04 21:49:54 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static public void setMaxDepth(int maxDepth) { |
| 100 | } |
| 101 | |
| 102 | } |