1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 let syslog_oc = ref None
27 let t = Unix.localtime
(Unix.time
()) in
28 let { Unix.tm_year
= tm_year
; Unix.tm_mon
= tm_mon
; Unix.tm_mday
= tm_mday
;
29 Unix.tm_hour
= tm_hour
; Unix.tm_min
= tm_min
; Unix.tm_sec
= tm_sec
} = t
31 Printf.sprintf
"%4d/%02d/%02d %02d:%02d:%02d " (tm_year
+1900) (tm_mon
+1) tm_mday tm_hour tm_min tm_sec
33 let lprintf_handler = ref (fun s time
->
34 Printf.printf
"%sMessage [%s] discarded\n" time s
;
37 let lprintf fmt
= Printf.ksprintf
(fun s
-> try !lprintf_handler "" s
with _
-> ()) fmt
38 let lprintf2 m fmt
= Printf.ksprintf
(fun s
-> try !lprintf_handler (log_time ()) (m^
" "^s
) with _
-> ()) fmt
39 let lprintf_nl fmt
= Printf.ksprintf
(fun s
-> try !lprintf_handler (log_time ()) (s^
"\n") with _
-> ()) fmt
40 let lprintf_nl2 m fmt
= Printf.ksprintf
(fun s
-> try !lprintf_handler (log_time ()) (m^
" "^s^
"\n") with _
-> ()) fmt
42 let lprint_newline () = lprintf "\n"
43 let lprint_char = lprintf "%c"
44 let lprint_int = lprintf "%d"
45 let lprint_string = lprintf "%s"
47 let set_lprintf_handler f
=
50 let lprintf_max_size = ref 100
51 let lprintf_size = ref 0
52 let lprintf_fifo = Fifo.create
()
53 let lprintf_to_channel = ref true
55 let lprintf_output = ref (Some stderr
)
56 let lprintf_original_output = ref None
58 let keep_console_output () =
59 match !lprintf_original_output with
60 Some c
when c
= stderr
|| c
= stdout
-> true
64 set_lprintf_handler (fun time s
->
65 (match !syslog_oc with
67 | Some oc
-> Syslog.syslog oc `LOG_INFO s
);
68 match !lprintf_output with
69 | Some out
when !lprintf_to_channel ->
70 Printf.fprintf out
"%s" (time ^ s
); flush out
72 if !lprintf_size >= !lprintf_max_size then
73 ignore
(Fifo.take
lprintf_fifo)
76 Fifo.put
lprintf_fifo (time ^ s
)
80 match !lprintf_output with
81 Some oc
when oc
== Pervasives.stdout
-> lprintf_output := None
85 lprintf_to_channel := false;
86 match !lprintf_output with
89 if oc
!= stderr
&& oc
!= stdout
then
91 lprintf_output := None
95 lprintf_output := Some oc
;
96 lprintf_to_channel := true
98 let log_to_buffer buf
=
101 let s = Fifo.take
lprintf_fifo in
103 Buffer.add_string buf
s
109 let html_mods_commands buf n c l
=
110 (* Name Class List *)
111 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\"
112 name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0
113 width=\\\"100pc\\\"\\>\\<tbody\\>\\<tr\\>"
115 List.iter
(fun (w
,x
,y
,z
) ->
116 (* Class Title Onclick Value *)
117 Printf.bprintf buf
"\\<td class=\\\"%s\\\"
118 title=\\\"%s\\\" onMouseOver=\\\"mOvr(this,'mOvr1');\\\" onMouseOut=\\\"mOut(this);\\\"
119 onClick=\\\"%s\\\" \\>%s\\</td\\>"
122 Printf.bprintf buf
"\\</tr\\>\\</tbody\\>\\</table\\>\\</div\\>"
125 html_mods_commands buf "commandsTable" "commands" [
126 ( "bu bbig", "Extend search to more servers and view results", "mSub('output','vr');", "Extend search" ) ;
129 type sort_kind
= Num
(* numeric, parse size suffixes (kMGT) *) | Str
(* plain string *)
131 let html_mods_table_header buf ?
(total
= "0") n c l
=
132 (* Name Class List *)
133 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\" name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0\\>"
135 if List.length l
> 0 then begin
136 Printf.bprintf buf
"\\<tr\\>";
137 List.iter
(fun (w
,x
,y
,z
) ->
138 let sort = match w
with Num
-> "1" | Str
-> "0" in
139 (* Sort Class Title Value *)
140 Printf.bprintf buf
"\\<td onClick=\\\"_tabSort(this,%s,%s);\\\" class=\\\"%s\\\" title=\\\"%s\\\"\\>%s\\</td\\>"
143 Printf.bprintf buf
"\\</tr\\>"
145 (* Add colspan functionality to html_mods_table_header *)
147 let html_mods_table_header_colspan buf ?
(total
="0") n c l
=
148 (* Name Class List *)
149 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\" name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0\\>\\<tr\\>"
151 List.iter
(fun (v
,w
,x
,y
,z
) ->
152 (* Sort Class Title Value *)
153 Printf.bprintf buf
"\\<td colspan=%s onClick=\\\"_tabSort(this,%s,%s);\\\" class=\\\"%s\\\" title=\\\"%s\\\"\\>%s\\</td\\>"
156 Printf.bprintf buf
"\\</tr\\>"
158 let html_mods_table_no_header buf n c l
=
160 (* Name Class List *)
161 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\" name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0\\>\\<tr\\>"
163 List.iter
(fun (t,c
,d
) ->
164 (* Title Class Value *)
165 Printf.bprintf buf
"\\<td class=\\\"%s\\\" %s\\>%s\\</td\\>"
166 c
(if t <> "" then "title=\\\"" ^
t ^
"\\\"" else "") d
;
168 Printf.bprintf buf
"\\</tr\\>\\</table\\>\\</div\\>"
170 let html_mods_table_one_row buf n c l
=
172 (* Name Class List *)
173 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\" name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0\\>\\<tr\\>"
175 List.iter
(fun (t,c
,d
) ->
176 (* Title Class Value *)
177 Printf.bprintf buf
"\\<td class=\\\"%s\\\" %s\\>%s\\</td\\>"
178 c
(if t <> "" then "title=\\\"" ^
t ^
"\\\"" else "") d
;
180 Printf.bprintf buf
"\\</tr\\>\\</table\\>\\</div\\>"
182 let html_mods_table_one_col buf n c l
=
184 (* Name Class List *)
185 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table id=\\\"%s\\\" name=\\\"%s\\\" class=\\\"%s\\\" cellspacing=0 cellpadding=0\\>\\<tr\\>"
187 List.iter
(fun (t,c
,d
) ->
188 (* Title Class Value *)
189 Printf.bprintf buf
"\\<tr\\>\\<td class=\\\"%s\\\" %s\\>%s\\</td\\>\\</tr\\>"
190 c
(if t <> "" then "title=\\\"" ^
t ^
"\\\"" else "") d
;
192 Printf.bprintf buf
"\\</tr\\>\\</table\\>\\</div\\>"
194 let html_mods_td buf l
=
196 List.iter
(fun (t,c
,d
) ->
197 (* Title Class Value *)
198 Printf.bprintf buf
"\\<td class=\\\"%s\\\" %s\\>%s\\</td\\>"
199 c
(if t <> "" then "title=\\\"" ^
t ^
"\\\"" else "") d
;
203 let html_mods_big_header_start buf c l
=
204 Printf.bprintf buf
"\\<div class=\\\"%s\\\"\\>\\<table class=main border=0 cellspacing=0 cellpadding=0\\>\\<tr\\>\\<td\\>" c
;
205 Printf.bprintf buf
"\\<table cellspacing=0 cellpadding=0 width=100%%\\>\\<tr\\>";
206 Printf.bprintf buf
"\\<td width=100%%\\>\\</td\\>";
207 let len = List.length l
in
211 Printf.bprintf buf
"\\<td nowrap class=\\\"fbig%s\\\"\\>%s\\</td\\>" (if !cnt = len then " pr" else "") s;
213 Printf.bprintf buf
"\\</tr\\>\\</table\\>\\</td\\>\\</tr\\>\\<tr\\>\\<td\\>"
215 let html_mods_big_header_end buf
=
216 Printf.bprintf buf
"\\</td\\>\\</tr\\>\\</table\\>\\</div\\>"
218 let html_mods_counter = ref true
220 let html_mods_cntr () =
221 html_mods_counter := not
!html_mods_counter;
222 if !html_mods_counter then 1 else 2
224 let html_mods_cntr_init () =
225 html_mods_counter := true
227 let print_plural_s v
=
228 if v
> 1 then "s" else ""