4188800aa3a9cbb603d3e51a3e7c8a929e7e80a9
[oweals/gnunet.git] / contrib / log.php
1 <?php
2
3 $path='log';
4 $lines = array();
5 $peers = array();
6 $comps = array();
7 $ajax = FALSE;
8 $colors = array('#F00', '#F80', '#FF0',
9                 '#4F0', '#0A0',
10                 '#22F', '#ADF', '#0FF', '#F0F', '#508', '#FAA',
11                 '#FFF', '#AAA', '#666', '#222');
12
13 function render_row ($d, $component, $pid, $level, $msg, $c)
14 {
15   global $ajax;
16   global $peers;
17   if (!$ajax && $level == "DEBUG")
18     return;
19
20   list($comp,$peer) = explode (',', preg_replace ('/(.*)-(\d*)/', '\1,\2', $component));
21   $peer = array_key_exists ($peer, $peers) ? $peers[$peer] : $peer;
22   $date = $d ? $d->format('Y-m-d'). $d->format('H:i:s') : "";
23   echo "<tr class=\"$level P-$peer C-$comp\" id=\"$c\">";
24   echo "<td class=\"date\"><small>$date</td>";
25   echo '<td class="usec"><small>';
26   echo $d ? $d->format('u') : "";
27   echo '</small></td>';
28   echo "<td class=\"comp\">$comp</td><td class=\"peer\">$peer</td>";
29   echo "<td class=\"level\">$level</td><td><pre>$msg</pre></td>";
30   if ($level != "DEBUG")
31   {
32     echo '<td><div class="btn-group"><button class="btn btn-xs btn-default btn-showup"><span class="glyphicon glyphicon-chevron-up"></span></button>';
33     echo '<button class="btn btn-xs btn-default btn-showdown"><span class="glyphicon glyphicon-chevron-down"></span></button></div></td>';
34   }
35   else
36     echo '<td></td>';
37   echo '</tr>';
38 }
39
40 function render_rows ()
41 {
42   global $lines;
43   foreach ($lines as $line) {
44     render_row ($line[0], $line[1], $line[2], $line[3], $line[4], $line[5]);
45   }
46 }
47
48 function process ($line, $c)
49 {
50   global $lines;
51   global $peers;
52   global $comps;
53   $a = explode (' ', $line);
54   if (count($a) < 6)
55     return;
56   $date = DateTime::createFromFormat ("M d H:i:s-u", implode (' ', array_slice ($a, 0, 3)));
57   $component = $a[3];
58   $level = $a[4];
59   $msg = implode (' ', array_slice ($a, 5));
60
61   if (FALSE !== strpos($line, "STARTING SERVICE")) {
62     $id = preg_replace ("/.*\[(....)\].*\n/", '\1', $line);
63     $pid = preg_replace ("/.*[a-z-]*-([0-9]*).*\n/", '\1', $line);
64     $peers[$pid] = $id;
65   }
66
67   $lines[] = array ($date, $component, 0, $level, $msg, $c);
68   $comp = preg_replace ('/(.*)-\d*/', '\1', $component);
69   $comps[$comp] = 1;
70 }
71
72 if (array_key_exists ('a', $_GET)) {
73   $start = (int)$_GET['a'];
74   $ajax= TRUE;
75 }
76 else
77 {
78   $start = null;
79 }
80 if (array_key_exists ('z', $_GET)) {
81   $stop = (int)$_GET['z'];
82   $ajax= TRUE;
83 }
84 else
85 {
86   $stop = null;
87 }
88 $t0 = microtime(true);
89 $handle = @fopen($path, 'r');
90 if ($handle) {
91     $c = 0;
92     while (($line = fgets($handle)) !== false) {
93         if (!$start || $c >= $start) {
94           process ($line, $c);
95         }
96         $c++;
97         if ($stop && $c > $stop)
98           break;
99     }
100 } else {
101    echo "<div class=\"alert alert-danger\">Error opening file $path.</div>";
102 }
103
104 $t1 = microtime(true);
105 /* Ajax request: don't render container HTML, just table rows. */
106 if ($start !== null || $stop !== null) {
107   render_rows();
108   die();
109 }
110 // echo $t1-$t0;
111
112 ?>
113 <!DOCTYPE html>
114 <html lang="en">
115 <head>
116   <meta charset="utf-8">
117   <meta http-equiv="X-UA-Compatible" content="IE=edge">
118   <meta name="viewport" content="width=device-width, initial-scale=1">
119
120   <title>GNUnet log view</title>
121
122   <!-- Latest compiled and minified Bootstrap CSS -->
123   <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
124   <!-- Optional theme -->
125   <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">
126
127   <style>
128     body {
129       font-family: arial,sans-serif;
130     }
131     table {
132       color:#000;
133       margin-top: 40px;
134       font-size:12px;
135       border-collapse:collapse;
136     }
137     pre {
138       padding: 0px;
139       margin: 0px;
140       border: 0px;
141       background-color: transparent;
142     }
143     .alert {
144       display: none;
145       position: fixed;
146       width: 75%;
147       left: 50%;
148       margin: 5% 0 0 -37.5%;
149     }
150     .btn-toolbar {
151       position: fixed;
152       top: 0px;
153     }
154     .btn-xs {
155       font-size: 9px;
156       padding: 0 5px;
157     }
158     .level {
159       display: none;
160     }
161     .DEBUG {
162       background-color:#CCC;
163     }
164     .WARNING {
165       background-color:#EB9316;
166     }
167     .ERROR {
168       background-color:#D2322D;
169     }
170     .btn-group {
171       min-width: 48px;
172     }
173     table.table tbody tr td,
174     table.table tbody th td {
175       padding: 0px 0px 0px 2px;
176       margin-bottom: 0px;
177     }
178 <?php
179     $c = 0;
180     foreach ($peers as $peer) {
181       echo "table.table tbody tr.P-$peer td.peer {\n";
182       echo '  background-color: ' . $colors[$c] . ";\n";
183       echo "}\n";
184       echo "#P-$peer { color: " . $colors[$c++] . "}\n";
185     } ?>
186   </style>
187 </head>
188
189
190 <body>
191 <div class="btn-toolbar" role="toolbar">
192   <div class="btn-group">
193     <button id="ERROR" class="btn btn-danger btn-showlevel"><span class="glyphicon glyphicon-fire"></span> Error</button>
194     <button id="WARNING" class="btn btn-warning btn-showlevel"><span class="glyphicon glyphicon-exclamation-sign"></span> Warning</button>
195     <button id="INFO" class="btn btn-default btn-showlevel active"><span class="glyphicon glyphicon glyphicon-info-sign"></span> Info</button>
196     <button id="DEBUG" class="btn btn-primary btn-showlevel"><span class="glyphicon glyphicon glyphicon-wrench"></span> Debug</button>
197   </div>
198   <div class="btn-group">
199     <?php foreach($peers as $pid=>$id): ?>
200     <button id="P-<?php echo $id ?>" class="btn btn-default btn-element btn-showpeer active"><?php echo $id ?></button>
201     <?php endforeach ?>
202     <button class="btn btn-default btn-showall">All</button>
203     <button class="btn btn-default btn-shownone">None</button>
204   </div>
205   <div class="btn-group">
206     <?php foreach($comps as $c=>$one): ?>
207     <button id="C-<?php echo $c ?>" class="btn btn-default btn-element btn-showcomp active"><?php echo $c ?></button>
208     <?php endforeach ?>
209     <button class="btn btn-default btn-showall">All</button>
210     <button class="btn btn-default btn-shownone">None</button>
211   </div>
212 </div>
213 <div id="msg" class="alert alert-success"></div>
214 <table class="table">
215   <thead>
216   <tr>
217     <th>Date Time</th>
218     <th>uSec</th>
219     <th>Comp</th>
220     <th>Peer</th>
221     <th class="level">Level</th>
222     <th>Message</th>
223     <th></th>
224   </tr>
225   </thead>
226   <tbody>
227 <?php render_rows(); ?>
228   </tbody>default
229 </table>
230 <p>Processed in <?php echo $t1-$t0; ?> seconds.</p>
231 <p>Rendered in <?php echo microtime(true)-$t1; ?> seconds.</p>
232   <!-- jQuery -->
233   <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
234   <!-- Latest compiled and minified Bootstrap JavaScript -->
235   <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
236
237   <script>
238
239     var types = ["ERROR", "WARNING", "INFO", "DEBUG"];
240     var peers = {<?php foreach($peers as $pid=>$id) echo "'$pid': '$id', "; ?>};
241     var msg_timeout;
242
243     function msg (content)
244     {
245       $("#msg").html(content);
246       $("#msg").stop(true);
247       $("#msg").fadeTo(100, 1).fadeTo(3000, 0.90).fadeOut(1000);
248     }
249
250     function showlevel (level)
251     {
252       $("tbody > tr").hide();
253       $(".btn-showlevel").removeClass("active");
254       $("#"+level).addClass("active");
255       for (var index = 0; index < types.length; ++index) {
256         $(".btn-showpeer.active").each(function(){
257           $("."+types[index]+"."+this.id).show();
258         });
259         if (types[index] == level)
260           return;
261       }
262     }
263
264     function shownone(btn)
265     {
266       $(btn).parents(".btn-group").children(".btn-element.active").each(function(){$(this).click()});
267     }
268
269     function showall(btn)
270     {
271       $(btn).parents(".btn-group").children(".btn-element:not(.active)").each(function(){$(this).click()});
272     }
273
274     function showpeer (peer)
275     {
276       $("#"+peer).toggleClass("active");
277       if ($("#"+peer).hasClass("active")) {
278         for (var index = 0; index < types.length; ++index) {
279           var className = "." + types[index] + "." + peer;
280           $(className).show();
281           if ($("#"+types[index]).hasClass("active"))
282             return;
283         }
284       } else {
285         $("."+peer).hide();
286       }
287     }
288     
289     function showcomp (comp)
290     {
291       $("#"+comp).toggleClass("active");
292       if ($("#"+comp).hasClass("active")) {
293         for (var index = 0; index < types.length; ++index) {
294           var className = "." + types[index] + "." + comp;
295           $(className).show();
296           if ($("#"+types[index]).hasClass("active"))
297             return;
298         }
299       } else {
300         $("."+comp).hide();
301       }
302     }
303
304     function load_debug (btn, up)
305     {
306       var tr = $(btn).parents("tr");
307       var level;
308       var pos = parseInt(tr.attr("id"));
309       var first = pos + 1;
310       var last = pos - 1;
311       for (var index = 0; index < types.length; ++index) {
312         if (tr.hasClass(types[index]))
313         {
314           level = types[index];
315           break;
316         }
317       }
318       if (up) {
319         if (parseInt(tr.prev().attr("id")) == last) {
320           msg ("Already loaded");
321           return;
322         }
323         first = parseInt(tr.prevAll("."+level).first().attr("id")) + 1;
324         first = isNaN(first) ? 0 : first;
325       } else {
326         if (parseInt(tr.next().attr("id")) == first) {
327           msg ("Already loaded");
328           return;
329         }
330         last = parseInt(tr.nextAll("."+level).first().attr("id")) - 1;
331       }
332       if (first > last)
333         return;
334       $.ajax({
335         url: document.location,
336         data: { a: first, z: last }
337       }).done(function ( resp ) {
338         var loc = $("#"+(first-1));
339         var trs = $(resp);
340         for (var peer in peers) {
341           console.log (peer + "=>" + peers[peer]);
342           trs.filter(".P-"+peer).removeClass('P-'+peer).addClass('P-'+peers[peer]).find("td.peer").html(peers[peer]);
343         }
344         console.log (trs);
345         if (loc.length > 0)
346           loc.after(trs);
347         else {
348           $("#"+(last+1)).before(trs);
349         }
350         msg("Done loading " + (last-first+1) + " lines.");
351       });
352       //tr.nextUntil("."+tr.attr("class")).show();
353
354     }
355
356     function hide (btn)
357     {
358       var tr = $(btn).parents("tr");
359       tr.nextUntil("."+tr.attr("class")).hide();
360     }
361
362     $(function() {
363       $(".btn-showup").on ("click", function(){ load_debug(this, true) });
364       $(".btn-showdown").on ("click", function(){ load_debug(this, false) });
365       $(".btn-showlevel").on ("click", function(){ showlevel(this.id) });
366       $(".btn-showpeer").on ("click", function(){ showpeer(this.id) });
367       $(".btn-showcomp").on ("click", function(){ showcomp(this.id) });
368       $(".btn-showall").on ("click", function(){ showall(this) });
369       $(".btn-shownone").on ("click", function(){ shownone(this) });
370     });
371   </script>
372 </body>
373 </html>