tags:

views:

612

answers:

6

Hi all,
Here's the use case: A user clicks on a link that opens a window displaying the contents of a text log. Easy enough. But is there a way of using POST, to open that text log to a certain location (i.e., search for a particular timestamp given in the post, and show the file at that specific location)?

(Assume I can't put html tags inside the text log -- it's a raw file).

Template of log:

+++ 2009/06/19 10:47:12.264 ACTION +++
+++ 2009/06/19 10:49:12.111 ACTION +++

So I want the page to load a specific timestamp.

Thanks,
Michael

A: 

If you could put some tags around the file's text, then you could probably insert some javascript that would scroll the window after loading it.

cube
+2  A: 

Since you can't modify the file, the only way would be to wrap it in a <frame> or an <iframe> and drive the searching and scrolling from JavaScript in the neighbouring/containing page.

Here's an example, which you can try out online at http://entrian.com/so-container.html

<html><head><script>
function go() {
    // "line" is the <input> for which line to jump to; see the HTML.
    var line = document.getElementById('line').value;
    if (document.body.createTextRange) {  // This is IE
        var range = frames['log'].document.body.createTextRange();
        if (range.findText(line)) {
            range.select(); // Scroll the match into view and highlight it.
        }
    } else {  // Non-IE.  Tested in Firefox; YMMV on other browsers.
        frames['log'].find(line); // Scroll the match into view and highlight it.
    }
}
</script></head><body>
<input type='text' size='5' name='line' id='line' value='10'>
<button onclick='go()'>Go</button><br>
<iframe name='log' width='100' height='50' src='so-data.txt'>
<!-- so-data.txt contains the numbers 01-20 on separate lines -->
</body></html>

I've tested that in IE7 and FF3; I'd be surprised if it worked elsewhere without edits, but you never know your luck!

Obviously in your case you'd be driving the scrolling programmatically rather than via an <input> box, but you can see how it would work for you.

RichieHindle
Yes, but there would be no accurate way to scroll to a specific area in a text file. Different users may have different fonts or font sizes, etc etc, and it would scroll to a different location for each.
musicfreak
Michael said "search for a particular timestamp given in the post" - the JavaScript can do that search, measure the distance to the top of the document, and scroll to that location. I've done exactly this before.
RichieHindle
Can you provide me some details?
Michael
Yes, I know it can do that with *HTML*, but can you make those measurements in a raw text file?
musicfreak
How do you make the measurements with HTML?
Michael
@musicfreak, @Michael: In fact there's no need to measure anything - you can simply find the matching entry and get the browser to do the measure-and-scroll for you. See my updated answer.
RichieHindle
I think this is exactly what I need. Thank's!
Michael
A: 

Yes, but passing your parameters via a querystring would be a whole lot simpler.

To scroll to a certain position in the text file you will need to user javascript (overly complicated in my opinion) or add an html anchor tag.

If you were planning to post the raw text log in a window, you will also run into some difficulty as HTML will not recognize the newlines and run the log together into one blob.

Chris Pebble
It seems like my log file does not appear as a blob when displayed in html.Basically, I want to emulate this behavior: If you search for a word that occurs at the bottom of a webpage, your browser jumps down to that instance. What's the way to do it with javascript?
Michael
@Michael: My updated answer shows how to do exactly that.
RichieHindle
A: 

have you tried

window.open ('log.txt');
window.scrollTo (0, window.scrollMaxY);

? From mozilla reference : https://developer.mozilla.org/en/DOM/window.scrollMaxY

Vlagged
See my comment on @RichieHindle's answer; there is no accurate way to scroll to a specific point in a text file because different users may have different fonts, font sizes, etc.
musicfreak
+3  A: 

Why can't you just have a php or perl or simlar script that processes the log file on the spot, and sticks in html anchors and calls it a day?

Doing on the spot processing would also allow you display a trimmed down copy of the log thats only relevant to the timespan around the event in question.

whatsisname
It's a large log file, and there would be a lot of anchors (each timestamp would have to be an anchor)
Michael
If you want to only go to a specific spot, just throw an anchor in for that specific one. Or only have anchors once per minute/hour in the timestamps, etc.
whatsisname
The issue with trimming down the file is there is no definitive start and end point.I'm looking for a specific point in the log, then checking the actions before and after it to find a sequence of actions.
Michael
I understand that, but 'trimming down' is proportional to the duration of the log file vs the duration of the event. As an example, if you have a log that is switched out once per day, and you are looking for an event that only lasts a few minutes, if you show what happened an hour before and after will probably do the job 95% of the time. For those other rare cases, then just bite the bullet and download the whole file and do a find for the timestamp in question.
whatsisname
A: 

Keep a 'living copy' of the log file that has been translated to HTML - every time the original file is modified (or simply every X seconds), check for and append the newest lines with HTML anchors applied to the HTML version.

Peter Boughton