webmcp
view framework/env/trace/_render_sub_tree.lua @ 0:9fdfb27f8e67
Version 1.0.0
| author | jbe/bsw | 
|---|---|
| date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) | 
| parents | |
| children | 0b7e87f2dc91 | 
 line source
     1 local function open(class)
     2   slot.put('<li class="trace_' .. class .. '">')
     3 end
     4 local function open_head()
     5   slot.put('<div class="trace_head">')
     6 end
     7 local function close_head()
     8   slot.put('</div>')
     9 end
    10 local function close()
    11   slot.put('</li>')
    12 end
    13 function trace._render_sub_tree(node)
    14   local function render_children(tail)
    15     if #node > 0 then
    16       slot.put('<ul class="trace_list">')
    17       for idx, child in ipairs(node) do
    18         trace._render_sub_tree(child)
    19       end
    20       if tail then
    21         slot.put(tail)
    22       end
    23       slot.put("</ul>")
    24     end
    25   end
    26   local function close_with_children()
    27     close_head()
    28     render_children()
    29     close()
    30   end
    31   local node_type = node.type
    32   if node_type == "root" then
    33     render_children()
    34   elseif node_type == "debug" then
    35     open("debug")
    36     slot.put(encode.html(node.message))
    37     close()
    38   elseif node_type == "request" then
    39     open("request")
    40     open_head()
    41     slot.put("REQUESTED")
    42     if node.view then
    43       slot.put(" VIEW")
    44     elseif node.action then
    45       slot.put(" ACTION")
    46     end
    47     slot.put(
    48       ": ",
    49       encode.html(node.module),
    50       "/",
    51       encode.html(node.view or node.action)
    52     )
    53     close_with_children()
    54   elseif node_type == "config" then
    55     open("config")
    56     open_head()
    57     slot.put('Configuration "', encode.html(node.name), '"')
    58     close_with_children()
    59   elseif node_type == "filter" then
    60     open("filter")
    61     open_head()
    62     slot.put(encode.html(node.path))
    63     close_with_children()
    64   elseif node_type == "view" then
    65     open("view")
    66     open_head()
    67     slot.put(
    68       "EXECUTE VIEW: ",
    69       encode.html(node.module),
    70       "/",
    71       encode.html(node.view)
    72     )
    73     close_with_children()
    74   elseif node_type == "action" then
    75     if
    76       node.status and (
    77         node.status == "ok" or
    78         string.find(node.status, "^ok_")
    79       )
    80     then
    81       open("action_success")
    82     elseif
    83       node.status and (
    84         node.status == "error" or
    85         string.find(node.status, "^error_")
    86       )
    87     then
    88       open("action_softfail")
    89     else
    90       open("action_neutral")
    91     end
    92     open_head()
    93     slot.put(
    94       "EXECUTE ACTION: ",
    95       encode.html(node.module),
    96       "/",
    97       encode.html(node.action)
    98     )
    99     close_head()
   100     if node.status == "softfail" then
   101       render_children(
   102         '<li class="trace_action_status">Status code: "' ..
   103         encode.html(node.failure_code) ..
   104         '"</li>'
   105       )
   106     else
   107       render_children()
   108     end
   109     close()
   110   elseif node_type == "redirect" then
   111     open("redirect")
   112     open_head()
   113     slot.put("303 REDIRECT TO VIEW: ", encode.html(node.module), "/", encode.html(node.view))
   114     close_with_children()
   115   elseif node_type == "forward" then
   116     open("forward")
   117     open_head()
   118     slot.put("INTERNAL FORWARD TO VIEW: ", encode.html(node.module), "/", encode.html(node.view))
   119     close_with_children()
   120   elseif node_type == "exectime" then
   121     open("exectime")
   122     open_head()
   123     slot.put(
   124       "Finished after " ..
   125       string.format("%.1f", os.monotonic_hires_time() * 1000) ..
   126       ' ms (' ..
   127       string.format("%.1f", os.clock() * 1000) ..
   128       ' ms CPU)'
   129     )
   130     close_with_children()
   131   elseif node_type == "sql" then
   132     open("sql")
   133     if node.error_position then
   134       -- error position starts counting with 1
   135       local part1 = string.sub(node.command, 1, node.error_position - 1)
   136       --local part2 = string.sub(node.command, node.error_position - 1, node.error_position + 1)
   137       local part2 = string.sub(node.command, node.error_position)
   138       slot.put(encode.html(part1))
   139       slot.put('<span class="trace_error_position">⇒</span>')
   140       slot.put(encode.html(part2))
   141       --slot.put('</span>')
   142       --slot.put(encode.html(part3))
   143     else
   144       slot.put(encode.html(node.command))
   145     end
   146     close();
   147   elseif node_type == "error" then
   148     open("error")
   149     open_head()
   150     slot.put("UNEXPECTED ERROR")
   151     close_head()
   152     close()
   153   end
   154 end
