summaryrefslogtreecommitdiff
path: root/contrib/log.php
blob: 6af3e57d4a4c543122131c2536ec4525d6799826 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php

$path='log';
$lines = array();
$ajax = FALSE;

function render_row ($d, $component, $pid, $level, $msg, $c)
{
  global $ajax;
  if (!$ajax && $level == "DEBUG")
    return;
  $date = $d ? $d->format('Y-m-d'). '<br />' . $d->format('H:i:s') : "";
  echo "<tr class=\"$level\" id=\"$c\">";
  echo "<td class=\"date\"><small>$date</small></td>";
  echo '<td class="usec">';
  echo $d ? $d->format('u') : "";
  echo '</td>';
  echo "<td class=\"comp\">$component</td><td class=\"level\">$level</td><td>$msg&nbsp;</td>";
  if ($level != "DEBUG")
  {
    echo '<td><button class="btn btn-xs btn-default btn-showup"><span class="glyphicon glyphicon-chevron-up"></span></button>';
    echo '<button class="btn btn-xs btn-default btn-showdown"><span class="glyphicon glyphicon-chevron-down"></span></button></td></tr>';
  }
  else
    echo '<td></td></tr>';
}

function render_rows ()
{
  global $lines;
  foreach ($lines as $line) {
    render_row ($line[0], $line[1], $line[2], $line[3], $line[4], $line[5]);
  }
}

function process ($line, $c)
{
  global $lines;
  $a = explode (' ', $line);
  if (count($a) < 6)
    return;
  $date = DateTime::createFromFormat ("M d H:i:s-u", implode (' ', array_slice ($a, 0, 3)));
  $component = $a[3];
  $level = $a[4];
  $msg = implode (' ', array_slice ($a, 5));
  
  $lines[] = array ($date, $component, 0, $level, $msg, $c);
}

if (array_key_exists ('a', $_GET)) {
  $start = (int)$_GET['a'];
  $ajax= TRUE;
}
else
{
  $start = null;
}
if (array_key_exists ('z', $_GET)) {
  $stop = (int)$_GET['z'];
  $ajax= TRUE;
}
else
{
  $stop = null;
}
$t0 = microtime(true);
$handle = @fopen($path, 'r');
if ($handle) {
    $c = 0;
    while (($line = fgets($handle)) !== false) {
	if ((!$start || $c >= $start) && (!$stop || $c <= $stop)) {
	  process ($line, $c);
	}
	$c++;
    }
} else {
   echo "<div class=\"alert alert-danger\">Error opening file $path.</div>";
}

$t1 = microtime(true);
if ($start !== null || $stop !== null) {
  render_rows();
  die();
}
// echo $t1-$t0;

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <title>GNUnet log view</title>

  <!-- Latest compiled and minified Bootstrap CSS -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
  <!-- Optional theme -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">

  <style>
    body {
      font-family: arial,sans-serif;
      color:#444;
    }
    table {
      font-size:12px;
      border-collapse:collapse;
    }
    .alert {
      display: none;
      position: fixed;
      width: 75%;
      left: 50%;
      margin: 0 0 0 -37.5%;
    }
    .level {
      display: none;
    }
    .DEBUG {
      background-color:#CCC;
    }
    .WARNING {
      background-color:#EB9316;
    }
    .ERROR {
      background-color:#D2322D;
    }

  </style>
</head>


<body>
<div class="btn-toolbar" role="toolbar">
  <div class="btn-group">
    <button class="btn btn-danger btn-showerror"><span class="glyphicon glyphicon-fire"></span> Error</button>
    <button class="btn btn-warning btn-showwarn"><span class="glyphicon glyphicon-exclamation-sign"></span> Warning</button>
    <button class="btn btn-info btn-showinfo"><span class="glyphicon glyphicon glyphicon-info-sign"></span> Info</button>
    <button class="btn btn-default btn-showdebug"><span class="glyphicon glyphicon glyphicon-wrench"></span> Debug</button>
</div>
</div>
<div id="msg" class="alert alert-success"></div>
<table class="table">
  <thead>
  <tr>
    <th>Date Time</th>
    <th>uSec</th>
    <th>Comp</th>
    <th class="level">Level</th>
    <th>Message</th>
    <th></th>
  </tr>
  </thead>
  <tbody>
<?php render_rows(); ?>
  </tbody>
</table>
  <!-- jQuery -->
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  <!-- Latest compiled and minified Bootstrap JavaScript -->
  <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>

  <script>

    var types = ["ERROR", "WARNING", "INFO", "DEBUG"];
    var msg_timeout;

    function msg (content)
    {
      $("#msg").html(content);
      $("#msg").stop(true);
      $("#msg").fadeTo(100, 1).fadeTo(3000, 0.90).fadeOut(1000);
    }

    function showlevel (level)
    {
      $("tr").hide();
      for (var index = 0; index < types.length; ++index) {
	$("."+types[index]).show();
	if (types[index] == level)
	  return;
      }
    }

    function show (btn, up)
    {
      var tr = $(btn).parents("tr");
      var level = tr.attr("class");
      var pos = parseInt(tr.attr("id"));
      var first = pos + 1;
      var last = pos - 1;
      if (up) {
	if (parseInt(tr.prev().attr("id")) == last) {
	  msg ("Already loaded");
	  return;
	}
	first = parseInt(tr.prevAll("."+level).first().attr("id")) + 1;
	first = isNaN(first) ? 0 : first;
      } else {
	if (parseInt(tr.next().attr("id")) == first) {
	  msg ("Already loaded");
	  return;
	}
	last = parseInt(tr.nextAll("."+level).first().attr("id")) - 1;
      }
      if (first > last)
	return;
      $.ajax({
	url: document.location,
	data: { a: first, z: last }
      }).done(function ( resp ) {
	var loc = $("#"+(first-1));
	if (loc.length > 0)
	  loc.after(resp);
	else {
	  $("#"+(last+1)).before(resp);
	}
	msg("Done loading " + (last-first+1) + " lines.");
      });
      //tr.nextUntil("."+tr.attr("class")).show();
      
    }

    function hide (btn)
    {
      var tr = $(btn).parents("tr");
      tr.nextUntil("."+tr.attr("class")).hide();
    }

    $(function() {
      $(".btn-showup").on ("click", function(){ show(this, true) });
      $(".btn-showdown").on ("click", function(){ show(this, false) });
      $(".btn-showerror").on ("click", function(){ showlevel("ERROR") });
      $(".btn-showwarn").on ("click", function(){ showlevel("WARNING") });
      $(".btn-showinfo").on ("click", function(){ showlevel("INFO") });
      $(".btn-showdebug").on ("click", function(){ showlevel("DEBUG") });
    });
  </script>
</body>
</html>