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