View unanswered posts | View active topics It is currently 23 Aug 2017, 13:59



This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Scripting tutorial part 4 
Author Message
Site Admin

Joined: 28 Jun 2014, 12:29
Posts: 25
In this post I will discuss how .NET components can be utilized in lua scripts that are run by TailExpert. The lua interpreter of TailExpert includes luainterface which makes it possible to use .NET components in your scripts. The example below shows you how you can open a custom tab in TailExpert and plot a histogram in it showing occurrence data of keywords in the TailExpert logfile that indicated the usage count of a number of functions of TailExpert.

Note: To test this script you need to enable debug logging in tailexpert, please look here http://tailexpert.neware.nl/support/viewtopic.php?f=5&t=3339&sid=b5e159e5e4f095fc27933f54791cf0b5 how to.

In the script we first need to import the necessary .NET libraries so we can use them:
Code:
-- Import Windows Assemblies
import("System")
import("System.Windows.Forms")
import("System.Drawing")
import("System.Drawing.Drawing2D")
import("ZedGraph")

Next we need to declare the graph we want to show and set the properties:
Code:
local graph = ZedGraphControl()
graph.Dock = DockStyle.Fill
myPane = graph.GraphPane

-- Set the titles and axis labels
myPane.Title.Text = "Bar Graph Example"
myPane.XAxis.Title.Text = "Categories"
myPane.YAxis.Title.Text = "Nr of Hits"

local label = String[5]
local dbl_arr = Double[5]

for i=0, 4,1 do
    dbl_arr[i] = 0.0
end

label[0] = "Pause"
label[1] = "Scroll"
label[2] = "Filter"
label[3] = "Compare"
label[4] = "Bookmarks"

-- Create the three BarItems, change the fill properties so the angle is at 90
-- degrees for horizontal bars
bar = myPane:AddBar( "Function usage", nil, dbl_arr, Color.Red )
bar.Bar.Fill = Fill( Color.Red, Color.White, Color.Red, 0 )

-- Set BarBase to the YAxis for horizontal bars
myPane.BarSettings.Base = BarBase.X
-- Make the bars stack instead of cluster
myPane.BarSettings.Type = BarType.Stack

-- Fill the axis background with a color gradient
myPane.Chart.Fill = Fill( Color.White, Color.LightGoldenrodYellow, 45.0 )
myPane.XAxis.Scale.TextLabels = label
myPane.XAxis.Type = AxisType.Text
graph:AxisChange()


Now that we have defined our graph we can show it in a tab when the script is run by adding this code to the OnEnter() function of our script:
Code:
    local tab = OpenCustomTab("OpenCustomTab2", false);
    tab.Controls:Add(graph)


To fill our graph we need to get data from the logfile, we do this by opening the file in the OnEnter() function of our script and read the file in our OnEvaluate() function, which will be called repeatedly by a background thread. In the ReadFile() function the logfile is read and the data retrieved is inspected on the occurrence of the keywords:
ButtonPause_Click, ButtonScroll_Click, ButtonFilter_Click, compareTabsToolStripMenuItem_Click, NextBookmark_Click, NextBookmark_Click. Whenever these words occur in the received message a corresponding counter in dbl_arr is incremented. This dbl_arr is attached to the histogram by the previously shown line of code:
Code:
bar = myPane:AddBar( "Function usage", nil, [b]dbl_arr[/b], Color.Red )


The zedGraph library requires you to create a new pointpair list on every update so a new pointpairlist is created using the new dbl_arr data by:
Code:
-- Create new pointPairList
pointPairList = PointPairList()
pointPairList:Add(nil, dbl_arr)
curveItem = myPane.CurveList[0]
curveItem.Points = pointPairList


To update the graph on the screen we need to update the scale and the graph by:
Code:
graph:AxisChange()
graph:Refresh()


This is just one example how you can utilize the built-in zedGraph library. Please consult the zedgraph documentation for more in-depth information on the usage of this library.

Below is the complete source code of the script described in this post.

Code:
--[[
    This is an example script for the lua engine in TailExpert
   
    Purpose:
    This script will read a file named TailExpert.log from the temp directory
    and submit it to TailExpert, the script will search for messages that indicate
   the usage of internal functions. To get a visible bargraph the
   TailExpert debuglogging should be enabled to get the logfile filled with
   the required debug messages.
]]--
require("Config")

-- Import Windows Assemblies
import("System")
import("System.Windows.Forms")
import("System.Drawing")
import("System.Drawing.Drawing2D")
import("ZedGraph")
local graph = ZedGraphControl()
graph.Dock = DockStyle.Fill
myPane = graph.GraphPane

-- Set the titles and axis labels
myPane.Title.Text = "Bar Graph Example"
myPane.XAxis.Title.Text = "Categories"
myPane.YAxis.Title.Text = "Nr of Hits"

local label = String[5]
local dbl_arr = Double[5]

for i=0, 4,1 do
    dbl_arr[i] = 0.0
end

label[0] = "Pause"
label[1] = "Scroll"
label[2] = "Filter"
label[3] = "Compare"
label[4] = "Bookmarks"

-- Create the three BarItems, change the fill properties so the angle is at 90
-- degrees for horizontal bars
bar = myPane:AddBar( "Function usage", nil, dbl_arr, Color.Red )
bar.Bar.Fill = Fill( Color.Red, Color.White, Color.Red, 0 )

-- Set BarBase to the YAxis for horizontal bars
myPane.BarSettings.Base = BarBase.X
-- Make the bars stack instead of cluster
myPane.BarSettings.Type = BarType.Stack

-- Fill the axis background with a color gradient
myPane.Chart.Fill = Fill( Color.White, Color.LightGoldenrodYellow, 45.0 )
myPane.XAxis.Scale.TextLabels = label
myPane.XAxis.Type = AxisType.Text
graph:AxisChange()

local filename = 'c:\\temp\\TailExpert.log'
local file = nill

local linenr = 0
function ReadFile(f)
   -- Do not put message here as it will make a recurring effect when you have log enabled and load TailExpert.log
   outputBuffer = {}
   if ScriptInitialized == true then
      if feof(f) ~= nill then
         while feof(f) ~= true do
            local line = f:read("*line")
            if line ~= nill then
               PutLogLine( line )
                   
               if string.match(line, "ButtonPause_Click") ~= nil then
                        dbl_arr[0] = dbl_arr[0] + 1.0
               end
               if string.match(line, "ButtonScroll_Click") ~= nil then
                        dbl_arr[1] = dbl_arr[1] + 1.0
               end
               if string.match(line, "ButtonFilter_Click") ~= nil then
                        dbl_arr[2] = dbl_arr[2] + 1.0
               end
               if string.match(line, "compareTabsToolStripMenuItem_Click") ~= nil then
                        dbl_arr[3] = dbl_arr[3] + 1.0
               end
               if string.match(line, "NextBookmark_Click") ~= nil then
                        dbl_arr[4] = dbl_arr[4] + 1.0
               end
               if string.match(line, "PrevBookmark_Click") ~= nil then
                        dbl_arr[4] = dbl_arr[4] + 1.0
               end
            end
                -- Create new pointPairList
                pointPairList = PointPairList()
                pointPairList:Add(nil, dbl_arr)
                curveItem = myPane.CurveList[0]
                curveItem.Points = pointPairList
                graph:AxisChange()
                graph:Refresh()
         end
      end
   end
end

--[[
Required predefined functions: Are called from TailExpert, adapt to your needs
]]--

function OnEvaluate(loglines)
    -- Do not put LogMessage() here this will overflow your logs as
    -- Evaluate is called every tail cycle
    -- If you need log make sure it's not output every cycle
    if ScriptInitialized == true then
        ReadFile(file)
    end
end

-- Initialize this script
function OnEnter()
   LogMessage(INFO, "OpenCustomTab2.lua script OnEnter()")
    -- tests the functions above
   ScriptInitialized = true

    -- tests the functions above
    if file_exists(filename) then
        file = assert(io.open(filename, "r"))
    end
    file:seek("set", 0)
    local tab = OpenCustomTab("OpenCustomTab2", false);
    tab.Controls:Add(graph)
end

-- Cleanup
function OnExit()
   LogMessage(INFO, "OpenCustomTab2.lua script OnExit()")
    graph:Dispose()
   file:close()
   CloseTab("OpenCustomTab2")
      ScriptInitialized = false
end

_________________
Kind Regards,

TailExpert support team


16 Apr 2015, 14:25
Profile
Display posts from previous:  Sort by  
This topic is locked, you cannot edit posts or make further replies.   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.