Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line wrapping to logs exposed via Touch UI #788

Merged
merged 3 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface AcHistoryService {
public String getLastInstallationHistory();

/**
* Exposes the log contents of a specific run of the AC Tool. The given index is volatile (due to history order changes),
* Returns the log's content of a specific run of the AC Tool. The given index is volatile (due to history order changes),
* so consider using {@link #getLogFromHistory(String, boolean, boolean)} instead.
* @param n the index of the child node below the history root node which should be returned
* @param inHtmlFormat
Expand All @@ -47,8 +47,27 @@ public interface AcHistoryService {
*/
public String getLogFromHistory(int n, boolean inHtmlFormat, boolean includeVerbose);

/**
* Shortcut for {@link #getLogFromHistory(String, boolean, boolean, int)} with last argument being -1 (no line wrapping).
* @param id
* @param inHtmlFormat
* @param includeVerbose
* @return
* @throws RepositoryException
*/
public String getLogFromHistory(String id, boolean inHtmlFormat, boolean includeVerbose) throws RepositoryException;

/**
* Returns the log's content of a specific run of the AC Tool.
* @param id the id of the child node below the history root node which should be returned
* @param inHtmlFormat
* @param includeVerbose
* @param maxLineWidth
* @return
* @throws RepositoryException
*/
public String getLogFromHistory(String id, boolean inHtmlFormat, boolean includeVerbose, int maxLineWidth) throws RepositoryException;

public boolean wasLastPersistHistoryCallSuccessful();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -25,6 +24,7 @@
import javax.jcr.Session;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -123,12 +123,12 @@ public List<AcToolExecution> getAcToolExecutions() throws RepositoryException {
}
}

private String getLogHtml(Session session, String path, boolean includeVerbose) {
return HistoryUtils.getLogHtml(session, path, includeVerbose);
private String getLogHtml(Session session, String path, boolean includeVerbose, int maxLineWidth) {
return HistoryUtils.getLogHtml(session, path, includeVerbose, maxLineWidth);
}

private String getLogTxt(Session session, String path, boolean includeVerbose) {
return HistoryUtils.getLogTxt(session, path, includeVerbose);
private String getLogTxt(Session session, String path, boolean includeVerbose, int maxLineWidth) {
return HistoryUtils.getLogTxt(session, path, includeVerbose, maxLineWidth);
}

@Override
Expand All @@ -145,7 +145,7 @@ public String getLastInstallationHistory() {
Node lastHistoryNode = it.nextNode();

if (lastHistoryNode != null) {
history = getLogHtml(session, lastHistoryNode.getName(), true);
history = getLogHtml(session, lastHistoryNode.getName(), true, -1);
}
} else {
history = "no history found!";
Expand Down Expand Up @@ -199,7 +199,7 @@ public String getLogFromHistory(int n, boolean inHtmlFormat, boolean includeVerb
if(n <= acToolExecutions.size()) {
AcToolExecution acToolExecution = acToolExecutions.get(n-1);
String path = acToolExecution.getLogsPath();
history = inHtmlFormat ? getLogHtml(session, path, includeVerbose) : getLogTxt(session, path, includeVerbose);
history = inHtmlFormat ? getLogHtml(session, path, includeVerbose, -1) : getLogTxt(session, path, includeVerbose, -1);
}

} catch (RepositoryException e) {
Expand All @@ -214,13 +214,18 @@ public String getLogFromHistory(int n, boolean inHtmlFormat, boolean includeVerb

@Override
public String getLogFromHistory(String id, boolean inHtmlFormat, boolean includeVerbose) throws RepositoryException {
return getLogFromHistory(id, inHtmlFormat, includeVerbose, -1);
}

@Override
public String getLogFromHistory(String id, boolean inHtmlFormat, boolean includeVerbose, int maxLineWidth) throws RepositoryException {
Session session = null;
try {
session = repository.loginService(null, null);
// construct path from id
Node acHistoryRootNode = HistoryUtils.getAcHistoryRootNode(session);
String path = HistoryUtils.getPathFromId(id, acHistoryRootNode.getPath());
return inHtmlFormat ? getLogHtml(session, path, includeVerbose) : getLogTxt(session, path, includeVerbose);
return inHtmlFormat ? getLogHtml(session, path, includeVerbose, maxLineWidth) : getLogTxt(session, path, includeVerbose, maxLineWidth);
} finally {
if (session != null) {
session.logout();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package biz.netcentric.cq.tools.actool.history.impl;

import java.io.BufferedReader;

/*-
* #%L
* Access Control Tool Bundle
Expand All @@ -15,6 +17,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -35,6 +38,7 @@

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -292,20 +296,20 @@ static String getPathFromId(String id, String rootPath) {
return rootPath + "/" + HISTORY_NODE_NAME_PREFIX + id;
}

public static String getLogTxt(final Session session, final String path, boolean includeVerbose) {
return getLog(session, path, "\n", includeVerbose).toString();
public static String getLogTxt(final Session session, final String path, boolean includeVerbose, int maxLineWidth) {
return getLog(session, path, "\n", includeVerbose, maxLineWidth);
}

public static String getLogHtml(final Session session, final String path, boolean includeVerbose) {
return getLog(session, path, "<br />", includeVerbose).toString();
public static String getLogHtml(final Session session, final String path, boolean includeVerbose, int maxLineWidth) {
return getLog(session, path, "<br />", includeVerbose, maxLineWidth);
}

/**
* Method which assembles String containing informations of the properties
* of the respective history node which is specified by the path parameter
*/
public static String getLog(final Session session, final String path,
final String lineFeedSymbol, boolean includeVerbose) {
final String lineFeedSymbol, boolean includeVerbose, int maxLineWidth) {

StringBuilder sb = new StringBuilder();
try {
Expand All @@ -318,18 +322,18 @@ public static String getLog(final Session session, final String path,
.getString());

if(historyNode.hasProperty(PROPERTY_MESSAGES)) {
sb.append(lineFeedSymbol
+ historyNode.getProperty(PROPERTY_MESSAGES)
.getString().replace("\n", lineFeedSymbol));
sb.append(PersistableInstallationLogger.EOL);
sb.append(historyNode.getProperty(PROPERTY_MESSAGES)
.getString());
} else {
Node logFileNode;
if(includeVerbose) {
logFileNode = historyNode.getNode(LOG_FILE_NAME_VERBOSE);
} else {
logFileNode = historyNode.getNode(LOG_FILE_NAME);
}
sb.append(lineFeedSymbol
+ IOUtils.toString(JcrUtils.readFile(logFileNode), StandardCharsets.UTF_8).replace("\n", lineFeedSymbol));
sb.append(PersistableInstallationLogger.EOL);
sb.append(IOUtils.toString(JcrUtils.readFile(logFileNode), StandardCharsets.UTF_8));
}

sb.append(lineFeedSymbol
Expand All @@ -341,11 +345,23 @@ public static String getLog(final Session session, final String path,
+ historyNode.getProperty(PROPERTY_SUCCESS)
.getBoolean());
}
// normalize line feeds and wrap lines
StringReader reader = new StringReader(sb.toString());
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder normalizingLineFeedStringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
if (maxLineWidth > 0 && line.length() > maxLineWidth) {
line = WordUtils.wrap(line, maxLineWidth, lineFeedSymbol, false);
}
normalizingLineFeedStringBuilder.append(line);
normalizingLineFeedStringBuilder.append(lineFeedSymbol);
}
return normalizingLineFeedStringBuilder.toString();
} catch (IOException|RepositoryException e) {
sb.append(lineFeedSymbol+"ERROR while retrieving log: "+e);
LOG.error("ERROR while retrieving log: "+e, e);
return "ERROR while retrieving log: "+e;
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

package biz.netcentric.cq.tools.actool.history.impl;

import java.io.PrintWriter;
import java.io.StringWriter;

/*-
* #%L
* Access Control Tool Bundle
Expand Down Expand Up @@ -35,6 +38,7 @@

public class PersistableInstallationLogger implements InstallationLogger, InstallationLog, InstallationResult {

static final String EOL = "\n";
protected static final String MSG_IDENTIFIER_ERROR = "ERROR: ";
protected static final String MSG_IDENTIFIER_WARNING = "WARNING: ";

Expand Down Expand Up @@ -145,16 +149,29 @@ protected void addMessage(String message) {

@Override
public void addError(Logger log, String error, Throwable e) {
log.error(error, e);
if (e != null) {
log.error(error, e);
} else {
log.error(error);
}
addError(error, e);
}

public void addError(final String error, Throwable e) {
String fullErrorValue = error + " / e=" + e;
String fullErrorValue = error;
if (e != null) {
fullErrorValue += " / e=" + e;
}
errors.add(new HistoryEntry(msgIndex, new Timestamp(
new Date().getTime()), MSG_IDENTIFIER_ERROR + fullErrorValue));
success = false;
msgIndex++;
if (e != null) {
// add the stack trace as verbose message
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
addVerboseMessage(sw.toString());
}
}

@Override
Expand Down Expand Up @@ -192,11 +209,11 @@ public void setSuccess(final boolean success) {
public String toString() {
StringBuilder sb = new StringBuilder();

sb.append("\n" + getMessageHistory() + "\n");
sb.append(EOL + getMessageHistory() + EOL);

sb.append("\n" + "Execution time: " + msHumanReadable(executionTime) + "\n");
sb.append(EOL + "Execution time: " + msHumanReadable(executionTime) + EOL);

sb.append("\n" + "Success: " + success);
sb.append(EOL + "Success: " + success);

return sb.toString();
}
Expand Down Expand Up @@ -236,7 +253,7 @@ private String getMessageString(Set<HistoryEntry> messageHistorySet) {
StringBuilder sb = new StringBuilder();
if (!messageHistorySet.isEmpty()) {
for (HistoryEntry entry : messageHistorySet) {
sb.append("\n" + timestampFormat.format(entry.getTimestamp()) + ": "
sb.append(EOL + timestampFormat.format(entry.getTimestamp()) + ": "
+ entry.getMessage());
}
}
Expand Down
Loading
Loading