- Added HTML log viewer
[oweals/gnunet.git] / contrib / log.php
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4   <meta charset="utf-8">
5   <meta http-equiv="X-UA-Compatible" content="IE=edge">
6   <meta name="viewport" content="width=device-width, initial-scale=1">
7   
8   <title>GNUnet log view</title>
9
10   <!-- Latest compiled and minified Bootstrap CSS -->
11   <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
12   <!-- Optional theme -->
13   <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">
14
15   <style>
16     body {
17       font-family: arial,sans-serif;
18       color:#444;
19     }
20 /*    a {
21       text-decoration: none;
22       color:#000;
23     }*/
24     table {
25       font-size:12px;
26       border-collapse:collapse;
27     }
28     .level {
29       display: none;
30     }
31     .DEBUG {
32       background-color:#CCC;
33     }
34
35   </style>
36 </head>
37
38
39 <body>
40
41 <table class="table">
42   <tr>
43     <th>Date Time</th>
44     <th>uSec</th>
45     <th>Comp</th>
46     <th class="level">Level</th>
47     <th>Message</th>
48     <th></th>
49   </tr>
50 <?php
51
52 $path='log';
53
54 function render_row ($d, $component, $pid, $level, $msg)
55 {
56   $date = $d->format('Y-m-d'). '<br />' . $d->format('H:i:s');
57   echo "<tr class=\"$level\">";
58   echo "<td class=\"date\">$date</td>";
59   echo '<td class="usec">';
60   echo $d->format('u');
61   echo '</td>';
62   echo "<td class=\"comp\">$component</td><td class=\"level\">$level</td><td>$msg&nbsp;</td>";
63   echo '<td><button class="btn btn-xs btn-default btn-show"><span class="glyphicon glyphicon-plus"></span></button>';
64   echo '<button class="btn btn-xs btn-default btn-hide"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
65   $olddate = $date;
66
67
68 function process ($line)
69 {
70   $a = explode (' ', $line);
71   $date = DateTime::createFromFormat ("M d H:i:s-u", implode (' ', array_slice ($a, 0, 3)));
72   $component = $a[3];
73   $level = $a[4];
74   $msg = implode (' ', array_slice ($a, 5));
75   
76   render_row ($date, $component, 0, $level, $msg);
77 }
78
79
80 $handle = @fopen($path, 'r');
81 if ($handle) {
82     while (($line = fgets($handle)) !== false) {
83         process ($line);
84     }
85 } else {
86    echo "<div class=\"alert alert-danger\">Error opening file $path.</div>";
87 }
88
89 ?>
90
91 </table>
92   <!-- jQuery -->
93   <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
94   <!-- Latest compiled and minified Bootstrap JavaScript -->
95   <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
96
97   <script>
98
99     function show (btn)
100     {
101       var tr = $(btn).parents("tr");
102       tr.nextUntil("."+tr.attr("class")).show();
103       return;
104     }
105
106     function hide (btn)
107     {
108       var tr = $(btn).parents("tr");
109       tr.nextUntil("."+tr.attr("class")).hide();
110       return;
111     }
112
113     $(function() {
114       $(".DEBUG").hide();
115       $(".btn-show").on ("click", function(){ show(this) });
116       $(".btn-hide").on ("click", function(){ hide(this) });
117       console.log( "ready!" );
118     });
119   </script>
120 </body>
121 </html>