1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.spi;
15
16 import ch.qos.logback.core.Context;
17 import ch.qos.logback.core.status.ErrorStatus;
18 import ch.qos.logback.core.status.InfoStatus;
19 import ch.qos.logback.core.status.Status;
20 import ch.qos.logback.core.status.StatusManager;
21 import ch.qos.logback.core.status.WarnStatus;
22
23
24
25
26
27
28
29 public class ContextAwareBase implements ContextAware {
30 private int noContextWarning = 0;
31 protected Context context;
32 final Object declaredOrigin;
33
34 public ContextAwareBase() {
35 declaredOrigin = this;
36 }
37
38 public ContextAwareBase(ContextAware declaredOrigin) {
39 this.declaredOrigin = declaredOrigin;
40 }
41
42 public void setContext(Context context) {
43 if (this.context == null) {
44 this.context = context;
45 } else if (this.context != context) {
46 throw new IllegalStateException("Context has been already set");
47 }
48 }
49
50 public Context getContext() {
51 return this.context;
52 }
53
54 public StatusManager getStatusManager() {
55 if (context == null) {
56 return null;
57 }
58 return context.getStatusManager();
59 }
60
61
62
63
64
65
66
67 protected Object getDeclaredOrigin() {
68 return declaredOrigin;
69 }
70
71 public void addStatus(Status status) {
72 if (context == null) {
73 if (noContextWarning++ == 0) {
74 System.out.println("LOGBACK: No context given for " + this);
75 }
76 return;
77 }
78 StatusManager sm = context.getStatusManager();
79 if (sm != null) {
80 sm.add(status);
81 }
82 }
83
84 public void addInfo(String msg) {
85 addStatus(new InfoStatus(msg, getDeclaredOrigin()));
86 }
87
88 public void addInfo(String msg, Throwable ex) {
89 addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
90 }
91
92 public void addWarn(String msg) {
93 addStatus(new WarnStatus(msg, getDeclaredOrigin()));
94 }
95
96 public void addWarn(String msg, Throwable ex) {
97 addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
98 }
99
100 public void addError(String msg) {
101 addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
102 }
103
104 public void addError(String msg, Throwable ex) {
105 addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
106 }
107 }