Skip to content

Commit

Permalink
feat: show only a current session log when Help>Log (#1077)
Browse files Browse the repository at this point in the history
* feat: introduce LogDialogController

- Add LogDialogController class
- Update form with NetBeans Gui editor

Signed-off-by: Hiroshi Miura <[email protected]>

* feat: log to OmegaT_session_yyyyMMddHHmmss.log file

Signed-off-by: Hiroshi Miura <[email protected]>

* feat: use timestap for log message

Signed-off-by: Hiroshi Miura <[email protected]>

* docs: update manual

Signed-off-by: Hiroshi Miura <[email protected]>

* fix: remove unused property

Signed-off-by: Hiroshi Miura <[email protected]>

* feat: accept both size, count and retention

Signed-off-by: Hiroshi Miura <[email protected]>

---------

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr authored Jul 25, 2024
1 parent ef4b01a commit 4541e8a
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 134 deletions.
10 changes: 7 additions & 3 deletions doc_src/en/App_ConfigurationFolder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@
<varlistentry id="configuration.folder.default.contents.logs">
<term id="configuration.folder.default.contents.logs.title">logs/</term>
<listitem>
<para>This folder contains a number of log files. The most current is
<filename>OmegaT.log</filename>.</para>
<para>This folder contains a number of log files. The file name format is
<filename>OmegaT_99999_yyyyMMdd-hhmmss.log</filename>.</para>

<para>99999 part is 5 digit session id from 00000 to 99999. It will be random number. </para>

<para>yyyyMMdd-hhmmss part is a date and time, like 20240704-201200 means July 4th, 2024, 20:12:00 in local time.</para>

<para>These files record various internal state and program event
messages generated while OmegaT is running. If OmegaT behaves
Expand All @@ -84,7 +88,7 @@

<para>Use <link linkend="menus.help" endterm="menus.help.title"/><link
linkend="menus.help.log" endterm="menus.help.log.title"/> to view the
contents of the file.</para>
contents of the log file of current session.</para>
</listitem>
</varlistentry>

Expand Down
8 changes: 5 additions & 3 deletions src/org/omegat/gui/dialogs/LogDialog.form
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Properties>
<Property name="editable" type="boolean" value="false"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="0"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
Expand Down Expand Up @@ -71,10 +74,8 @@
<ResourceString bundle="org/omegat/Bundle.properties" key="BUTTON_SAVE_AS" replaceFormat="OStrings.getString(&quot;{key}&quot;)"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveAsButtonActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="0"/>
<AuxValue name="generateMnemonicsCode" type="java.lang.Boolean" value="true"/>
</AuxValues>
</Component>
Expand All @@ -88,6 +89,7 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="okButtonActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="0"/>
<AuxValue name="generateMnemonicsCode" type="java.lang.Boolean" value="true"/>
</AuxValues>
</Component>
Expand Down
76 changes: 8 additions & 68 deletions src/org/omegat/gui/dialogs/LogDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,9 @@
package org.omegat.gui.dialogs;

import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import javax.swing.JFileChooser;
import javax.swing.SwingWorker;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.omegat.util.Log;
import org.omegat.util.OStrings;
import org.omegat.util.Preferences;
import org.omegat.util.gui.OSXIntegration;
import org.omegat.util.gui.StaticUIUtils;

/**
Expand All @@ -57,9 +46,6 @@ public class LogDialog extends javax.swing.JDialog {
public static final int RET_CANCEL = 0;
/** A return status code - returned if OK button has been pressed */
public static final int RET_OK = 1;
public static final String DIALOG_NAME = "log_dialog";
public static final String OK_BUTTON_NAME = "log_dialog_ok_button";
public static final String LOG_TEXTPANE_NAME = "log_dialog_log_textpane";

private int returnStatus = RET_CANCEL;

Expand All @@ -71,9 +57,6 @@ public LogDialog(java.awt.Frame parent) {
initComponents();

setTitle(OStrings.getString("LOGDIALOG_TITLE") + " " + Log.getLogFileName());
setName(DIALOG_NAME);
okButton.setName(OK_BUTTON_NAME);
logTextPane.setName(LOG_TEXTPANE_NAME);

StaticUIUtils.setEscapeClosable(this);

Expand All @@ -82,26 +65,6 @@ public LogDialog(java.awt.Frame parent) {

Font currFont = logTextPane.getFont();
logTextPane.setFont(new Font(Font.MONOSPACED, currFont.getStyle(), currFont.getSize()));

File logLocation = new File(Log.getLogFilePath());
new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
try (FileInputStream fis = new FileInputStream(logLocation)) {
return IOUtils.toString(fis, StandardCharsets.UTF_8);
} catch (Exception e) {
return "";
}
}
protected void done() {
try {
logTextPane.setText(get());
} catch (Exception e) {
Log.log(e);
}
OSXIntegration.setProxyIcon(getRootPane(), logLocation);
};
}.execute();
}

/**
Expand All @@ -116,6 +79,7 @@ public int getReturnStatus() {
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

Expand Down Expand Up @@ -143,11 +107,6 @@ public void windowClosing(java.awt.event.WindowEvent evt) {
innerButtonPanel.setLayout(new javax.swing.BoxLayout(innerButtonPanel, javax.swing.BoxLayout.LINE_AXIS));

org.openide.awt.Mnemonics.setLocalizedText(saveAsButton, OStrings.getString("BUTTON_SAVE_AS")); // NOI18N
saveAsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveAsButtonActionPerformed(evt);
}
});
innerButtonPanel.add(saveAsButton);

org.openide.awt.Mnemonics.setLocalizedText(okButton, OStrings.getString("BUTTON_OK")); // NOI18N
Expand All @@ -165,32 +124,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
pack();
}// </editor-fold>//GEN-END:initComponents

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
doClose(RET_OK);
}//GEN-LAST:event_okButtonActionPerformed
}// GEN-LAST:event_okButtonActionPerformed

private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
private void closeDialog(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_closeDialog
doClose(RET_CANCEL);
}//GEN-LAST:event_closeDialog

private void saveAsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsButtonActionPerformed
String curDir = Preferences.getPreferenceDefault(Preferences.CURRENT_FOLDER, System.getProperty("user.home"));
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(curDir, Log.getLogFileName()));
if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
File outFile = chooser.getSelectedFile();
if (outFile.exists() && !FileCollisionDialog.promptToReplace(this, outFile.getName())) {
return;
}
try {
FileUtils.copyFile(new File(Log.getLogFilePath()), chooser.getSelectedFile());
} catch (IOException ex) {
Log.log(ex);
}
Preferences.setPreference(Preferences.CURRENT_FOLDER, outFile.getParent());
}//GEN-LAST:event_saveAsButtonActionPerformed
}// GEN-LAST:event_closeDialog

private void doClose(int retStatus) {
returnStatus = retStatus;
Expand All @@ -201,9 +141,9 @@ private void doClose(int retStatus) {
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel buttonPanel;
private javax.swing.JPanel innerButtonPanel;
private javax.swing.JTextPane logTextPane;
private javax.swing.JButton okButton;
private javax.swing.JButton saveAsButton;
javax.swing.JTextPane logTextPane;
javax.swing.JButton okButton;
javax.swing.JButton saveAsButton;
private javax.swing.JScrollPane scroll;
// End of variables declaration//GEN-END:variables
}
151 changes: 151 additions & 0 deletions src/org/omegat/gui/dialogs/LogDialogController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*******************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT 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.
OmegaT 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 this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/

package org.omegat.gui.dialogs;

import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;

import org.omegat.util.Log;
import org.omegat.util.Preferences;
import org.omegat.util.gui.OSXIntegration;

/**
* @author miurahr
*/
public class LogDialogController {
public static final String DIALOG_NAME = "log_dialog";
public static final String OK_BUTTON_NAME = "log_dialog_ok_button";
public static final String SAVE_AS_BUTTON_NAME = "log_dialog_save_as_button";
public static final String LOG_TEXTPANE_NAME = "log_dialog_log_textpane";
private final LogDialog logDialog;

/**
* Create LogDialog dialog.
*
* @param parent
* parent frame of the dialog.
*/
public LogDialogController(JFrame parent) {
logDialog = new LogDialog(parent);
}

/**
* Utility function to show LogDialog dialog and load current session log.
*
* @param parent
* parent frame of the dialog.
*/
public static void show(JFrame parent) {
new LogDialogController(parent).show();
}

/**
* Load the current session log on the LogDialog and show it.
*/
public void show() {
logDialog.setName(DIALOG_NAME);
logDialog.okButton.setName(OK_BUTTON_NAME);
logDialog.saveAsButton.setName(SAVE_AS_BUTTON_NAME);
logDialog.logTextPane.setName(LOG_TEXTPANE_NAME);
logDialog.saveAsButton.addActionListener(this::saveAsButtonActionPerformed);
loadCurrentSessionLog(logDialog.logTextPane);
logDialog.setVisible(true);
}

private void loadCurrentSessionLog(JTextPane logTextPane) {
File logLocation = new File(Log.getLogFilePath());
new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
return loadCurrentSessionLogFromFile(logLocation);
}

protected void done() {
try {
logTextPane.setText(get());
} catch (Exception e) {
Log.log(e);
}
OSXIntegration.setProxyIcon(logDialog.getRootPane(), logLocation);
};
}.execute();
}

/**
* Load current session log from log file.
*
* @param logLocation
* log file path to retrieve log messages.
* @return Log messages as String.
*/
protected String loadCurrentSessionLogFromFile(File logLocation) {
final StringBuilder sb = new StringBuilder();
try (Stream<String> lines = Files.lines(logLocation.toPath())) {
lines.forEachOrdered(s -> sb.append(s).append("\n"));
} catch (Exception ignored) {
}
return sb.toString();
}

private void saveAsButtonActionPerformed(ActionEvent evt) {
File outFile = showChooserLogFile();
if (outFile == null) {
return;
}
try {
try (BufferedWriter writer = Files.newBufferedWriter(outFile.toPath())) {
logDialog.logTextPane.write(writer);
}
} catch (IOException ex) {
Log.log(ex);
}
Preferences.setPreference(Preferences.CURRENT_FOLDER, outFile.getParent());
}

private File showChooserLogFile() {
String curDir = Preferences.getPreferenceDefault(Preferences.CURRENT_FOLDER,
System.getProperty("user.home"));
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(curDir, Log.getLogFileName()));
if (chooser.showSaveDialog(logDialog) != JFileChooser.APPROVE_OPTION) {
return null;
}
File outFile = chooser.getSelectedFile();
if (outFile.exists() && !FileCollisionDialog.promptToReplace(logDialog, outFile.getName())) {
return null;
}
return outFile;
}
}
4 changes: 2 additions & 2 deletions src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import org.omegat.gui.dialogs.AboutDialog;
import org.omegat.gui.dialogs.GoToSegmentDialog;
import org.omegat.gui.dialogs.LastChangesDialog;
import org.omegat.gui.dialogs.LogDialog;
import org.omegat.gui.dialogs.LogDialogController;
import org.omegat.gui.dialogs.VersionCheckDialog;
import org.omegat.gui.editor.EditorSettings;
import org.omegat.gui.editor.EditorUtils;
Expand Down Expand Up @@ -887,7 +887,7 @@ public void helpLastChangesMenuItemActionPerformed() {
* Show log
*/
public void helpLogMenuItemActionPerformed() {
new LogDialog(Core.getMainWindow().getApplicationFrame()).setVisible(true);
LogDialogController.show(Core.getMainWindow().getApplicationFrame());
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/org/omegat/logger.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ org.omegat.level = ALL
java.util.logging.ConsoleHandler.level = ALL
org.omegat.util.logging.OmegaTFileHandler.level = ALL

org.omegat.util.logging.OmegaTLogFormatter.mask=$mark: $level: $text $key
org.omegat.util.logging.OmegaTLogFormatter.mask=$time: $level: $text $key
#org.omegat.util.logging.OmegaTLogFormatter.mask=$time: $threadName [$level] $key $text
#org.omegat.util.logging.OmegaTLogFormatter.mask=$mark: $level: $text $key

org.omegat.util.logging.OmegaTLogFormatter.timeFormat=HH:mm:ss,SSSS
org.omegat.util.logging.OmegaTLogFormatter.timeFormat=HH:mm:ss.SSS

java.util.logging.ConsoleHandler.formatter = org.omegat.util.logging.OmegaTLogFormatter

org.omegat.util.logging.OmegaTFileHandler.formatter = org.omegat.util.logging.OmegaTLogFormatter

# maximum log file size, which triggers rotation
org.omegat.util.logging.OmegaTFileHandler.size=1048576
org.omegat.util.logging.OmegaTFileHandler.count=10

# retention policy: maximum 30 files
org.omegat.util.logging.OmegaTFileHandler.count=30
# retention policy; remove older than specified duration.
# 20 days * 24 hours * 60 min * 60 sec = 1,020,000 seconds
org.omegat.util.logging.OmegaTFileHandler.retention=1020000

# precise control of log levels, please change when debug
org.omegat.core.data.level = INFO
Expand Down
Loading

0 comments on commit 4541e8a

Please sign in to comment.