Author Topic: Is it possible to empty the Python plugin log (console) or perhaps disable it?  (Read 3099 times)

0 Members and 1 Guest are viewing this topic.

marcusvdt

  • Sr. Member
  • ****
  • Posts: 152
  • Karma: 6
  • Researching
    • View Profile
I've some python scripts running on my VC that are called from startup events and stay there and these scripts generate some output using the print function, which normally prints text to Python's console and in this case it prints te text to somewhere else (a variable perhaps?) which is then displayed in the log field of the Python plugin (txtConsole).
Well, since my py script is currently generating lots of output, it seems this log area is becoming weighty for VC and the overall VC performance is significantly decreased after some time of operation.

I let it running yesterday before I went to bed, and this morning I had to clear that log area (by using the clear log button) at least 5 times sequentially. I pressed the button once, then the log area immediately became full again, and then I pressed the clear log button again. Did that 5 times until the log area finally became empty and then the VC returned to normal performance.

So it would be great if the oldest log data could be dynamically discarded before it affects the performance. Of course, in the future I'll also disable many of these prints in the code because they are there for debugging purposes only. But anyway, I guess this slow down in performance could be avoided if I can manage to clear that log either by code or automatically.

Is it possible?

Btw, I love that log area! It is a must have for debugging python scripts.




Thanks

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Yes, I will add this feature.  A function to clear the log at the very least, but probably also something automatic like what we have in the history window.

But in the mean time there are other solutions.

1 - As you say this is only for debugging and it is a good idea to disable this when your script is working.  One easy solution is to create a debug function, that accepts a string and then prints it.  If you use this you can then disable the log by commenting out one line in your function (the one that prints).

Example: (with log printing disabled)
Code: [Select]
def myLog(logLine):
    #print logLine
    pass
   
myLog("test the log")

2 - Same idea as above, but instead of using print, you can save the log to a file.  This has many advantages.  One big one is that even if your python script crashes VC you can still look in this text file log.  If you use notepad++ to view the file, it will offer to automatically reload the file when it detects changes to the file.

Example: (with file log enabled)
Code: [Select]
log_file = open("PY/myLog.txt", "w")

def myLog(logLine):
    global log_file
    log_file.write(logLine+'\n')   
    #pass
   
myLog("test the log.")
myLog("test the log again.")

3 - Instead of using print you can use the action VcAdvanced.Log to log to the History Window and VC log.  This is handy if you want to see what your debug info is doing in relation to other activities in VC.

marcusvdt

  • Sr. Member
  • ****
  • Posts: 152
  • Karma: 6
  • Researching
    • View Profile
Thanks for the ideas, yes I was too lazy to convert all my prints to calls to a function, but I'll probably do that.
I was going the easy way replacing print with #print haha! But then I'll think I'll do it more professional like you said.

My concern regarding that log file is that it really decreases VC performance significantly after a while. Of course this situation is less likely to happen with anyone else... But having it clearing itself automatically is the way to go to keep VC stable...

Thanks!




marcusvdt

  • Sr. Member
  • ****
  • Posts: 152
  • Karma: 6
  • Researching
    • View Profile
I ended up doing a regex find/replace in Notepad++ to replace the prints with calls to this function below.

Here is the code and function:
Code: [Select]
logFile = open("PY\\MySensorsDEBUG.txt", "wt")
logFile.close()
global willdebug
willdebug=True
def debuglog(txt=""):
    global willdebug
    if willdebug:
        try:
            with open("PY\\MySensorsDEBUG.txt", "a") as debuglogfile:
                nowtimestamp=datetime.datetime.now()
                nowtimestamp=str(nowtimestamp)
                txt=str(txt)
                debuglogfile.write(nowtimestamp+'\t'+txt+'\n')
        except:
            print "Critical error!"+'\n'+traceback.format_exc()
            with open("PY\\MySensorsDEBUG.txt", "a") as debuglogfile:
                debuglogfile.write("Critical error!"+'\n'+traceback.format_exc()+'\n')
            pass

And here is how to use it:
Code: [Select]
debuglog("write one line to the log with current timestamp")
And here is how to turn logging on/off without having to restart VC.
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.5.2-->
<command id="679" name="Toggle MySensors debugging log" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=willdebug</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;True</ifParams>
    <then>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>debuglog("Log toggled False")</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>willdebug=not willdebug</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>willdebug=not willdebug</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>debuglog("Log toggled True")</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=willdebug</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>My Sensors Debug mode now: {LastResult}</param>
      <param>3000</param>
      <param>-1</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Toggle MySensors debugging log</phrase>
</command>

Here one example of how a line of the log file looks like:
Code: [Select]
2015-07-17 13:06:24.815000 Normal Set Message received (and I'm not waiting for a response): 4;1;1;0;16;0
« Last Edit: July 22, 2015, 11:57:58 AM by marcusvdt »