Forum Discussion

vex's avatar
vex
Contributor
14 years ago

Log.Attributes question

Is there a way to define attribute colors/etc throughout the whole test?  It seems they are not global for me in my tests and I either have to continually define them on a per-function basis, or I need to write my own log function that will set these every time.



How can I make these global so I don't have to continually set them?

4 Replies

  • vex's avatar
    vex
    Contributor
    Ignore this, just figured it out.  I can do it through the GeneralEvents handler :)
  • Hi Vince,



    Yes, to have color and font style attributes defined in one place, you can set them from within the OnLog* event handlers, like this:

    function GeneralEvents_OnLogError(Sender, LogParams)

    {

      LogParams.Color = clRed;

      LogParams.FontStyle = 1; // bold

    }
    (Tip: You can reuse the same event handler for events that have the same parameters, e.g. OnLogMesage, OnLogEvent, OnLogCreateNode etc.)



    We also have a feature request to make it possible to globally set up default log style attributes, and I've increased its rating based on your post.
  • vex's avatar
    vex
    Contributor
    Thanks Helen.  I'd also like to a very specific attr setting overwrite the ones in the event handler.



    For example, I have colors/styles setup in the event handler for Log.Event, but if I do a





    Set attrBlock = Log.CreateNewAttributes

      attrBlock.Bold = true

      attrBlock.FontColor = vbWhite

      attrBlock.BackColor = Builtin.clNavy

    Log.Event("End Test: Test ABC", "", pmNormal, attrBlock)





    The attrBlock style won't apply - it will still apply the one from the event handler.  I get why that is, but it would be nice to have a forced style applied to a log line take precedence over the general event handler.
  • Hi Vince,



    This is actually possible. In the event handler, you can check the current attributes and only override them if they have default values. The default value for font color is clWindowText, background color - clWindow, font style - 0 (normal). This way, log entries posted without the attributes parameter will be styled as specified by the event handler, and those with custom attributes will have these "forced" attributes. Here's an example:

    Sub GeneralEvents_OnLogEvent(Sender, LogParams)

      With LogParams

        If .FontColor = clWindowText Then .FontColor = clGray

        If .Color     = clWindow     Then .Color     = clCream

        If .FontStyle = 0            Then .FontStyle = 2 ' italic

      End With

    End Sub



    Sub Test

      Log.Event "Gray italic text on a cream background"



      Set attrBlock = Log.CreateNewAttributes

      attrBlock.Bold = true

      attrBlock.FontColor = vbWhite

      attrBlock.BackColor = Builtin.clNavy

      Log.Event "White bold text on a blue background", "", , attrBlock

    End Sub