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