ItsMods
Reading console.log - Printable Version

+- ItsMods (https://www.itsmods.com)
+-- Forum: Archive (https://www.itsmods.com/Forum-Archive.html)
+--- Forum: Archive (https://www.itsmods.com/Forum-Archive--64.html)
+--- Thread: Reading console.log (/Thread-Reading-console-log.html)



Reading console.log - bonemind - 02-03-2012

Hey,
Since i'm on a posting spree anyway, I have another question, although it has to do with plugin development and c#, it's for MW3 and i need help so that's why this is here.

My question:
The console.log contains a lot of information that can be quite useful to be able to read in a plugin, and i know how to read a text file, but i can't seem to find a way to resume reading at the location reading was stopped.
I have spent some time googling and all the results i could find were and implementation that depended on knowing the number of characters that are written in a certain time, and i obviously can't know that for sure. Is there a way to read the logfile, close it, and later open it again and continue reading from the place you last closed it?

I guess you could always read the whole file and discard the data that has already been read, but that seem's quite inefficient
-Bonemind


RE: Reading console.log - crAyon - 02-03-2012

Easy.

When I developed rCrayon - SL I was faced with the same issue.

The best solution was to record the last time stamp of the file, then compare it against the first line being read.. if its newer or > than the last timestamp recorded, proceed.

Also, used linux timestamp as it was much easier to compare than the timestamp that
is the default.

Really simple actually.


RE: Reading console.log - bonemind - 02-03-2012

(02-03-2012, 18:39)deadcrayon Wrote: Easy.

When I developed rCrayon - SL I was faced with the same issue.

The best solution was to record the last time stamp of the file, then compare it against the first line being read.. if its newer or > than the last timestamp recorded, proceed.

Also, used linux timestamp as it was much easier to compare than the timestamp that
is the default.

Really simple actually.

But this would still translate to iterating through the whole file and starting to read at the correct timestamp. Or am i misunderstanding?

-Bonemind


RE: Reading console.log - crAyon - 02-03-2012

Code:
Imports System.Text Imports MySql.Data.MySqlClient Imports System.IO Public Class Form1 Inherits System.Windows.Forms.Form Private Function GetFileSize(ByVal MyFilePath As String) As Long Dim MyFile As New FileInfo(MyFilePath) Dim FileSize As Long = MyFile.Length Return FileSize End Function Private Function Find(ByVal StrSearchString As String) As Boolean Try DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect Dim intcount As Integer = 0 For Each Row As DataGridViewRow In DataGridView1.Rows If DataGridView1.Rows(intcount).Cells(0).Value.ToString = StrSearchString Then DataGridView1.Rows(intcount).Selected = True Find = True End If intcount += 1 Next Row Find = False Catch ex As Exception Debug.WriteLine("HERE IT IS") End Try End Function Private Function GetLogs() Timer2.Enabled = False Dim x As Integer Dim n As Integer n = 0 x = 0 If GetFileSize("C:\rcrayon\games_mp.log") < 50 Then log.Text = "Log Delay..." TASSMySQL.Delay(1) log.Text = "Fetching..." Shell("C:\rcrayon\bin\get_logs.bat", AppWinStyle.Hide, True) Timer2.Enabled = True Exit Function Else Dim fsize As Long fsize = GetFileSize("C:\rcrayon\games_mp.log") log.Text = "Parsing:" & fsize ParseLogs() End If End Function Private Function ParseLogs() Dim p As Integer Dim q As Integer Dim r As Integer Dim db_id As String Dim x As Integer Dim k As Integer Dim d As Integer Dim e As Integer Dim f As Integer Dim LR As String Dim LS As String Dim aSwitch As Array Me.Show() db_id = TASSMySQL.ReadFile("C:\rcrayon\logs\dbid.log") LR = TASSMySQL.readQuery("SELECT COUNT(*) FROM " + db_id) LS = TASSMySQL.readQuery("SELECT ts FROM " + db_id + " WHERE rid=" + LR) ListBox2.Items.Clear() ListBox2.Items.AddRange(System.IO.File.ReadAllLines("C:\rcrayon\games_mp.log")) log.Text = "Loaded: " & ListBox2.Items.Count k = 0 pb.Minimum = 1 pb.Maximum = ListBox2.Items.Count + 1 qBox.Items.Clear() Dim ParsedLine As Array Dim phpUrl As String Dim nospace As String Dim rep As String Dim rep2 As String Dim rep3 As String Dim rep4 As Integer Dim sConv As Integer For x = 0 To ListBox2.Items.Count - 1 Application.DoEvents() ParsedLine = Split(ListBox2.Items.Item(x), ";") nospace = LTrim(ParsedLine(0)) aSwitch = Split(nospace, " ") rep3 = Replace(aSwitch(0), ":", "") rep4 = Replace(LS, ":", "") TASSMySQL.WritelogFile(rep4 & " :: " & rep3) sConv = Val(rep3) If IsNumeric(rep3) = False Then GoTo SKIP If rep3 > rep4 Then Try Select Case aSwitch(1) Case "K" rep = Replace(ParsedLine(4), "'", "~") rep2 = Replace(ParsedLine(8), "'", "~") light.BackColor = Color.LimeGreen k = Val(k + 1) phpUrl = "INSERT IGNORE INTO " + db_id + " (rid, ts, aflag, vguid, vpos, vteam, vname, pguid, ppos, pteam, pname, pweapon, pdamage, ptype, hitmap, time, type) VALUES ('', '" + aSwitch(0) + "', '" + aSwitch(1) + "','" + ParsedLine(1) + "','" + ParsedLine(2) + "','" + ParsedLine(3) + "','" + rep + "', '" + ParsedLine(5) + "', '" + ParsedLine(6) + "', '" + ParsedLine(7) + "', '" + rep2 + "', '" + ParsedLine(9) + "', '" + ParsedLine(10) + "', '" + ParsedLine(11) + "', '" + ParsedLine(12) + "', CURRENT_TIMESTAMP, '1')" qBox.Items.Add(phpUrl) light.BackColor = Color.Black log.Text = "Parsing: Add! (K)" kills.Text = Val(kills.Text + 1) Case "D" rep = Replace(ParsedLine(4), "'", "~") rep2 = Replace(ParsedLine(8), "'", "~") phpUrl = "INSERT IGNORE INTO " + db_id + " (rid, ts, aflag, vguid, vpos, vteam, vname, pguid, ppos, pteam, pname, pweapon, pdamage, ptype, hitmap, time, type) VALUES ('', '" + aSwitch(0) + "', '" + aSwitch(1) + "','" + ParsedLine(1) + "','" + ParsedLine(2) + "','" + ParsedLine(3) + "','" + rep + "', '" + ParsedLine(5) + "', '" + ParsedLine(6) + "', '" + ParsedLine(7) + "', '" + rep2 + "', '" + ParsedLine(9) + "', '" + ParsedLine(10) + "', '" + ParsedLine(11) + "', '" + ParsedLine(12) + "', CURRENT_TIMESTAMP, '2')" qBox.Items.Add(phpUrl) log.Text = "Parsing: Add! (D)" d = damage.Text e = ParsedLine(10) f = Val(d + e) damage.Text = f '///////////// JOIN ///////////// Case "J" phpUrl = "INSERT IGNORE INTO " + db_id + " (rid, ts, aflag, vguid, vpos, vteam, vname, pguid, ppos, pteam, pname, pweapon, pdamage, ptype, hitmap, time, type) VALUES ('', '" + aSwitch(0) + "', '" + aSwitch(1) + "','" + ParsedLine(1) + "','" + ParsedLine(2) + "','" + ParsedLine(3) + "','', '', '', '', '', '', '', '', '', CURRENT_TIMESTAMP, '3')" qBox.Items.Add(phpUrl) My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^0Welcome: ^2" + ParsedLine(3) + " ^0|^2 type !coms for commands'); ?>", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") log.Text = "Parsing: Add! (J)" '/////////////////////////////// Case "VD" phpUrl = "INSERT IGNORE INTO " + db_id + " (rid, ts, aflag, vguid, vpos, vteam, vname, pguid, ppos, pteam, pname, pweapon, pdamage, ptype, hitmap, time, type) VALUES ('', '" + aSwitch(0) + "', '" + aSwitch(1) + "','" + ParsedLine(1) + "','" + ParsedLine(2) + "','" + ParsedLine(3) + "','" + ParsedLine(4) + "', '" + ParsedLine(5) + "', '" + ParsedLine(6) + "', '" + ParsedLine(7) + "', '" + ParsedLine(8) + "', '" + ParsedLine(9) + "', '" + ParsedLine(10) + "', '" + ParsedLine(11) + "', '" + ParsedLine(12) + "', CURRENT_TIMESTAMP, '4')" qBox.Items.Add(phpUrl) log.Text = "Parsing: Add! (VD)" Case "say" phpUrl = "INSERT IGNORE INTO " + db_id + " (rid, ts, aflag, vguid, vpos, vteam, vname, pguid, ppos, pteam, pname, pweapon, pdamage, ptype, hitmap, time, type) VALUES ('', '" + aSwitch(0) + "', '" + aSwitch(1) + "','" + ParsedLine(1) + "','" + ParsedLine(2) + "','" + ParsedLine(3) + "','" + ParsedLine(4) + "', '', '', '', '', '', '', '', '', CURRENT_TIMESTAMP, '5')" qBox.Items.Add(phpUrl) log.Text = "Parsing: Add! (S)" Dim cmnd As Array cmnd = Split(ParsedLine(4), "!") Select Case cmnd(1) Case "rules" My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^2 No foul language ^0|^2 Play fair ^0|^2 Dont Cheat!'); $rcon->rcon('tell " & ParsedLine(2) & " ^2 You will not be warned^0,^2 only kicked^0.'); ?>", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") Case "time" My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^0 Current Time: ^2'.$today.'');", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") Case "coms" My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^2 !rules, !time, !coms, !about, !stats');", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") Case "about" My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^2 rCrayon - SL [rcrayon.com]'); $rcon->rcon('tell " & ParsedLine(2) & " ^2 Version 2.4.11 | Beta');", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") Case "stats" Dim guid As String Dim kills As Integer Dim deaths As Integer Dim ratio As Integer Dim rtotal As Integer guid = ParsedLine(1) kills = TASSMySQL.ReadQuery2("SELECT COUNT(pname) FROM " + db_id + " WHERE type='1' AND pguid='" + guid + "'") deaths = TASSMySQL.ReadQuery2("SELECT COUNT(vname) FROM " + db_id + " WHERE type='1' AND pguid='" + guid + "'") ratio = kills / deaths rtotal = Math.Round(ratio, 1) My.Computer.FileSystem.WriteAllText("C:\rcrayon\php\" & ParsedLine(2) & ".php", "<?php include ('rcon_lib.php'); $server_ip = '" + My.Settings.rcon_ip + "'; $server_port = '" + My.Settings.rcon_port + "'; $server_pass = '" + My.Settings.rcon_pass + "'; $rcon = new BOrcon(); date_default_timezone_set('America/Chicago'); $today = date('H:i:s'); $rcon->connect($server_ip, $server_port, $server_pass); $rcon->rcon('tell " & ParsedLine(2) & " ^0Server Totals: K - ^2 " & kills & " ^0 D - ^2 " & deaths & " ^0 KDR ^2" & rtotal & "^0'); $rcon->rcon('tell " & ParsedLine(2) & " ^0Stats by: ^2rCrayon.com');", False) Shell("C:\rcrayon\bin\php.exe C:\rcrayon\php\" & ParsedLine(2) & ".php", AppWinStyle.Hide, True) My.Computer.FileSystem.DeleteFile("C:\rcrayon\php\" & ParsedLine(2) & ".php") End Select Case Else End Select Catch ex As Exception End Try pb.Increment(+2) x = Val(x + 1) ticker.Text = x End If SKIP: Next x Dim n As Integer pb.Minimum = 1 pb.Maximum = qBox.Items.Count + 1 pb.Value = 1 For n = 0 To qBox.Items.Count - 1 Application.DoEvents() light.BackColor = Color.LightBlue light.BackColor = Color.LimeGreen TASSMySQL.writeQuery(qBox.Items.Item(n)) log.Text = "Insert: " & n & "/" & qBox.Items.Count - 1 pb.Increment(+1) light.BackColor = Color.Black Next qBox.Items.Clear() pb.Value = 1 My.Computer.FileSystem.WriteAllText("C:\rcrayon\games_mp.log", "", False) MySql.Data.MySqlClient.MySqlConnection.ClearAllPools() log.Text = "Parse Done." TASSMySQL.Delay(1) log.Text = "Sleeping: " & Timer4.Interval Timer4.Enabled = True End Function Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim d As Integer = damage.Text Dim k As Integer = kills.Text My.Computer.FileSystem.WriteAllText("C:\rcrayon\logs\counters.log", k & ":" & d, False) End Sub Sub FinishedCountingEventHandler(ByVal Count As Integer) End Sub Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click End End Sub Private Sub ConfigureToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigureToolStripMenuItem1.Click Me.Hide() Form2.Show() End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick Dim counter1 As New Count1() Dim Thread3 As New System.Threading.Thread(AddressOf counter1.Count) Dim Thread4 As New System.Threading.Thread(AddressOf counter1.Count) counter1.CountTo = 5 Thread3.Start() Thread4.Start() log.Text = "Fetching..." GetLogs() End Sub Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick Timer2.Enabled = True Timer4.Enabled = False End Sub Private Sub Timer3_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load LinkLabel1.Visible = False Dim counter1 As New Count1() Dim Thread1 As New System.Threading.Thread(AddressOf counter1.Count) Dim Thread2 As New System.Threading.Thread(AddressOf counter1.Count) Dim OhShit As New System.Threading.Thread(AddressOf counter1.Count) counter1.CountTo = 5 AddHandler counter1.FinishedCounting, AddressOf FinishedCountingEventHandler Dim dataSrc Dim db_key As String Dim csplit As Array Dim counters As String If My.Computer.FileSystem.FileExists("C:\rcrayon\logs\counters.log") = True Then counters = My.Computer.FileSystem.ReadAllText("C:\rcrayon\logs\counters.log") csplit = Split(counters, ":") kills.Text = csplit(0) damage.Text = csplit(1) End If log.Text = "Warming Up!" Me.Text = "rCrayon - SL" If My.Computer.FileSystem.FileExists("C:\rcrayon\1") = True Then TASSMySQL.TestConnection() Thread1.Start() Timer4.Interval = My.Settings.Fetch_Rate_ms db_key = TASSMySQL.ReadFile("C:\rcrayon\logs\dbid.log") dataSrc = TASSMySQL.FillForm("SELECT ts FROM " + db_key) DataGridView1.DataSource = dataSrc log.Text = "Online - Active" Me.Text = "rCrayon - SL (2.4.12) - Online (" + db_key + ")" LinkLabel1.Visible = True Dim spp As Array spp = Split(db_key, "_") LinkLabel1.Text = "http://rcrayon.com/clans/?id=" & spp(1) & "" If My.Computer.FileSystem.FileExists("C:\rcrayon\games_mp.log") = True Then Thread2.Start() Shell("C:\rcrayon\bin\get_logs.bat", AppWinStyle.Hide, True) If GetFileSize("C:\rcrayon\games_mp.log") > 50 Then Me.NotifyIcon1.BalloonTipTitle = "rCrayon - SL Beta" Me.NotifyIcon1.BalloonTipText = "We are fetching your logfile, please stand by..." Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info Me.NotifyIcon1.ShowBalloonTip(3) ' MsgBox("We found a log with entries, we will parse this before" + vbCrLf + "we contiue loading the application. Don't worry we" + vbCrLf + "have started additional threads, it should only take a minute...", vbInformation + vbOKOnly, "rCrayon - Friendly Reminder") Application.DoEvents() OhShit.Start() ListBox2.Items.AddRange(System.IO.File.ReadAllLines("C:\rcrayon\games_mp.log")) db_key = TASSMySQL.ReadFile("C:\rcrayon\logs\dbid.log") dataSrc = TASSMySQL.FillForm("SELECT ts FROM " + db_key) DataGridView1.DataSource = dataSrc DataGridView1.Refresh() ParseLogs() Else Timer2.Enabled = True End If End If End If If My.Computer.FileSystem.FileExists("C:\rcrayon\1") = False Then Form2.Show() End If End Sub Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick If Me.WindowState = FormWindowState.Minimized Then Me.Visible = True End If End Sub Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick End Sub Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize If Me.WindowState = FormWindowState.Minimized Then Me.Visible = False End If End Sub Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged End Sub End Class



RE: Reading console.log - JariZ - 02-03-2012

This might be useful
http://www.itsmods.com/forum/Thread-Release-Read-command-output.html


RE: Reading console.log - crAyon - 02-03-2012

Quote:But this would still translate to iterating through the whole file and starting to read at the correct timestamp. Or am i misunderstanding?

-Bonemind

The file will have to be read if it's not kept in memory. The only other way to do it
is to remember / record line number of file.

However, the logfile would have to only ever be appended to and never modified or you
will lose the correct line numbering.

It's fast to just parse the timestamps out in memory and read them... try it.

You can parse 1000 lines in memory in about 0.01 seconds as these lines aren't very long.

As you can see I was parsing the mp.log and it is much thicker.

I've posted the source to the program here

It was too big to attach to this post. (sorry @d0h, @jariz, @everyone for external link).