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