Module org.autogui

Class GuiLogManager

java.lang.Object
org.autogui.base.log.GuiLogManager
Direct Known Subclasses:
GuiLogManagerConsole, GuiSwingLogManager

public class GuiLogManager extends Object
the base class for log managers
     GuiLogManager.get().log("hello");

     GuiLogManager.get().logFormat("%d", 123);

     try { ... } catch (Exception ex) { GuiLogManager.get().logError(ex); }

     try (GuiLogEntryProgress p = GuiLogManager.get().logProgress()) { //see logProgress()
         ...
     }
 
  • Field Details

  • Constructor Details

    • GuiLogManager

      public GuiLogManager()
  • Method Details

    • get

      public static GuiLogManager get()
      Returns:
      a shared instance
    • setManager

      public static void setManager(GuiLogManager manager)
    • log

      public GuiLogEntry log(Object... args)
      create and report a log entry. args are concatenated with a space and pass to logString(String). if the previous arg is a String and it ends with an ASCII symbol char, it will not insert a space.
              log("hello", "world") => logString("hello world")
              log("hello:", "world") => logString("hello:world")
              log("x=", 1, "y=", 2, "ex", new Exception())
                 => logString("x=1 y=2 ex") and logError(new Exception())
          
      when the last item of args is an exception, it will be passed to logError(Throwable). if the args is only a single exception, it will returns its GuiLogEntryException, otherwise GuiLogEntryString
      Parameters:
      args - concatenated values for creating a log entry.
      Returns:
      the created log entry
    • isSymbol

      public boolean isSymbol(char c)
    • logFormat

      public GuiLogEntryString logFormat(String format, Object... args)
      create and report a log entry. use String.format(String, Object...) and pass to logString(String)
      Parameters:
      format - the format string
      args - the arguments for the format
      Returns:
      the created log entry
    • replaceConsole

      public void replaceConsole(boolean replaceError, boolean replaceOutput)
      replace System.err and System.out with a string redirecting to this manager. System.err will be completely replaced with the manager, because the manager usually outputs an entry to the original System.err. System.out will be replaced with a manager stream with original System.out. those replaced streams are instances of GuiLogManager.LogPrintStream.

      synchronized with System.class

      Parameters:
      replaceError - if true, it will replace with a new GuiLogManager.LogPrintStream.
      replaceOutput - if true, it will replace with a new GuiLogManager.LogPrintStream.
    • resetErr

      public void resetErr()
      synchronized with System.class
      Since:
      1.1
    • resetOut

      public void resetOut()
      synchronized with System.class
      Since:
      1.1
    • replaceUncaughtHandler

      public void replaceUncaughtHandler()
      replace the default uncaught handler with GuiLogManager.LogUncaughtHandler by calling Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler). this makes the uncaught exception will be sent to the manager.

      synchronized with System.class

    • resetUncaughtHandler

      public void resetUncaughtHandler()
      synchronized with System.class
      Since:
      1.1
    • getSystemErr

      public PrintStream getSystemErr()
      Returns:
      obtains the original System.err from wrapped System.err
    • getErr

      public PrintStream getErr()
    • logString

      public GuiLogEntryString logString(String str)
      create a string entry and show it. call logString(String, boolean) with fromStdout=false
      Parameters:
      str - the string for the entry
      Returns:
      the created log entry
    • logString

      public GuiLogEntryString logString(String str, boolean fromStandard)
      create a string entry and show it.
      Parameters:
      str - the string for the entry
      fromStandard - whether the string come from the standard output (or error) redirection
      Returns:
      the created log entry
      Since:
      1.1
    • formatTime

      public String formatTime(Instant i)
      Parameters:
      i - the formatted date-time
      Returns:
      [YYYY-mm-dd HH:MM:SS.LLL] with local date-time
    • logError

      public GuiLogEntryException logError(Throwable ex)
      create an exception entry and show it
      Parameters:
      ex - the exception
      Returns:
      the created entry
    • logProgress

      public GuiLogEntryProgress logProgress()
      create an active progress entry and show it. the returned progress is determinate (isIndeterminate() == false), and notifies progress changes to the manager by updateProgress(GuiLogEntryProgress)
           try (GuiLogEntryProgress p = manager.logProgress();) {
               for (...) {
                   //those methods will cause a runtime-exception if the thread is interrupted.
                   p.addValueP(0.01f)
                     .setMessage("running ...");
                   ...
                   //the running code can checks interruption
                   if (Thread.interrupted()) {
                       break;
                   }
                   ...
               }
           } catch (Exception ex) { ... }
       
      Returns:
      the created progress entry
    • logProgress

      public GuiLogEntryProgress logProgress(int max)
      Parameters:
      max - a maximum value
      Returns:
      an active progress entry with the maximum value, and show it.
    • updateProgress

      public void updateProgress(GuiLogEntryProgress p)
    • show

      public void show(GuiLogEntry entry)
      update display of the entry.
      Parameters:
      entry - the displayed entry
    • formatMessageLine

      public String formatMessageLine(String msg)
      Parameters:
      msg - the message to be formatted
      Returns:
      the formatted line: a tab and a newline are converted to an space. the length is limited to 70 chars
    • formatDuration

      public String formatDuration(Instant from, Instant to)
      Parameters:
      from - the start time
      to - the end time
      Returns:
      [[[[[[<years>y] <months>mo] <days>d] <hours>h] <minutes>m] <seconds>s] (<millis>ms|<nanos>ns) : years, months, and days are calculated by local date