<commit_before>(* Ocsigen
 * Copyright (C) 2005 <NAME>
 * Laboratoire PPS - CNRS Universit Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)


(** Writing messages in the logs *)

let access = "access.log", ref stdout, ref Unix.stdout
let warningfile = "warnings.log", ref stderr, ref Unix.stderr
let error = "errors.log", ref stderr, ref Unix.stderr


(* Several processes will access the same files, but if I am right,
   it is not a problem when opening with O_APPEND
 *)
let open_files =
  let opened = ref false in
  let openlog f =
    Unix.openfile
      ((Ocsigen_config.get_logdir ())^"/"^f)
      [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_APPEND] 0o640
  in
  fun () ->
    if !opened
    then begin
      Unix.close !(Ocsigen_lib.thd3 access);
      Unix.close !(Ocsigen_lib.thd3 warningfile);
      Unix.close !(Ocsigen_lib.thd3 error)
    end;
    opened := true;
    let acc = openlog (Ocsigen_lib.fst3 access) in
    let war = openlog (Ocsigen_lib.fst3 warningfile) in
    let err = openlog (Ocsigen_lib.fst3 error) in
    Ocsigen_lib.snd3 access := Unix.out_channel_of_descr acc;
    Ocsigen_lib.snd3 warningfile := Unix.out_channel_of_descr war;
    Ocsigen_lib.snd3 error := Unix.out_channel_of_descr err;
    Ocsigen_lib.thd3 access := acc;
    Ocsigen_lib.thd3 warningfile := war;
    Ocsigen_lib.thd3 error := err;
    Unix.set_close_on_exec acc;
    Unix.set_close_on_exec war;
    Unix.set_close_on_exec err

let log_aux file console_print s =
  let date =
    let t = Unix.localtime (Unix.time ()) in
    Printf.sprintf
      "%04d-%02d-%02d %02d:%02d:%02d"
      (1900 + t.Unix.tm_year)
      (t.Unix.tm_mon + 1)
      t.Unix.tm_mday
      t.Unix.tm_hour
      t.Unix.tm_min
      t.Unix.tm_sec
  in
  if console_print then
    prerr_endline ("["^(Ocsigen_lib.fst3 file)^"] "^date^" - "^s);
  output_string !(Ocsigen_lib.snd3 file) date;
  output_string !(Ocsigen_lib.snd3 file) " - ";
  output_string !(Ocsigen_lib.snd3 file) s;
  output_string !(Ocsigen_lib.snd3 file) "\n";
  flush !(Ocsigen_lib.snd3 file)


let accesslog s =
  log_aux access (Ocsigen_config.get_verbose ()) s

let errlog s =
  log_aux error (not (Ocsigen_config.get_silent ())) s

let warning s =
  log_aux warningfile (Ocsigen_config.get_verbose ()) s

(*
let lwtlog =
  fun s ->
    let s = s^"\n" in
    let syslog = Syslog.openlog ~facility:`LOG_DAEMON ~logpath:????
           ~flags:[ `LOG_CONS ] "ocsigen" in
    Syslog.syslog syslog `LOG_NOTICE s;
    Syslog.closelog syslog
*)


let debug_noel =
  if Ocsigen_config.get_veryverbose () then
    (fun s -> Pervasives.prerr_string (s ()))
  else
    (fun s -> ())

let debug_noel2 =
  if Ocsigen_config.get_veryverbose () then
    Pervasives.prerr_string
  else
    (fun s -> ())

let debug =
  if Ocsigen_config.get_veryverbose () then
    (fun s -> Pervasives.prerr_endline (s ()))
  else
    (fun s -> ())

let debug2 =
  if Ocsigen_config.get_veryverbose () then
    Pervasives.prerr_endline
  else
    (fun s -> ())

let bip =
  if Ocsigen_config.get_veryverbose () then
    (fun i -> Pervasives.prerr_endline ("bip"^(string_of_int i)))
  else
    (fun i -> ())

let console =
  if (not (Ocsigen_config.get_silent ())) then
    (fun s -> print_endline (s ()))
  else
    (fun s -> ())

let console2 =
  if (not (Ocsigen_config.get_silent ())) then
    print_endline
  else
    (fun s -> ())

let unexpected_exception e s =
  warning ("Unexpected exception in "^s^": "^Ocsigen_lib.string_of_exn e)



(*

Re: [Caml-list] log function without evaluate arguments
De :
tmp123 <<EMAIL>>
   :
<EMAIL>
  Date :
Aujourd'hui 11:31:04

Hello,

Thanks a lot to everybody for your help.

I've been testing the different proposals. I must recognize I've not yet
reviewed the proposed library, it is next step.

The four methods tested are: lazy, fun, ifprint, and fun moving the "if"
to the caller (see full listing and results at the end of the post). Two
test has been done for each one: when parameter is an integer constant
and when parameter is the result of a funcion call who mades an addition.

The conclusion seems: defining that "lazy" method needs 1 unit of time,
proposal using "fun" instead of lazy needs 0.8, and the version<commit_msg>Fix email reference in a comment<commit_after>(* Ocsigen
 * Copyright (C) 2005 <NAME>
 * Laboratoire PPS - CNRS Universit Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)


(** Writing messages in the logs *)

let access = "access.log", ref stdout, ref Unix.stdout
let warningfile = "warnings.log", ref stderr, ref Unix.stderr
let error = "errors.log", ref stderr, ref Unix.stderr


(* Several processes will access the same files, but if I am right,
   it is not a problem when opening with O_APPEND
 *)
let open_files =
  let opened = ref false in
  let openlog f =
    Unix.openfile
      ((Ocsigen_config.get_logdir ())^"/"^f)
      [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_APPEND] 0o640
  in
  fun () ->
    if !opened
    then begin
      Unix.close !(Ocsigen_lib.thd3 access);
      Unix.close !(Ocsigen_lib.thd3 warningfile);
      Unix.close !(Ocsigen_lib.thd3 error)
    end;
    opened := true;
    let acc = openlog (Ocsigen_lib.fst3 access) in
    let war = openlog (Ocsigen_lib.fst3 warningfile) in
    let err = openlog (Ocsigen_lib.fst3 error) in
    Ocsigen_lib.snd3 access := Unix.out_channel_of_descr acc;
    Ocsigen_lib.snd3 warningfile := Unix.out_channel_of_descr war;
    Ocsigen_lib.snd3 error := Unix.out_channel_of_descr err;
    Ocsigen_lib.thd3 access := acc;
    Ocsigen_lib.thd3 warningfile := war;
    Ocsigen_lib.thd3 error := err;
    Unix.set_close_on_exec acc;
    Unix.set_close_on_exec war;
    Unix.set_close_on_exec err

let log_aux file console_print s =
  let date =
    let t = Unix.localtime (Unix.time ()) in
    Printf.sprintf
      "%04d-%02d-%02d %02d:%02d:%02d"
      (1900 + t.Unix.tm_year)
      (t.Unix.tm_mon + 1)
      t.Unix.tm_mday
      t.Unix.tm_hour
      t.Unix.tm_min
      t.Unix.tm_sec
  in
  if console_print then
    prerr_endline ("["^(Ocsigen_lib.fst3 file)^"] "^date^" - "^s);
  output_string !(Ocsigen_lib.snd3 file) date;
  output_string !(Ocsigen_lib.snd3 file) " - ";
  output_string !(Ocsigen_lib.snd3 file) s;
  output_string !(Ocsigen_lib.snd3 file) "\n";
  flush !(Ocsigen_lib.snd3 file)


let accesslog s =
  log_aux access (Ocsigen_config.get_verbose ()) s

let errlog s =
  log_aux error (not (Ocsigen_config.get_silent ())) s

let warning s =
  log_aux warningfile (Ocsigen_config.get_verbose ()) s

(*
let lwtlog =
  fun s ->
    let s = s^"\n" in
    let syslog = Syslog.openlog ~facility:`LOG_DAEMON ~logpath:????
           ~flags:[ `LOG_CONS ] "ocsigen" in
    Syslog.syslog syslog `LOG_NOTICE s;
    Syslog.closelog syslog
*)


let debug_noel =
  if Ocsigen_config.get_veryverbose () then
    (fun s -> Pervasives.prerr_string (s ()))
  else
    (fun s -> ())

let debug_noel2 =
  if Ocsigen_config.get_veryverbose () then
    Pervasives.prerr_string
  else
    (fun s -> ())

let debug =
  if Ocsigen_config.get_veryverbose () then
    (fun s -> Pervasives.prerr_endline (s ()))
  else
    (fun s -> ())

let debug2 =
  if Ocsigen_config.get_veryverbose () then
    Pervasives.prerr_endline
  else
    (fun s -> ())

let bip =
  if Ocsigen_config.get_veryverbose () then
    (fun i -> Pervasives.prerr_endline ("bip"^(string_of_int i)))
  else
    (fun i -> ())

let console =
  if (not (Ocsigen_config.get_silent ())) then
    (fun s -> print_endline (s ()))
  else
    (fun s -> ())

let console2 =
  if (not (Ocsigen_config.get_silent ())) then
    print_endline
  else
    (fun s -> ())

let unexpected_exception e s =
  warning ("Unexpected exception in "^s^": "^Ocsigen_lib.string_of_exn e)



(*

Re: [Caml-list] log function without evaluate arguments
From: tmp123 <<EMAIL>>
To: <EMAIL>
Date: Nov 7 2007, 10:37 am

Hello,

Thanks a lot to everybody for your help.

I've been testing the different proposals. I must recognize I've not yet
reviewed the proposed library, it is next step.

The four methods tested are: lazy, fun, ifprint, and fun moving the "if"
to the caller (see full listing and results at the end of the post). Two
test has been done for each one: when parameter is an integer constant
and when parameter is the result of a funcion call who mades an addition.

The conclusion seems: defining that "lazy" method needs 1 unit of time,
proposal using "fun" instead of lazy needs 0.8, and the version
{'fraction_non_alphanumeric': 0.10030425963488844, 'fraction_numerical': 0.015314401622718053, 'mean_word_length': 3.3671390611160317, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3799469', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3432, 'n_words': 1134}