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 public ContextAwareBase(Object declaredOrigin) {
38 this.declaredOrigin = declaredOrigin;
39 }
40
41 public void setContext(Context context) {
42 if (this.context == null) {
43 this.context = context;
44 } else if (this.context != context) {
45 throw new IllegalStateException("Context has been already set");
46 }
47 }
48
49 public Context getContext() {
50 return this.context;
51 }
52
53 public StatusManager getStatusManager() {
54 if (context == null) {
55 return null;
56 }
57 return context.getStatusManager();
58 }
59
60
61
62
63
64
65
66 protected Object getDeclaredOrigin() {
67 return declaredOrigin;
68 }
69
70 public void addStatus(Status status) {
71 if (context == null) {
72 if (noContextWarning++ == 0) {
73 System.out.println("LOGBACK: No context given for " + this);
74 }
75 return;
76 }
77 StatusManager sm = context.getStatusManager();
78 if (sm != null) {
79 sm.add(status);
80 }
81 }
82
83 public void addInfo(String msg) {
84 addStatus(new InfoStatus(msg, getDeclaredOrigin()));
85 }
86
87 public void addInfo(String msg, Throwable ex) {
88 addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
89 }
90
91 public void addWarn(String msg) {
92 addStatus(new WarnStatus(msg, getDeclaredOrigin()));
93 }
94
95 public void addWarn(String msg, Throwable ex) {
96 addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
97 }
98
99 public void addError(String msg) {
100 addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
101 }
102
103 public void addError(String msg, Throwable ex) {
104 addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
105 }
106 }