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