blob: 8df9f7a326fb648923dd53526db48459597087ef [file] [log] [blame]
Ceki Gulcu054ed392011-10-16 22:53:17 +02001/*
2 * Copyright 2001-2004 The Apache Software Foundation.
Ceki Gulcu7ba06052011-10-16 10:28:44 +02003 *
Ceki Gulcu054ed392011-10-16 22:53:17 +02004 * 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 Gulcu7ba06052011-10-16 10:28:44 +02007 *
Ceki Gulcu054ed392011-10-16 22:53:17 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Ceki Gulcu7ba06052011-10-16 10:28:44 +02009 *
Ceki Gulcu054ed392011-10-16 22:53:17 +020010 * 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 Gulcue2f99d42010-04-04 21:49:54 +020015 */
Ceki Gulcu054ed392011-10-16 22:53:17 +020016
Ceki Gulcue2f99d42010-04-04 21:49:54 +020017package org.apache.log4j;
18
Ceki Gulcu8a329392011-07-07 22:27:13 +020019import org.slf4j.MDC;
20
Ceki Gulcue2f99d42010-04-04 21:49:54 +020021import java.util.Stack;
22
23/**
Ceki Gulcu8a329392011-07-07 22:27:13 +020024 * A log4j's NDC implemented in terms of SLF4J MDC primitives.
25 *
Ceki Gulcue2f99d42010-04-04 21:49:54 +020026 * @since SLF4J 1.6.0
27 */
28
29public class NDC {
30
Ceki Gulcu8a329392011-07-07 22:27:13 +020031 public final static String PREFIX = "NDC";
32
Ceki Gulcue2f99d42010-04-04 21:49:54 +020033 public static void clear() {
Ceki Gulcu8a329392011-07-07 22:27:13 +020034 int depth = getDepth();
35 for (int i = 0; i < depth; i++) {
36 String key = PREFIX + i;
37 MDC.remove(key);
38 }
Ceki Gulcue2f99d42010-04-04 21:49:54 +020039 }
40
Ceki Gulcue14afa62014-12-14 22:12:23 +010041 @SuppressWarnings("rawtypes")
Ceki Gulcue2f99d42010-04-04 21:49:54 +020042 public static Stack cloneStack() {
43 return null;
44 }
45
Ceki Gulcue14afa62014-12-14 22:12:23 +010046 @SuppressWarnings("rawtypes")
Ceki Gulcue2f99d42010-04-04 21:49:54 +020047 public static void inherit(Stack stack) {
48 }
49
50 static public String get() {
51 return null;
52 }
53
54 public static int getDepth() {
Ceki Gulcu8a329392011-07-07 22:27:13 +020055 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 Gulcue2f99d42010-04-04 21:49:54 +020065 }
66
67 public static String pop() {
Ceki Gulcu8a329392011-07-07 22:27:13 +020068 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 Gulcue2f99d42010-04-04 21:49:54 +020077 }
78
79 public static String peek() {
Ceki Gulcu8a329392011-07-07 22:27:13 +020080 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 Gulcue2f99d42010-04-04 21:49:54 +020088 }
89
90 public static void push(String message) {
Ceki Gulcu8a329392011-07-07 22:27:13 +020091 int next = getDepth();
92 MDC.put(PREFIX + next, message);
Ceki Gulcue2f99d42010-04-04 21:49:54 +020093 }
94
95 static public void remove() {
Ceki Gulcu8a329392011-07-07 22:27:13 +020096 clear();
Ceki Gulcue2f99d42010-04-04 21:49:54 +020097 }
98
99 static public void setMaxDepth(int maxDepth) {
100 }
101
102}