use of org.apache.commons.io.input.ReversedLinesFileReader in project libresonic by Libresonic.
the class HelpController method getLatestLogEntries.
private static List<String> getLatestLogEntries(File logFile) {
try {
List<String> lines = new ArrayList<>(LOG_LINES_TO_SHOW);
ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile, Charset.defaultCharset());
String current;
while ((current = reader.readLine()) != null && lines.size() < LOG_LINES_TO_SHOW) {
lines.add(0, current);
}
return lines;
} catch (Exception e) {
logger.warn("Could not open log file " + logFile, e);
return null;
}
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project FlareBot by FlareBot.
the class LogsCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
int lineCount = DEFAULT_LINE_COUNT;
if (args.length == 1) {
lineCount = GeneralUtils.getInt(args[0], DEFAULT_LINE_COUNT);
}
try {
ReversedLinesFileReader rlfr = new ReversedLinesFileReader(new File("latest.log"), Charset.forName("UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
for (int i = 0; i < lineCount; i++) {
line = rlfr.readLine();
if (line == null)
break;
sb.append(line).append("\n");
}
String pasteUrl = MessageUtils.paste(sb.toString());
MessageUtils.sendPM(channel, sender, pasteUrl == null ? "null" : pasteUrl, "Could not DM you the logs! " + "Please make sure the privacy settings allow me :( dis is pwivate, me need to send pwivately.");
} catch (IOException e) {
FlareBot.LOGGER.error("Failed to read latest.log", e);
MessageUtils.sendException("Failed to read latest.log", e, channel);
}
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project top20 by algorithmica-repository.
the class NthLineFromEnd method nthLineFromEnd4.
public static String nthLineFromEnd4(String file, int n) throws Exception {
ReversedLinesFileReader rfr = new ReversedLinesFileReader(new File(file));
int count = 0;
String line = null;
while ((line = rfr.readLine()) != null) {
if (++count == n)
break;
}
rfr.close();
return line;
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project structr by structr.
the class ServerLogFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
int lines = 50;
if (sources != null && sources.length > 0 && sources[0] instanceof Number) {
lines = ((Number) sources[0]).intValue();
}
try (final ReversedLinesFileReader reader = new ReversedLinesFileReader(getServerlogFile(), Charset.forName("utf-8"))) {
final StringBuilder sb = new StringBuilder();
while (lines > 0) {
final String line = reader.readLine();
if (line == null) {
lines = 0;
} else {
sb.insert(0, line.concat("\n"));
lines--;
}
}
return sb.toString();
} catch (IOException ex) {
logger.warn("", ex);
}
return "";
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project top20 by algorithmica-repository.
the class FindKthLinefromEnd method findkthline3.
public static String findkthline3(File f, int k) throws Exception {
ReversedLinesFileReader rfr = new ReversedLinesFileReader(f);
String line = null;
int count = 0;
while ((line = rfr.readLine()) != null) {
if (++count == k)
break;
}
rfr.close();
return line;
}
Aggregations