Class Log


  • public class Log
    extends java.lang.Object
    Provides basic log functionality.

    The Log class owns a number of LogWriters in a static map, which can be accessed
    using a string identifier. All logs are global.

    By default the Log class contains a Debug, Info, Warning and Error log. These can be accessed
    statically as:
    Log::debugLog() << "This is an debug message"; Log::infoLog() << "This is an info message"; Log::warnLog() << "This is an error message"; Log::errorLog() << "This is an error message";
    or on the log instance
    Log &log = Log::log(); log.debug() << "This is an debug message"; log.info() << "This is an info message"; log.warn() << "This is an error message"; log.error() << "This is an error message";
    or using one one the RW_LOG, RW_LOGLINE or RW_LOG2 macros, e.g.
    RW_LOG_INFO("The value of x is "<<x); RW_LOG_DEBUG("The value of x is "<<x); RW_LOG_ERROR(Log::infoId(), "The value of x is "<<x);

    You can control what logs are active both using a loglevel and by using a log mask.
    The loglevel enables all logs with LogIndex lower or equal to the loglevel. As default
    loglevel is LogIndex::info which means debug and all user logs are disabled. However,
    logs can be individually enabled using log masks which will override loglevel setting.

    Notice that logmasks cannot disable logs that are below or equal to loglevel.

    change loglevel:
    Log::log().setLevel(Log::Debug);
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Log.LogIndex
      Indices for different logs.
      static class  Log.LogIndexMask
      loglevel mask
    • Constructor Summary

      Constructors 
      Constructor Description
      Log()
      constructor
      Log​(long cPtr, boolean cMemoryOwn)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      LogWriter debug()
      convenience function for getting the LogWriter
      that is associated with the debug loglevel
      static LogWriter debugLog()
      convenience function for getting the LogWriter
      that is associated with the debug loglevel
      void decreaseTabLevel()
      Decrease the indentation.
      void delete()  
      LogWriter error()
      convenience function for getting the LogWriter
      that is associated with the error loglevel
      static LogWriter errorLog()
      convenience function for getting the LogWriter
      that is associated with the error loglevel
      void flush​(Log.LogIndex id)
      Calls flush on the specified log

      If the id cannot be found an exception is thrown

      void flushAll()
      Calls flush on all logs
      LogWriter get​(Log.LogIndex id)
      Returns the LogWriter that is associated with LogIndex id

      If the id is unknown an exception is thrown.

      static long getCPtr​(Log obj)  
      static LogPtr getInstance()
      returns the global log instance.
      int getLogIndexMask()
      get the current log mask
      LogWriter getLogWriter​(Log.LogIndex id)
      Returns the LogWriter that is associated with LogIndex id

      If the id is unknown an exception is thrown.

      LogWriterPtr getWriter​(Log.LogIndex id)
      gets the log writer associated to logindex id
      void increaseTabLevel()
      Make indentation to make logs easier to read.
      LogWriter info()
      convenience function for getting the LogWriter
      that is associated with the info loglevel
      static LogWriter infoLog()
      convenience function for getting the LogWriter
      that is associated with the info loglevel
      boolean isEnabled​(Log.LogIndex idx)
      Checks if the given LogIndex is enabled.
      static Log log()
      convenience function of getInstance
      void remove​(Log.LogIndex id)
      Removes a log

      If the id cannot be found an exception is thrown

      void removeAll()
      Removes all log writers
      void setDisable​(int mask)
      Disable log(s) given by log mask.
      void setEnable​(int mask)
      Enable log(s) given by log mask.
      void setLevel​(Log.LogIndex loglevel)
      set the loglevel.
      static void setLog​(LogPtr log)
      sets the instance of the log class
      void setLogIndexMask​(int mask)
      the loglevel is a runtime handle for enabling/disabling
      logging to specific loglevels.
      void setWriter​(Log.LogIndex id, LogWriterPtr writer)
      Associates a LogWriter with the LogIndex id.

      SetWriter can either be used to redefine an existing log or to create a new
      custom output.

      Example:
      Log::SetWriter(Log::User1, new LogStreamWriter(std::cout)); RW_LOG(Log::User1, "Message send to User log 1");

      void setWriterForMask​(int mask, LogWriterPtr writer)
      Associates a LogWriter with the logs specified with mask.

      SetWriter can either be used to redefine an existing log or to create a new
      custom output.

      Example:
      log.setWriterForMask(Log::InfoMask | Log::DebugMask, new LogStreamWriter(std::cout)); RW_LOG(Log::Info, "Message send to User log 1");

      static Log.LogIndexMask toMask​(Log.LogIndex idx)
      Convert a LogIndex to a mask.
      LogWriter warning()
      convenience function for getting the LogWriter
      that is associated with the warning loglevel
      static LogWriter warningLog()
      convenience function for getting the LogWriter
      that is associated with the warning loglevel
      void write​(Log.LogIndex id, java.lang.String message)
      Writes message to the log

      If the id cannot be found an exception is thrown

      void write​(Log.LogIndex id, Message message)
      Writes message to the logwriter associated with LogIndex id

      If the id cannot be found an exception is thrown


      void writeln​(Log.LogIndex id, java.lang.String message)
      Writes message followed by a '\n' to the log

      If the id cannot be found an exception is thrown

      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Log

        public Log​(long cPtr,
                   boolean cMemoryOwn)
      • Log

        public Log()
        constructor
    • Method Detail

      • getCPtr

        public static long getCPtr​(Log obj)
      • delete

        public void delete()
      • toMask

        public static Log.LogIndexMask toMask​(Log.LogIndex idx)
        Convert a LogIndex to a mask.
        Parameters:
        idx - [in] the LogIndex.
        Returns:
        the mask enabling the given log level.
      • infoLog

        public static LogWriter infoLog()
        convenience function for getting the LogWriter
        that is associated with the info loglevel
        Returns:
        info LogWriter
      • warningLog

        public static LogWriter warningLog()
        convenience function for getting the LogWriter
        that is associated with the warning loglevel
        Returns:
        warning LogWriter
      • errorLog

        public static LogWriter errorLog()
        convenience function for getting the LogWriter
        that is associated with the error loglevel
        Returns:
        error LogWriter
      • debugLog

        public static LogWriter debugLog()
        convenience function for getting the LogWriter
        that is associated with the debug loglevel
        Returns:
        debug LogWriter
      • getInstance

        public static LogPtr getInstance()
        returns the global log instance. Global in the sence
        of whatever is linked staticly together.
        Returns:
        a Log
      • log

        public static Log log()
        convenience function of getInstance
        Returns:
        a Log
      • setLog

        public static void setLog​(LogPtr log)
        sets the instance of the log class
        Parameters:
        log - [in] the log that will be used through the static log methods.
      • setLevel

        public void setLevel​(Log.LogIndex loglevel)
        set the loglevel. Any log with LogIndex equal to or less than
        loglevel will be enabled. Any log above will be disabled unless an
        enabled mask is specified for that log
        Parameters:
        loglevel - [in] the level
      • getWriter

        public LogWriterPtr getWriter​(Log.LogIndex id)
        gets the log writer associated to logindex id
        Parameters:
        id - [in] logindex
        Returns:
        log writer
      • setWriter

        public void setWriter​(Log.LogIndex id,
                              LogWriterPtr writer)
        Associates a LogWriter with the LogIndex id.

        SetWriter can either be used to redefine an existing log or to create a new
        custom output.

        Example:
        Log::SetWriter(Log::User1, new LogStreamWriter(std::cout)); RW_LOG(Log::User1, "Message send to User log 1");

        Parameters:
        id - [in] the LogIndex that the logwriter is associated with.
        writer - [in] LogWriter object to use
      • setWriterForMask

        public void setWriterForMask​(int mask,
                                     LogWriterPtr writer)
        Associates a LogWriter with the logs specified with mask.

        SetWriter can either be used to redefine an existing log or to create a new
        custom output.

        Example:
        log.setWriterForMask(Log::InfoMask | Log::DebugMask, new LogStreamWriter(std::cout)); RW_LOG(Log::Info, "Message send to User log 1");

        Parameters:
        mask - [in] the LogIndexMask that the logwriter is associated with.
        writer - [in] LogWriter object to use
      • get

        public LogWriter get​(Log.LogIndex id)
        Returns the LogWriter that is associated with LogIndex id

        If the id is unknown an exception is thrown.

        Parameters:
        id - [in] loglevel
        Returns:
        Reference to LogWriter object
      • write

        public void write​(Log.LogIndex id,
                          java.lang.String message)
        Writes message to the log

        If the id cannot be found an exception is thrown

        Parameters:
        id - [in] Log identifier
        message - [in] String message to write
      • write

        public void write​(Log.LogIndex id,
                          Message message)
        Writes message to the logwriter associated with LogIndex id

        If the id cannot be found an exception is thrown


        Parameters:
        id - [in] Log identifier
        message - [in] Message to write
      • writeln

        public void writeln​(Log.LogIndex id,
                            java.lang.String message)
        Writes message followed by a '\n' to the log

        If the id cannot be found an exception is thrown

        Parameters:
        id - [in] Log identifier
        message - [in] Message to write
      • flush

        public void flush​(Log.LogIndex id)
        Calls flush on the specified log

        If the id cannot be found an exception is thrown

        Parameters:
        id - [in] loglevel
      • flushAll

        public void flushAll()
        Calls flush on all logs
      • remove

        public void remove​(Log.LogIndex id)
        Removes a log

        If the id cannot be found an exception is thrown

        Parameters:
        id - [in] Log identifier
      • removeAll

        public void removeAll()
        Removes all log writers
      • increaseTabLevel

        public void increaseTabLevel()
        Make indentation to make logs easier to read.
      • decreaseTabLevel

        public void decreaseTabLevel()
        Decrease the indentation.
      • info

        public LogWriter info()
        convenience function for getting the LogWriter
        that is associated with the info loglevel
        Returns:
        info LogWriter
      • warning

        public LogWriter warning()
        convenience function for getting the LogWriter
        that is associated with the warning loglevel
        Returns:
        info LogWriter
      • error

        public LogWriter error()
        convenience function for getting the LogWriter
        that is associated with the error loglevel
        Returns:
        info LogWriter
      • debug

        public LogWriter debug()
        convenience function for getting the LogWriter
        that is associated with the debug loglevel
        Returns:
        info LogWriter
      • setLogIndexMask

        public void setLogIndexMask​(int mask)
        the loglevel is a runtime handle for enabling/disabling
        logging to specific loglevels.
        Parameters:
        mask -

        Note: DEPRECATED. Use setEnable/setDisable instead
      • getLogIndexMask

        public int getLogIndexMask()
        get the current log mask
        Returns:
        the LogIndex
        Note: DEPRECATED. To be removed
      • setEnable

        public void setEnable​(int mask)
        Enable log(s) given by log mask.
        Parameters:
        mask - [in] the mask for the logs to enable.
      • isEnabled

        public boolean isEnabled​(Log.LogIndex idx)
        Checks if the given LogIndex is enabled. This can be used to
        determine if a certain log level will be displayed or not.
        Parameters:
        idx - [in] the level
      • setDisable

        public void setDisable​(int mask)
        Disable log(s) given by log mask.
        Parameters:
        mask - [in] the mask for the logs to disable.
      • getLogWriter

        public LogWriter getLogWriter​(Log.LogIndex id)
        Returns the LogWriter that is associated with LogIndex id

        If the id is unknown an exception is thrown.

        Parameters:
        id - [in] loglevel
        Returns:
        Reference to LogWriter object