Monday 13 July 2015

Infolog levels. Setprefix and other tricks.

Sometimes we have to display some kind of treeview in info message.
We need to make somehow sub-level of infolog.

To do that we can use a setPrefix() build-in function.

As an example:

setPrefix ('A new node of infolog');
info('A sub node of infolog');

Instead of using setPrefix(), we can just use a tab '\t' to format our message

info(strFmt('A new node of infolog%1A sub node of infolog', '\t'));

Result in both cases will be the same

To have a possibility to go back to level 1, we have to use another methods, sub methods or loop statements.

So, before calling our method or loop we have an infolog level X, and after this method or statement we will have exactly the same level X of our infolog.

Please have a look in that code below. This will produce few levels of our message, and sometimes it will get back to previous level or dig deeper.

I'll use sub method in job and for loop to show how does it work.


static void Job201(Args _args)
{
    Counter i, k;
    
    void sendMessage(str _message, str _prefix = '')
    {
        if (_prefix)
        {
            setPrefix(_prefix);    
        }
        
        info(_message);
    }
    
    
    //A prefix on the very top of infolog window
    setPrefix('Build a tree');
    
    //Two lines on first level of infolog. 
    //Just to show that two of them will be on the same level
    info('Just a message on first level');
    info('Another message on the same level');    
    
    //Sub method of that job used to display sub node of 
    //that infolog and display a message 
    //under that subnode
    sendMessage('This is message on sub level 2', 'Sub level 1 prefix');
    
    //When trace went out from submethod 'sendMessage' 
    //infolog level is again on level one!!
    //Be careful. In next line there will be 
    //a little trick to display another message 
    //on sub level 2from 'sendMessage' submethod. 
    //so this line above will be displayed after 
    //this all sublevel 2. 
    //Infolog will change an order of those lines
    info('This should be a message on level 1. Where it has been displayed? :)');    
    
    //You can use a tab '\t' instead of setPrefix().
    //When you'll place exactly the same string 
    //as a prefix as it has been previously used
    //an Infolog will put not create new node, 
    //but will just put a message to previous prefix node 
    //with the same name
    info(strfmt('Sub level 1 prefix%1This is another message on sub level 2', '\t'));
    
    //and again, another sublevel 2 node, 
    //but different prefix
    //so it will be displayed as separate subnode.
    info(strfmt('Another sub level 1 prefix%1This is another message on sub level 2', '\t'));    
    
    //and third level of infolog. 
    //Just used a tab '\t' to dig there.
    info(strfmt('Another sub level 1 prefix%1This is prefix message on sub level 2%1and message on sublevel 3!', '\t', '\t'));      
    
    //and we are getting back to level 1.
    info('This should be a message on level 1');
    
    //Let's dig one level deeper.    
    setPrefix('Well, level 2 before loop');
    
    //we can use a loop instead of subMethod.
    //It will do the same for us, just close 
    //a prefix level in one statement
    //and outside of that statement infolog level will 
    //go back to level before loop.    
    
    for (i = 1;i <= 2;i++)
    {
        //next level of prefix. This will be level 3   
        setPrefix('Level 3 inside of loop');
        
        for(k = 1; k <= 2; k++)
        {
            info('Message on level 4');    
        }
    }
    
    //and after a loop, Infolog will display 
    //this message on level 3
    //because before a loop there was a setPrefix statement
    info('This should on level 3');
}

This is how it will look like after that



2 comments:

  1. All this talk about prefixes is nice, but how do you UN-set a prefix? How do you cancel it and make it go away?

    ReplyDelete
    Replies
    1. Well, haven't tried. You can copy this infolog to container, using c = infolog.cut(). After that you can clear infolog ( infolog.clear() ) and then recreate this infolog from container c without this prefix which you don't want. This is the first and only what I'm thinking how to do this what you want.

      Delete