-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ResilientOutputStreamBase.java
163 lines (138 loc) · 4.56 KB
/
ResilientOutputStreamBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.core.recovery;
import java.io.IOException;
import java.io.OutputStream;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.InfoStatus;
import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.StatusManager;
abstract public class ResilientOutputStreamBase extends OutputStream {
final static int STATUS_COUNT_LIMIT = 2 * 4;
private int noContextWarning = 0;
private int statusCount = 0;
private Context context;
private RecoveryCoordinator recoveryCoordinator;
protected OutputStream os;
protected boolean presumedClean = true;
private boolean isPresumedInError() {
// existence of recoveryCoordinator indicates failed state
return (recoveryCoordinator != null && !presumedClean);
}
public void write(byte b[], int off, int len) {
if (isPresumedInError()) {
if (!recoveryCoordinator.isTooSoon()) {
attemptRecovery();
}
return; // return regardless of the success of the recovery attempt
}
try {
os.write(b, off, len);
postSuccessfulWrite();
} catch (IOException e) {
postIOFailure(e);
}
}
@Override
public void write(int b) {
if (isPresumedInError()) {
if (!recoveryCoordinator.isTooSoon()) {
attemptRecovery();
}
return; // return regardless of the success of the recovery attempt
}
try {
os.write(b);
postSuccessfulWrite();
} catch (IOException e) {
postIOFailure(e);
}
}
@Override
public void flush() {
if (os != null) {
try {
os.flush();
postSuccessfulWrite();
} catch (IOException e) {
postIOFailure(e);
}
}
}
abstract String getDescription();
abstract OutputStream openNewOutputStream() throws IOException;
private void postSuccessfulWrite() {
if (recoveryCoordinator != null) {
recoveryCoordinator = null;
statusCount = 0;
addStatus(new InfoStatus("Recovered from IO failure on " + getDescription(), this));
}
}
public void postIOFailure(IOException e) {
addStatusIfCountNotOverLimit(new ErrorStatus("IO failure while writing to " + getDescription(), this, e));
presumedClean = false;
if (recoveryCoordinator == null) {
recoveryCoordinator = new RecoveryCoordinator();
}
}
@Override
public void close() throws IOException {
if (os != null) {
os.close();
}
}
void attemptRecovery() {
try {
close();
} catch (IOException e) {
}
addStatusIfCountNotOverLimit(new InfoStatus("Attempting to recover from IO failure on " + getDescription(), this));
// subsequent writes must always be in append mode
try {
os = openNewOutputStream();
presumedClean = true;
} catch (IOException e) {
addStatusIfCountNotOverLimit(new ErrorStatus("Failed to open " + getDescription(), this, e));
}
}
void addStatusIfCountNotOverLimit(Status s) {
++statusCount;
if (statusCount < STATUS_COUNT_LIMIT) {
addStatus(s);
}
if (statusCount == STATUS_COUNT_LIMIT) {
addStatus(s);
addStatus(new InfoStatus("Will supress future messages regarding " + getDescription(), this));
}
}
public void addStatus(Status status) {
if (context == null) {
if (noContextWarning++ == 0) {
System.out.println("LOGBACK: No context given for " + this);
}
return;
}
StatusManager sm = context.getStatusManager();
if (sm != null) {
sm.add(status);
}
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}