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