- reduce button size
[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>$msg&nbsp;</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       font-family: courier,sans-serif;
129       color:#000;
130       margin-top: 40px;
131       font-size:12px;
132       border-collapse:collapse;
133     }
134     .alert {
135       display: none;
136       position: fixed;
137       width: 75%;
138       left: 50%;
139       margin: 5% 0 0 -37.5%;
140     }
141     .btn-toolbar {
142       position: fixed;
143       top: 0px;
144     }
145     .btn-xs {
146       font-size: 9px;
147       padding: 0 5px;
148     }
149     .level {
150       display: none;
151     }
152     .DEBUG {
153       background-color:#CCC;
154     }
155     .WARNING {
156       background-color:#EB9316;
157     }
158     .ERROR {
159       background-color:#D2322D;
160     }
161     .btn-group {
162       min-width: 48px;
163     }
164     table.table tbody tr td,
165     table.table tbody th td {
166       padding: 0px 0px 0px 2px;
167       margin-bottom: 0px;
168     }
169 <?php
170     $c = 0;
171     foreach ($peers as $peer) {
172       echo "table.table tbody tr.P-$peer td.peer {\n";
173       echo '  background-color: ' . $colors[$c] . ";\n";
174       echo "}\n";
175       echo "#P-$peer { color: " . $colors[$c++] . "}\n";
176     } ?>
177   </style>
178 </head>
179
180
181 <body>
182 <div class="btn-toolbar" role="toolbar">
183   <div class="btn-group">
184     <button id="ERROR" class="btn btn-danger btn-showlevel"><span class="glyphicon glyphicon-fire"></span> Error</button>
185     <button id="WARNING" class="btn btn-warning btn-showlevel"><span class="glyphicon glyphicon-exclamation-sign"></span> Warning</button>
186     <button id="INFO" class="btn btn-info btn-showlevel active"><span class="glyphicon glyphicon glyphicon-info-sign"></span> Info</button>
187     <button id="DEBUG" class="btn btn-default btn-showlevel"><span class="glyphicon glyphicon glyphicon-wrench"></span> Debug</button>
188   </div>
189   <div class="btn-group">
190     <?php foreach($peers as $pid=>$id): ?>
191     <button id="P-<?php echo $id ?>" class="btn btn-default btn-showpeer active"><?php echo $id ?></button>
192     <?php endforeach ?>
193     <button id="btn-showall" class="btn btn-default">All</button>
194     <button id="btn-shownone" class="btn btn-default">None</button>
195   </div>
196 </div>
197 <div id="msg" class="alert alert-success"></div>
198 <table class="table">
199   <thead>
200   <tr>
201     <th>Date Time</th>
202     <th>uSec</th>
203     <th>Comp</th>
204     <th>Peer</th>
205     <th class="level">Level</th>
206     <th>Message</th>
207     <th></th>
208   </tr>
209   </thead>
210   <tbody>
211 <?php render_rows(); ?>
212   </tbody>
213 </table>
214   <!-- jQuery -->
215   <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
216   <!-- Latest compiled and minified Bootstrap JavaScript -->
217   <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
218
219   <script>
220
221     var types = ["ERROR", "WARNING", "INFO", "DEBUG"];
222     var peers = {<?php foreach($peers as $pid=>$id) echo "'$pid': '$id', "; ?>};
223     var msg_timeout;
224
225     function msg (content)
226     {
227       $("#msg").html(content);
228       $("#msg").stop(true);
229       $("#msg").fadeTo(100, 1).fadeTo(3000, 0.90).fadeOut(1000);
230     }
231
232     function showlevel (level)
233     {
234       $("tbody > tr").hide();
235       $(".btn-showlevel").removeClass("active");
236       $("#"+level).addClass("active");
237       for (var index = 0; index < types.length; ++index) {
238         $(".btn-showpeer.active").each(function(){
239           $("."+types[index]+"."+this.id).show();
240         });
241         if (types[index] == level)
242           return;
243       }
244     }
245
246     function shownone()
247     {
248       $(".btn-showpeer").removeClass("active");
249       $("tbody > tr").hide();
250     }
251
252     function showall()
253     {
254       $(".btn-showpeer:not(.active)").each(function(){showpeer(this.id)});
255     }
256
257     function showpeer (peer)
258     {
259       $("#"+peer).toggleClass("active");
260       if ($("#"+peer).hasClass("active")) {
261         for (var index = 0; index < types.length; ++index) {
262           var className = "." + types[index] + "." + peer;
263           $(className).show();
264           if ($("#"+types[index]).hasClass("active"))
265             return;
266         }
267       } else {
268         $("."+peer).hide();
269       }
270     }
271
272     function load_debug (btn, up)
273     {
274       var tr = $(btn).parents("tr");
275       var level;
276       var pos = parseInt(tr.attr("id"));
277       var first = pos + 1;
278       var last = pos - 1;
279       for (var index = 0; index < types.length; ++index) {
280         if (tr.hasClass(types[index]))
281         {
282           level = types[index];
283           break;
284         }
285       }
286       if (up) {
287         if (parseInt(tr.prev().attr("id")) == last) {
288           msg ("Already loaded");
289           return;
290         }
291         first = parseInt(tr.prevAll("."+level).first().attr("id")) + 1;
292         first = isNaN(first) ? 0 : first;
293       } else {
294         if (parseInt(tr.next().attr("id")) == first) {
295           msg ("Already loaded");
296           return;
297         }
298         last = parseInt(tr.nextAll("."+level).first().attr("id")) - 1;
299       }
300       if (first > last)
301         return;
302       $.ajax({
303         url: document.location,
304         data: { a: first, z: last }
305       }).done(function ( resp ) {
306         var loc = $("#"+(first-1));
307         var trs = $(resp);
308         for (var peer in peers) {
309           console.log (peer + "=>" + peers[peer]);
310           trs.filter(".P-"+peer).removeClass('P-'+peer).addClass('P-'+peers[peer]).find("td.peer").html(peers[peer]);
311         }
312         console.log (trs);
313         if (loc.length > 0)
314           loc.after(trs);
315         else {
316           $("#"+(last+1)).before(trs);
317         }
318         msg("Done loading " + (last-first+1) + " lines.");
319       });
320       //tr.nextUntil("."+tr.attr("class")).show();
321
322     }
323
324     function hide (btn)
325     {
326       var tr = $(btn).parents("tr");
327       tr.nextUntil("."+tr.attr("class")).hide();
328     }
329
330     $(function() {
331       $(".btn-showup").on ("click", function(){ load_debug(this, true) });
332       $(".btn-showdown").on ("click", function(){ load_debug(this, false) });
333       $(".btn-showlevel").on ("click", function(){ showlevel(this.id) });
334       $(".btn-showpeer").on ("click", function(){ showpeer(this.id) });
335       $("#btn-showall").on ("click", function(){ showall() });
336       $("#btn-shownone").on ("click", function(){ shownone() });
337     });
338   </script>
339 </body>
340 </html>