org.apache.commons.io.input.ReversedLinesFileReader Java Examples

The following examples show how to use org.apache.commons.io.input.ReversedLinesFileReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogsCommand.java    From FlareBot with MIT License 7 votes vote down vote up
@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);
    }
}
 
Example #2
Source File: HelpController.java    From airsonic-advanced with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> getLatestLogEntries(Path logFile) {
    List<String> lines = new LinkedList<>();
    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile.toFile(), Charset.defaultCharset())) {
        String current;
        while ((current = reader.readLine()) != null) {
            if (lines.size() >= LOG_LINES_TO_SHOW) {
                break;
            }
            lines.add(0, current);
        }
        return lines;
    } catch (IOException e) {
        LOG.warn("Could not open log file " + logFile, e);
        return null;
    }
}
 
Example #3
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the last n lines from a file
 * @param file file where the data to be read is.
 * @param numOfLastLinesToRead the number of last lines to read
 * @return read lines
 */
public static String readLastLines(File file, int numOfLastLinesToRead) throws IOException {
    List<String> result = new ArrayList<>();

    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(file, StandardCharsets.UTF_8)) {
        String line;
        while ((line = reader.readLine()) != null && result.size() < numOfLastLinesToRead) {
            result.add(line);
        }
    } catch (IOException e) {
        log.error("Error while reading log file", e);
        throw e;
    }

    Collections.reverse(result);
    return String.join(System.lineSeparator(), result);
}
 
Example #4
Source File: LauncherUtils.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the last n lines from a file
 * @param file file where the data to be read is.
 * @param numOfLastLinesToRead the number of last lines to read
 * @return read lines
 */
public static String readLastLines(File file, int numOfLastLinesToRead) throws IOException {
    List<String> result = new ArrayList<>();

    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(file, StandardCharsets.UTF_8)) {
        String line;
        while ((line = reader.readLine()) != null && result.size() < numOfLastLinesToRead) {
            result.add(line);
        }
    } catch (IOException e) {
        log.error("Error while reading log file", e);
        throw e;
    }

    Collections.reverse(result);
    return String.join(System.lineSeparator(), result);
}
 
Example #5
Source File: HelpController.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> getLatestLogEntries(File logFile) {
    List<String> lines = new ArrayList<>(LOG_LINES_TO_SHOW);
    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile, Charset.defaultCharset())) {
        String current;
        while ((current = reader.readLine()) != null) {
            if (lines.size() >= LOG_LINES_TO_SHOW) {
                break;
            }
            lines.add(0, current);
        }
        return lines;
    } catch (IOException e) {
        LOG.warn("Could not open log file " + logFile, e);
        return null;
    }
}
 
Example #6
Source File: InternalHelpController.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> getLatestLogEntries(File logFile) {
    List<String> lines = new ArrayList<>(LOG_LINES_TO_SHOW);
    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile, Charset.defaultCharset())) {
        String current;
        while ((current = reader.readLine()) != null) {
            if (lines.size() >= LOG_LINES_TO_SHOW) {
                break;
            }
            lines.add(0, current);
        }
        return lines;
    } catch (IOException e) {
        LOG.warn("Could not open log file " + logFile, e);
        return null;
    }
}
 
Example #7
Source File: LastShutdownTimeServiceImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Optional<Date> findBestEstimate(final LogManager logManager) {
  File nexusFile = logManager.getLogFileForLogger(DEFAULT_LOGGER).orElse(null);
  Optional<Date> estimatedTime = Optional.empty();

  if (nexusFile == null) {
    log.warn("Missing log file for {} , so last shutdown time can't be estimated.", DEFAULT_LOGGER);
  } else if(nexusFile.length() == 0) {
    log.warn("Empty log file {} , so last shutdown time can't be estimated.", nexusFile);
  } else {
    try (ReversedLinesFileReader logReader = new ReversedLinesFileReader(nexusFile)) {
      estimatedTime = findShutdownTimeInLog(logReader, START_INDICATOR, nexusPattern, DEFAULT_LINE_READING_LIMIT, GROUP_NAME, nexusFormat);
    } catch (Exception e) {
      log.warn("Failed to process file {}.  Assuming no previous start time", nexusFile, e);
    }
  }

  return estimatedTime;
}
 
Example #8
Source File: LastShutdownTimeServiceImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Optional<Date> findTimeFromPreviousInstance(final ReversedLinesFileReader logReader,
                                                    final Pattern timestampPattern,
                                                    final int lineScanLimit,
                                                    final String groupName,
                                                    final DateFormat dateFormat) throws IOException, ParseException
{
  int lineCount = 0;
  String line;
  while(lineCount++ < lineScanLimit && (line = logReader.readLine()) != null) {
    Matcher matcher = timestampPattern.matcher(line);
    if (matcher.find()) {
      return Optional.of(dateFormat.parse(matcher.group(groupName)));
    }
  }
  return Optional.empty();
}
 
Example #9
Source File: CheckPointStore.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private CheckPoint getLatestCheckPoint(File checkPointFile) {
    if (checkPointFile == null) {
        return null;
    }
    CheckPoint cp = null;
    try (ReversedLinesFileReader fileReader = new ReversedLinesFileReader(checkPointFile, 4096,
            Charset.forName("UTF-8"))) {
        String line = fileReader.readLine();
        while (!CP_END.equals(line) && line != null) {
            line = fileReader.readLine();
        }

        if (line == null) { //not found the checkpoint end line
            return null;
        }

        LinkedList<String> stack = Lists.newLinkedList();
        line = fileReader.readLine();
        while (!CP_START.equals(line) && line != null) {
            stack.push(line);
            line = fileReader.readLine();
        }
        if (line == null) {
            return null;
        }
        StringBuilder cpJson = new StringBuilder();
        while (!stack.isEmpty()) {
            cpJson.append(stack.pop());
        }
        cp = JsonUtil.readValue(cpJson.toString(), CheckPoint.class);
    } catch (IOException e) {
        logger.error("error when parse checkpoint");
    }
    return cp;
}
 
Example #10
Source File: ServiceSimple.java    From jqm with Apache License 2.0 5 votes vote down vote up
@GET
@Path("enginelog")
@Produces(MediaType.TEXT_PLAIN)
public String getEngineLog(@QueryParam("latest") int latest)
{
    if (n == null)
    {
        throw new ErrorDto("can only retrieve a file when the web app runs on top of JQM", "", 7, Status.BAD_REQUEST);
    }

    // Failsafe
    if (latest > 10000)
    {
        latest = 10000;
    }

    File f = new File(FilenameUtils.concat("./logs/", "jqm-" + context.getInitParameter("jqmnode") + ".log"));
    try(ReversedLinesFileReader r =  new ReversedLinesFileReader(f, Charset.defaultCharset()))
    {
        StringBuilder sb = new StringBuilder(latest);
        String buf = r.readLine();
        int i = 1;
        while (buf != null && i <= latest)
        {
            sb.append(buf);
            sb.append(System.getProperty("line.separator"));
            i++;
            buf = r.readLine();
        }
        return sb.toString();
    }
    catch (Exception e)
    {
        throw new ErrorDto("Could not return the desired file", 8, e, Status.NO_CONTENT);
    }
}
 
Example #11
Source File: PartialLogFixer.java    From sequence-mining with GNU General Public License v3.0 5 votes vote down vote up
public static HashMap<Sequence, Double> readLastEMStepSequences(final File logFile) throws IOException {
	final HashMap<Sequence, Double> sequences = new HashMap<>();

	final ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile);
	String line = reader.readLine();
	while (line != null) {

		if (line.contains("Parameter Optimal Sequences:")) {
			final Matcher m = Pattern
					.compile(
							"\\[((?:[0-9]|,| )+?)\\]=\\(((?:(?:[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)|,)+?)\\)")
					.matcher(line);
			while (m.find()) {
				final Sequence sequence = new Sequence();
				final String[] items = m.group(1).split(", ");
				for (final String item : items)
					sequence.add(Integer.parseInt(item));
				final double prob = 1 - Double.parseDouble(m.group(2).split(",")[0]);
				sequences.put(sequence, prob);
			}
			break;
		}
		line = reader.readLine();

	}
	reader.close();

	return sequences;
}
 
Example #12
Source File: CheckPointStore.java    From kylin with Apache License 2.0 5 votes vote down vote up
private CheckPoint getLatestCheckPoint(File checkPointFile) {
    if (checkPointFile == null) {
        return null;
    }
    CheckPoint cp = null;
    try (ReversedLinesFileReader fileReader = new ReversedLinesFileReader(checkPointFile, 4096,
            Charset.forName("UTF-8"))) {
        String line = fileReader.readLine();
        while (!CP_END.equals(line) && line != null) {
            line = fileReader.readLine();
        }

        if (line == null) { //not found the checkpoint end line
            return null;
        }

        LinkedList<String> stack = Lists.newLinkedList();
        line = fileReader.readLine();
        while (!CP_START.equals(line) && line != null) {
            stack.push(line);
            line = fileReader.readLine();
        }
        if (line == null) {
            return null;
        }
        StringBuilder cpJson = new StringBuilder();
        while (!stack.isEmpty()) {
            cpJson.append(stack.pop());
        }
        cp = JsonUtil.readValue(cpJson.toString(), CheckPoint.class);
    } catch (IOException e) {
        logger.error("error when parse checkpoint");
    }
    return cp;
}
 
Example #13
Source File: FileSystemFileProvider.java    From spring-boot-actuator-logview with MIT License 5 votes vote down vote up
@Override
public void tailContent(Path folder, String filename, OutputStream stream, int lines) throws IOException {
    try (ReversedLinesFileReader reader = new ReversedLinesFileReader(getFile(folder, filename))) {
        int i = 0;
        String line;
        List<String> content = new ArrayList<>();
        while ((line = reader.readLine()) != null && i++ < lines) {
            content.add(line);
        }
        Collections.reverse(content);
        IOUtils.writeLines(content, System.lineSeparator(), stream);
    }
}
 
Example #14
Source File: PartialLogFixer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public static HashMap<Sequence, Double> readLastEMStepSequences(final File logFile) throws IOException {
	final HashMap<Sequence, Double> sequences = new HashMap<>();

	final ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile);
	String line = reader.readLine();
	while (line != null) {

		if (line.contains("Parameter Optimal Sequences:")) {
			final Matcher m = Pattern
					.compile(
							"\\[((?:[0-9]|,| )+?)\\](?:\\^\\(([0-9]+)\\))?=([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)")
					.matcher(line);
			while (m.find()) {
				final Sequence sequence = new Sequence();
				final String[] items = m.group(1).split(", ");
				for (final String item : items)
					sequence.add(Integer.parseInt(item));
				if (m.group(2) != null) { // has occurrence
					for (int i = 0; i < Integer.parseInt(m.group(2)) - 1; i++)
						sequence.incrementOccurence();
				}
				final double prob = Double.parseDouble(m.group(3));
				sequences.put(sequence, prob);
			}
			break;
		}
		line = reader.readLine();

	}
	reader.close();

	return sequences;
}
 
Example #15
Source File: LastShutdownTimeServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The main work of this method is done in moving the logReader to the point just before the last shutdown
 */
private int advanceReaderToShutdownLine(final ReversedLinesFileReader logReader,
                                        final String startIndicator,
                                        final int lineScanLimit) throws IOException {
  int lineCount = 0;
  String line;
  while(lineCount++ < lineScanLimit && (line = logReader.readLine()) != null) {
    if (line.contains(startIndicator)) {
      break;
    }
  }

  return lineCount;
}
 
Example #16
Source File: LastShutdownTimeServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@VisibleForTesting
Optional<Date> findShutdownTimeInLog(final ReversedLinesFileReader logReader,
                                     final String startIndicator,
                                     final Pattern timestampPattern,
                                     final int lineScanLimit,
                                     final String groupName,
                                     final DateFormat dateFormat) throws IOException, ParseException {

  int linesRead = advanceReaderToShutdownLine(logReader, startIndicator, lineScanLimit);
  return findTimeFromPreviousInstance(logReader, timestampPattern, lineScanLimit - linesRead, groupName, dateFormat);
}
 
Example #17
Source File: ConsoleEndpoint.java    From Cleanstone with MIT License 5 votes vote down vote up
/**
 * Returns a reversed Output from the Logfile.
 *
 * @return
 * @throws IOException
 */
@ReadOperation
public List<String> getLog() throws IOException {
    Resource logFileResource = getLogFileResource();
    if (logFileResource == null || !logFileResource.isReadable()) {
        return null;
    }

    ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(logFileResource.getFile(), StandardCharsets.UTF_8);

    List<String> log = new ArrayList<>();

    for (int i = 0; i < 20; i++) {
        final String line = reversedLinesFileReader.readLine();

        if (line.startsWith("\t") || line.isEmpty()) { //Remove Stacktrace and Empty lines
            i--;
            continue;
        }

        log.add(line);
    }

    reversedLinesFileReader.close();

    return log;
}
 
Example #18
Source File: OperationManager.java    From product-emm with Apache License 2.0 4 votes vote down vote up
public static String getOperationResponseFromLogcat(Context context, String logcat) throws IOException {
    File logcatFile = new File(logcat);
    if (logcatFile.exists() && logcatFile.canRead()) {
        DeviceInfo deviceInfo = new DeviceInfo(context);
        EventPayload eventPayload = new EventPayload();
        eventPayload.setPayload(logcat);
        eventPayload.setType("LOGCAT");
        eventPayload.setDeviceIdentifier(deviceInfo.getDeviceId());

        StringBuilder emmBuilder = new StringBuilder();
        StringBuilder publisherBuilder = new StringBuilder();
        int index = 0;
        String line;
        ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(logcatFile, Charset.forName("US-ASCII"));
        while ((line = reversedLinesFileReader.readLine()) != null) {
            publisherBuilder.insert(0, "\n");
            publisherBuilder.insert(0, line);
            //OPERATION_RESPONSE filed in the DM_DEVICE_OPERATION_RESPONSE is declared as a blob and hence can only hold 64Kb.
            //So we don't want to throw exceptions in the server. Limiting the response in here to limit the server traffic also.
            if (emmBuilder.length() < Character.MAX_VALUE - 8192) { //Keeping 8kB for rest of the response payload.
                emmBuilder.insert(0, "\n");
                emmBuilder.insert(0, line);
            }
            if (++index >= Constants.LogPublisher.NUMBER_OF_LOG_LINES) {
                break;
            }
        }
        LogPublisherFactory publisher = new LogPublisherFactory(context);
        if (publisher.getLogPublisher() != null) {
            eventPayload.setPayload(publisherBuilder.toString());
            publisher.getLogPublisher().publish(eventPayload);
            if (Constants.DEBUG_MODE_ENABLED) {
                Log.d(TAG, "Logcat published size: " + eventPayload.getPayload().length());
            }
        }
        eventPayload.setPayload(emmBuilder.toString());
        Gson logcatResponse = new Gson();
        logcatFile.delete();
        if (Constants.DEBUG_MODE_ENABLED) {
            Log.d(TAG, "Logcat payload size: " + eventPayload.getPayload().length());
        }
        return logcatResponse.toJson(eventPayload);
    } else {
        throw new IOException("Unable to find or read log file.");
    }
}
 
Example #19
Source File: LogDB.java    From Babler with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a new ID by checking the last ID in the log, if the log contains non int ID's
 * the method will generate a new random ID.
 * @param lang The languageCode of the log
 * @return Last id in the log +1 OR a random int
 * @throws Exception if failed to get id
 */
public static String getNewId(String lang) throws Exception {
    File f = new File(SCRAPING_FOLDER+lang+LOG_FILE);
    String id = "";

    if(!f.exists()){
        FileUtils.writeStringToFile(f,"id: 1,url:www.test.com\n");
    }

    try {
        ReversedLinesFileReader fr = new ReversedLinesFileReader(f);

        String lastLine = fr.readLine();
        int commaLocation;
        try {
             commaLocation = lastLine.indexOf(",");
        }
        catch (Exception ex){
            lastLine = fr.readLine();
            commaLocation = lastLine.indexOf(",");
        }
        id = lastLine.substring(4, commaLocation);

        if (id == "0") {
            throw new Exception("Couldn't get ID");
        }

    } catch (IOException e) {
        log.error(e);
    }

    try{
        int numId = Integer.parseInt(id);
        id = String.valueOf(++numId);
        return id;
    }
    catch(Exception exx){
        Random rand = new Random();
        id = String.valueOf(rand.nextInt()%10000);
        return id;
    }

}
 
Example #20
Source File: TableScannerITCase.java    From Rhombus with MIT License 4 votes vote down vote up
private void verifySavepoints(Integer numPartitions, String savepointDirectoryName, String objectType, ObjectMapper om, List<Map.Entry<Long, Long>> ranges) throws Exception {
	// Open up the savepoint directory
	File savepointDirectory = new File(savepointDirectoryName);
	assertTrue(savepointDirectory.exists());
	assertTrue(savepointDirectory.isDirectory());

	File[] fileArray = savepointDirectory.listFiles();
	assertNotNull(fileArray);

	// Make sure we have all the savepoint files we expect
	Map<String, File> files = Maps.newHashMap();
	for (File savepoint : fileArray) {
		files.put(savepoint.getName(), savepoint);
	}

	boolean foundAtLeastOneLine = false;
	UUID highestUuid = null;
	for (int i = 0; i < numPartitions; i++) {
		String filename = TableScanner.getSavepointFilename(i);
		assertTrue(files.containsKey(filename));

		File file = files.get(filename);
		ReversedLinesFileReader reader = new ReversedLinesFileReader(file);
		String line = reader.readLine();
		// A null line is actually ok, that just means there were no results in that partition's token range this time around
		if (line != null) {
			foundAtLeastOneLine = true;
			UUID savedUuid = UUID.fromString(line);
			assertNotNull(savedUuid);
			List<Map<String, Object>> values = om.scanTableWithStartId(objectType, savedUuid.toString(), TableScanner.maxToken, 1L);

			// This means there is no next id from this uuid so our normal check doesn't work.
			// This also means this is the highest uuid in the total range (might not be in the last partition if the last partition didn't end up with any objects)
			if (values.size() == 0) {
				// Make sure this only happens once
				assertNull(highestUuid);
				highestUuid = savedUuid;
			} else {
				// Otherwise there is a next uuid from the saved point, so compare that to the next uuid from the end of the partition's range and make sure they match
				UUID nextSavedUuid = (UUID) values.get(0).get("id");
				UUID nextExpectedUuid = (UUID) om.scanTableWithStartToken(objectType, ranges.get(i).getValue(), TableScanner.maxToken, 1L).get(0).get("id");
				assertEquals(nextExpectedUuid, nextSavedUuid);
			}
		}
	}
	assertTrue(foundAtLeastOneLine);
}