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