1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-29 03:05:31 +00:00

Log transfer stats

This commit is contained in:
M66B 2021-10-09 19:16:32 +02:00
parent 6bc8576636
commit f1cae5839e
7 changed files with 67 additions and 5 deletions

View file

@ -635,6 +635,9 @@ public class Protocol {
// ignore it
}
socket = null;
if (traceInput != null && traceOutput != null)
eu.faircode.email.TrafficStatsHelper.report(host, "IMAP",
traceOutput.getSent(), traceInput.getReceived());
}
}

View file

@ -806,6 +806,9 @@ class Protocol {
try {
if (socket != null)
socket.close();
if (traceInput != null && traceOutput != null)
eu.faircode.email.TrafficStatsHelper.report(host, "POP3",
traceOutput.getSent(), traceInput.getReceived());
} catch (IOException ex) {
// ignore it
} finally {

View file

@ -1472,6 +1472,9 @@ public class SMTPTransport extends Transport {
serverOutput = null;
serverInput = null;
lineInputStream = null;
if (traceInput != null && traceOutput != null)
eu.faircode.email.TrafficStatsHelper.report(host, "SMTP",
traceOutput.getSent(), traceInput.getReceived());
if (super.isConnected()) // only notify if already connected
super.close();
}

View file

@ -33,6 +33,8 @@ public class TraceInputStream extends FilterInputStream {
private boolean quote = false;
private OutputStream traceOut;
private long pos = 0;
/**
* Creates an input stream filter built on top of the specified
* input stream.
@ -82,6 +84,7 @@ public class TraceInputStream extends FilterInputStream {
@Override
public int read() throws IOException {
int b = in.read();
pos++;
if (trace && b != -1) {
if (quote)
writeByte(b);
@ -100,6 +103,7 @@ public class TraceInputStream extends FilterInputStream {
@Override
public int read(byte b[], int off, int len) throws IOException {
int count = in.read(b, off, len);
pos += count;
if (trace && count != -1) {
if (quote) {
for (int i = 0; i < count; i++)
@ -137,4 +141,8 @@ public class TraceInputStream extends FilterInputStream {
traceOut.write(b);
}
}
public long getReceived() {
return pos;
}
}

View file

@ -33,7 +33,8 @@ public class TraceOutputStream extends FilterOutputStream {
private boolean quote = false;
private OutputStream traceOut;
private int pos = 0;
private long pos = 0;
private long base = 0;
private int last = 0;
private int total = 0;
private IReport reporter = null;
@ -153,15 +154,20 @@ public class TraceOutputStream extends FilterOutputStream {
}
}
public long getSent() {
return pos;
}
void report() {
if (reporter != null && pos - last > 1024) {
last = pos;
reporter.report(pos, total);
int p = (int) (pos - base);
if (reporter != null && p - last > 1024) {
last = p;
reporter.report(p, total);
}
}
public void setReporter(int total, IReport reporter) {
this.pos = 0;
this.base = pos;
this.last = 0;
this.total = total;
this.reporter = reporter;

View file

@ -190,6 +190,7 @@ public class ApplicationEx extends Application
if (Helper.hasWebView(this))
CookieManager.getInstance().setAcceptCookie(false);
TrafficStatsHelper.init(this);
EncryptionHelper.init(this);
MessageHelper.setSystemProperties(this);

View file

@ -0,0 +1,38 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
public class TrafficStatsHelper {
private static Context ctx;
static void init(Context context) {
ctx = context;
}
public static void report(String host, String protocol, long sent, long received) {
String msg = protocol + " " + host + " tx=" + sent + " rx=" + received;
if (ctx == null)
Log.i(msg);
else
EntityLog.log(ctx, EntityLog.Type.Statistics, msg);
}
}