File: lua_trace.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (88 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
--
-- (C) 2014-15 - ntop.org
--

-- Trace Level
TRACE_LEVEL = 2


-- Login & session
debug_login = false
debug_session = false
debug_host = false
debug_flow_data = false

-------------------------------- Trace Event ----------------------------------
-- Trace level
TRACE_ERROR    = 0
TRACE_WARNING  = 1
TRACE_NORMAL   = 2
TRACE_INFO     = 3
TRACE_DEBUG    = 4

MAX_TRACE_LEVEL = 4
-- Trace mode
TRACE_CONSOLE = 0
TRACE_WEB = 1

function traceError(p_trace_level, p_trace_mode,p_message)
  local currentline = debug.getinfo(2).currentline
  local what =  debug.getinfo(2).what
  local src =  debug.getinfo(2).short_src
  local traceback = debug.traceback()

  for str in (string.gmatch(traceback, '([^\n]+)')) do
    traceback = str
  end
  for str in (string.gmatch(traceback, '([^/]+)')) do
    traceback = str
  end
  local i = 0
  for str in (string.gmatch(traceback, '([^:][^ ]+)')) do
    if (i == 0) then traceback = str end
    i = i + 1
  end
  traceback = traceback:sub(1, string.len(traceback)-1)
  local filename = src
  for str in (string.gmatch(src, '([^/]+)')) do
    filename = str
  end

  if ((p_trace_level <= MAX_TRACE_LEVEL) and (p_trace_level <= TRACE_LEVEL) )then
    if (p_trace_mode == TRACE_WEB) then
      local date = os.date("%d/%b/%Y %X")
      local trace_prefix = ''

      if (p_trace_level == TRACE_ERROR) then trace_prefix = 'ERROR: ' end
      if (p_trace_level == TRACE_WARNING) then trace_prefix = 'WARNING: ' end
      if (p_trace_level == TRACE_INFO) then trace_prefix = 'INFO: ' end
      if (p_trace_level == TRACE_DEBUG) then trace_prefix = 'DEBUG: ' end

      if (filename..':'..currentline ~= traceback) then
        print('<b>'..date..' ['..traceback..'] ['..filename..':'..currentline..'] ' ..trace_prefix..p_message..'</b></br>')
      else
        print('<b>'..date..' ['..filename..':'..currentline..'] ' ..trace_prefix..p_message..'</b></br>')
      end
    elseif (p_trace_mode == TRACE_CONSOLE) then
      if (filename..':'..currentline ~= traceback) then
        --~ io.write(date..' ['..traceback..'] ['..filename..':'..currentline..'] ' ..trace_prefix..p_message..'\n')
        ntop.traceEvent(p_trace_level, traceback..'] [' .. filename, currentline, p_message)
      else
        --~ io.write(date..' ['..filename..':'..currentline..'] ' ..trace_prefix..p_message..'\n')
        ntop.traceEvent(p_trace_level, filename, currentline, p_message)
      end
    end
  end
end

function setTraceLevel(p_trace_level) 
  if (p_trace_level <= MAX_TRACE_LEVEL) then
    TRACE_LEVEL = p_trace_level
  end
end

function resetTraceLevel()
  TRACE_LEVEL = 1
end

--------------------------------