<commit_before>(* Unison file synchronizer: src/ubase/trace.ml *)
(* Copyright 1999-2020, <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)


(* ---------------------------------------------------------------------- *)
(* Choosing where messages go *)

type trace_printer_choices = [`Stdout | `Stderr | `FormatStdout]

let traceprinter = ref (`Stderr : trace_printer_choices)

let redirect x = (traceprinter := x)

(* ---------------------------------------------------------------------- *)
(* Debugging messages *)

let debugmods =
  Prefs.createStringList "debug"
    "!debug module xxx ('all' -> everything, 'verbose' -> more)"
    ("This preference is used to make Unison print various sorts of "
     ^ "information about what it is doing internally on the standard "
     ^ "error stream.  It can be used many times, each time with the name "
     ^ "of a module for which debugging information should be printed.  "
     ^ "Possible arguments for \\verb|debug| can be found "
     ^ "by looking for calls to \\verb|Util.debug| in the "
     ^ "sources (using, e.g., \\verb|grep|).  "
     ^ "Setting \\verb|-debug all| causes information from {\\em all} "
     ^ "modules to be printed (this mode of usage is the first one to try, "
     ^ "if you are trying to understand something that Unison seems to be "
     ^ "doing wrong); \\verb|-debug verbose| turns on some additional "
     ^ "debugging output from some modules (e.g., it will show exactly "
     ^ "what bytes are being sent across the network).")

let debugtimes =
  Prefs.createBool "debugtimes"
    false "*annotate debugging messages with timestamps" ""

let runningasserver = ref false

let debugging() = (Prefs.read debugmods) <> []

let enabled modname =
  let m = Prefs.read debugmods in
  let en =
    m <> [] && (   (* tracing labeled "" is enabled if anything is *)
                   (modname = "")
                || (* '-debug verbose' enables everything *)
                   (Safelist.mem "verbose" m)
                || (* '-debug all+' likewise *)
                   (Safelist.mem "all+" m)
                || (* '-debug all' enables all tracing not marked + *)
                   (Safelist.mem "all" m && not (Util.endswith modname "+"))
                || (* '-debug m' enables m and '-debug m+' enables m+ *)
                   (Safelist.mem modname m)
                || (* '-debug m+' also enables m *)
                   (Safelist.mem (modname ^ "+") m)
               ) in
  en

let enable modname onoff =
  let m = Prefs.read debugmods in
  let m' = if onoff then (modname::m) else (Safelist.remove modname m) in
  Prefs.set debugmods m'

let debug modname thunk =
  if enabled modname then begin
    let s = if !runningasserver then "server: " else "" in
    let time =
      if Prefs.read debugtimes then
        let tm = Util.localtime (Util.time()) in
        Printf.sprintf "%02d:%02d:%02d"
          tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
      else "" in
    if time<>"" || s<>"" || modname<>"" then begin
      let time = if time="" || (s=""&&modname="") then time else time^": " in
      match !traceprinter with
      | `Stdout -> Printf.printf "[%s%s%s] " time s modname
      | `Stderr -> Printf.eprintf "[%s%s%s] " time s modname
      | `FormatStdout -> Format.printf "[%s%s%s] " time s modname
      end;
    thunk();
    flush stderr
  end

(* We set the debugPrinter variable in the Util module so that other modules
   lower down in the module dependency graph (so that they can't just
   import Trace) can also print debugging messages. *)
let _ = Util.debugPrinter := Some(debug)


(* ---------------------------------------------------------------------- *)
(* Logging *)

let logging =
  Prefs.createBool "log" true
    "!record actions in logfile"
    "When this flag is set, Unison will log all changes to the filesystems
     on a file."

let logfile =
  Prefs.createFspath "logfile"
    (Util.fileInHomeDir "unison.log")
    "!logfile name"
    "By default, logging messages will be appended to the file
     \\verb|unison.log| in your HOME directory.  Set this preference if<commit_msg>Make logfile default location .unison rather than $HOME.<commit_after>(* Unison file synchronizer: src/ubase/trace.ml *)
(* Copyright 1999-2020, <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)


(* ---------------------------------------------------------------------- *)
(* Choosing where messages go *)

type trace_printer_choices = [`Stdout | `Stderr | `FormatStdout]

let traceprinter = ref (`Stderr : trace_printer_choices)

let redirect x = (traceprinter := x)

(* ---------------------------------------------------------------------- *)
(* Debugging messages *)

let debugmods =
  Prefs.createStringList "debug"
    "!debug module xxx ('all' -> everything, 'verbose' -> more)"
    ("This preference is used to make Unison print various sorts of "
     ^ "information about what it is doing internally on the standard "
     ^ "error stream.  It can be used many times, each time with the name "
     ^ "of a module for which debugging information should be printed.  "
     ^ "Possible arguments for \\verb|debug| can be found "
     ^ "by looking for calls to \\verb|Util.debug| in the "
     ^ "sources (using, e.g., \\verb|grep|).  "
     ^ "Setting \\verb|-debug all| causes information from {\\em all} "
     ^ "modules to be printed (this mode of usage is the first one to try, "
     ^ "if you are trying to understand something that Unison seems to be "
     ^ "doing wrong); \\verb|-debug verbose| turns on some additional "
     ^ "debugging output from some modules (e.g., it will show exactly "
     ^ "what bytes are being sent across the network).")

let debugtimes =
  Prefs.createBool "debugtimes"
    false "*annotate debugging messages with timestamps" ""

let runningasserver = ref false

let debugging() = (Prefs.read debugmods) <> []

let enabled modname =
  let m = Prefs.read debugmods in
  let en =
    m <> [] && (   (* tracing labeled "" is enabled if anything is *)
                   (modname = "")
                || (* '-debug verbose' enables everything *)
                   (Safelist.mem "verbose" m)
                || (* '-debug all+' likewise *)
                   (Safelist.mem "all+" m)
                || (* '-debug all' enables all tracing not marked + *)
                   (Safelist.mem "all" m && not (Util.endswith modname "+"))
                || (* '-debug m' enables m and '-debug m+' enables m+ *)
                   (Safelist.mem modname m)
                || (* '-debug m+' also enables m *)
                   (Safelist.mem (modname ^ "+") m)
               ) in
  en

let enable modname onoff =
  let m = Prefs.read debugmods in
  let m' = if onoff then (modname::m) else (Safelist.remove modname m) in
  Prefs.set debugmods m'

let debug modname thunk =
  if enabled modname then begin
    let s = if !runningasserver then "server: " else "" in
    let time =
      if Prefs.read debugtimes then
        let tm = Util.localtime (Util.time()) in
        Printf.sprintf "%02d:%02d:%02d"
          tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
      else "" in
    if time<>"" || s<>"" || modname<>"" then begin
      let time = if time="" || (s=""&&modname="") then time else time^": " in
      match !traceprinter with
      | `Stdout -> Printf.printf "[%s%s%s] " time s modname
      | `Stderr -> Printf.eprintf "[%s%s%s] " time s modname
      | `FormatStdout -> Format.printf "[%s%s%s] " time s modname
      end;
    thunk();
    flush stderr
  end

(* We set the debugPrinter variable in the Util module so that other modules
   lower down in the module dependency graph (so that they can't just
   import Trace) can also print debugging messages. *)
let _ = Util.debugPrinter := Some(debug)


(* ---------------------------------------------------------------------- *)
(* Logging *)

let logging =
  Prefs.createBool "log" true
    "!record actions in logfile"
    "When this flag is set, Unison will log all changes to the filesystems
     on a file."

let logfile =
  Prefs.createFspath "logfile"
    (Util.fileInUnisonDir "unison.log")
    "!logfile name"
    "By default, logging messages will be appended to the file
     \\verb|unison.log| in your .unison directory.  Set this preference if
{'fraction_non_alphanumeric': 0.14787547328565417, 'fraction_numerical': 0.003155237694572991, 'mean_word_length': 2.242072962836686, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4379014', 'n_tokens_mistral': 2750, 'n_tokens_neox': 2650, 'n_words': 1076}