Kodi Community Forum

Full Version: XBMC Log Viewer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to write a simple addon to show the contents of the log file in a textbox control on screen.

I looked into the XBMC log uploader addon to see how does it get the path and how it reads the xbmc.log file.
I managed to create the right skin but I cannot read the log file contents, I think because the file is opened by XBMC and in use all the time.
Just to check this theory I changed from xbmc.log to xbmc.old.log and the file shows OK.

The code from XBMC uploader that opens and reads the file is just:

Code:
def upload_file(self, filepath):
        url = 'http://xbmclogs.com/'
        self.__log('reading log...')
        file_content = open(filepath, 'r').read()
        self.__log('starting upload "%s"...' % filepath)
        post_dict = {'paste_data': file_content,
                     'api_submit': True,
                     'mode': 'xml'}
        if filepath.endswith('.log'):
            post_dict['paste_lang'] = 'xbmc'
        elif filepath.endswith('.xml'):
            post_dict['paste_lang'] = 'advancedsettings'
        post_data = urllib.urlencode(post_dict)
        req = urllib2.Request(url, post_data)
        . . .

And I use the exact same code to read the file in my addon:

Code:
file_content = open(filepath, 'r').read()

How comes I get no content but the uploader addon does succeed in sending the log data?
The log file is readable, I know this because in Windows (Win7) you can open the log file while XBMC is running and you get to see all log messages, you can even refresh it after some event and you get the extra lines too.
Well, I managed to display the xbmc.log.
The trick was to copy it to a temp file and display that file instead (I use special://temp)

Some technical problems still prevent me from finishing it, any help will be appreciated:

1. How to prevent opening the addon a second time if it is already showing, or more precisely, if the user wants to open it again, just bring him to the opened one and refresh the contents

2. How to manage the scrollbar. Mainly I succeeded to show the file and have move up/down page/up down home/end. But I have the problem that to implement "end" I use textBox.scroll(-1), because I did not find how to get the exact number of lines the textBox is displaying (I tried counting newlines, but the content is wrapped, depending on the window width, thus increasing the number o lines to unknown).

3. I would like to dynamically calculate the number of lines displayed on one screen, so page up/down will use the number to scroll one page up/down. Currently I just put a setting to define the number of lines scrolled on page up/down.

4. I would like to improve it by having a side list to choose from xbmc.log xbmc.old.log and any crash files present, but I am not sure how to implement the list in python (I think I know how to modify the skin, but not where do i go from there)
I got it. I think, I will use the onClick callback to know which item in the list is selected and display the selected log file.
1. using sys.arg to check if it's already running
Martijn, you mean to check sys,argv[1]? Is this a session ID, a handler?

Can you elaborate, or point to an example, on how to use sys.argv to know the script is running already?
I've seen also somone uses the window properties to set a property named "myscriptisrunning", but I did not find how it is used to prevent opening a second instance of he script.

If I find the script is running already, how do I get the running instance to do something?
Here are two example

https://github.com/XBMC-Addons/service.s...ult.py#L62
Note: may require some more code than listed below
PHP Code:
import xbmgui

self
.WINDOW xbmcgui.Window(20000)  # 20000 is fictional window id
if not self.WINDOW.getProperty('SkinWidgets_Running') == 'true':
    
# set running state to true
    
self.WINDOW.setProperty('SkinWidgets_Running''true')
    
# execute script below
    
some python code
    
# clear again when finished 
self.WINDOW.clearProperty('SkinWidgets_Running')
else:
print 'already started so do nothing'
[/php]


Other example:
https://github.com/XBMC-Addons/service.x...ice.py#L49
This checks the sys.arg however it is not guaranteed that sys.arg[0] is empty (iirc)
Best is to make some print/log statement to try it out
Well, I tried those approaches, and I must be doing something wrong.
Because each time I launch the script a window is created. Right, the second time and on the window is empty, but nevertheless I have to 'exit' all the windows if I want to clear the screen.

This is the code I use:

Code:
def onInit( self ):
        self.win = xbmcgui.Window(10000)
        if( not self.win.getProperty(RUNNING) ):
            self.win.setProperty( RUNNING, 'true')
            self.reset_controls()
            self.textBox = self.getControl( TEXT_CONTROL )
            self.logViewer = LogViewer()
            self.list = self.initList();
            self.list.selectItem(0)
            self.setFocus(self.list)
            self.show_control( TEXT_CONTROL )
        self.win.getFocus()

According to the docs, Window(id) should not create a window if a window with the same ID already exists