
     Copyright © 2011 , 2012 MLstate 

     This file is part of  . 

      is free software : you can redistribute it and/or modify it under the 
     terms of the GNU Affero General Public License , version 3 , as published by 
     the Free Software Foundation . 

      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 Affero General Public License for 
     more details . 

     You should have received a copy of the GNU Affero General Public License 
     along with  . If not , see < / > . 

    Copyright © 2011, 2012 MLstate

    This file is part of Opa.

    Opa is free software: you can redistribute it and/or modify it under the
    terms of the GNU Affero General Public License, version 3, as published by
    the Free Software Foundation.

    Opa 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 Affero General Public License for
    more details.

    You should have received a copy of the GNU Affero General Public License
    along with Opa. If not, see </>.
*)

* 
    Debug Variables Management for the MLstate debug preprocessor ( ppdebug ) 

    @see " opalang / utils / ppdebug.pl " the script which implement the preprocess 
    @author   
    @author   

   Debug Variables Management for the MLstate debug preprocessor (ppdebug)

   @see "opalang/utils/ppdebug.pl" the script which implement the preprocess
   @author Louis Gesbert
   @author Mathieu Barbin
*)

* { 6 Template }

* 
    Template for a module name [ MyId ] 
    { [ 
    # < Debugvar : MY_VAR > 
    let debug fmt = 
      OManager.printf ( " @{<cyan>[MyId]@}@ @[<2>"^^fmt^^"@]@. " ) 

    ( * utilisation
   Template for a module name [MyId]
   {[
   #<Debugvar:MY_VAR>
   let debug fmt =
     OManager.printf ("@{<cyan>[MyId]@}@ @[<2>"^^fmt^^"@]@.")

   (* utilisation *)
   ...
   let _ =
     #<If:MY_VAR $minlevel 4>
       debug "do some %s of level %d@\n" "debug" 4
     #<End>
   in
   ...
   ]}
*)

* { 6 Static Debug Preprocessor -- Syntax Documentation }

(**
   The preprocessor {b ppdebug} is used whenever you want to add logs or computation
   in your application (assert, etc...), but you do not want the code to be embedded in release mode.

   {[
   let g =
       #<Debug>
         print_endline "this is a log" ;
       #<End>
       5
   ]}

   will be either
   {[
   let g =
(*        #<Debug> *)
         print_endline "this is a log" ;
(*        #<End> *)
       5
   ]}
   or
   {[
   let g =
(*        #<Debug> *)(*
         print_endline "this is a log" ;
*)(*        #<End> *)
       5
   ]}
   depending of the env var {b MLSTATE_PPDEBUG}, or on the tag used with ocamlbuild.

   Note that you can also have a different code instead of nothing, using [#else]
   {[
   let g =
       #<Debug>
         print_endline "this is BIG BIG LOG" ;
         print_endline "with so many details that I'd like to keep it only in debug mode" ;
       #<Else>
         print_endline "short log" ;
       #<End>
       5
   ]}

   <!> The code should be totally recompiled with/without option PPDEBUG to make changes be taken in consideration.
   For dynamic toggle behavior, cf the next part.

   For other compile-time checks (eg. target-dependent code), you can use the directive [#<Ifstatic:VAR REGEXP>].
   A check will be done by the preprocessor on environment variable [VAR] with the regexp [REGEXP], and comment
   the unappropriate code:

   {[
   let build_arch =
       #<Ifstatic:ARCH Win.*> "Windows" #<Else> "Other" #<End>
   ]}

   will define [build_arch] to be ["Windows"] whenever a variable [ARCH] is defined with a value beginning with
   ["Win"]
*)

* { 6 Dynamic Debug Preprocessor -- Syntax Documentation }

* 
    This preprocessor allows you to do some extra logs and computation without recompiling 
    everything with a different preprocess configuration , but assures you that your code will 
    not be embeded in release mode . 

    { [ 
    let g = 
       # < If : MY_SPLENDID_VARIABLE > 
          print_endline " this is a log " 
       # < End > ; 
       5 
    ] } 

    will be transformed in : 
    { [ 
    let g = 
 ( *        # < If >
   This preprocessor allows you to do some extra logs and computation without recompiling
   everything with a different preprocess configuration, but assures you that your code will
   not be embeded in release mode.

   {[
   let g =
      #<If:MY_SPLENDID_VARIABLE>
         print_endline "this is a log"
      #<End> ;
      5
   ]}

   will be transformed in :
   {[
   let g =
(*       #<If> *) if (DebugVariables.default) DebugVariables.my_splendid_variable then begin
         print_endline "this is a log"
(*       #<End> *) end else begin () end ;
      5
   ]}
   or if release-mode is activated :
   {[
   let g =
  if ( DebugVariables.default )  then begin 
          print_endline " this is a log " 
 ( *        # < End >
         print_endline "this is a log"
(*       #<End> *) end else *) begin () end ;
      5
   ]}

   Note that the [;] at the end is kept: the ppdebug-[#<If>] should be an ocaml expression.

   You can find in this file all documented variables available.
   You should add your variables there, and documenting it following the guidelines.

   Extension of the test condition :

   By default, we check that the env var is defined and doesn't have value ["0"] (test is [DebugVariables.default]).
   You can use a different test using the following syntax :

   {[
   let g =
      #<If:MY_SPLENDID_VARIABLE $equals "MY_SPLENDID_VALUE">
         print_endline "this is a log"
      #<End> ;
      5
   ]}

   processed as :

   {[
   let  =
(*       #<If> *) if (DebugVariables.equals "MY_SPLENDID_VALUE") DebugVariables.my_splendid_variable then begin
         print_endline "this is a log"
(*       #<End> *) end else begin () end ;
      5
   ]}

   Matching on debug variables :

   If you have a multi-state debug variable, you can add a runtime-match as long as it's enclosed
   in a [#<If$defined>] :

   {[
   let verb x =
     #<If:MY_VERBOSE$defined> match DebugVariables.my_verbose with
       | Some "quite verbose" -> print_debug x
       | Some "very verbose" -> print_debug_very_verbose x
       | _ -> failwith "Bad value for debug variable MY_VERBOSE"
     #<End>
   ]}

   Warnings :
   + <!> This preprocessor should not be used for top-level definitions.
   + <!> Such a preprocessed part is a Ocaml.expr, necessary of type [unit] if there is no [#<Else>]
   + <!> Remember that in release, you're always in the Else case. Keep this for debug !

   You can find some tester available in this module.
   You can add any tester you want editing this module, and following guidelines.
*)


* { 6 Variables }

* 
    When you do lots of tests on the same debug - variable , you can use the shortcut : 

    { [ 
    # < Debugvar : VAR > 
    ] } 

    The [: VAR ] in subsequent uses of [ # < If > ] can be omitted . 

    Guidelines : 
       + all variables are documented in alphabetic order ! ! 
       + all variable begin with { e MLSTATE _ } when you export it in shell 
       + all variable are UPPERCASED 
       + all corresponding variable name in this file are lowercased and does not contains MLSTATE _ 

   When you do lots of tests on the same debug-variable, you can use the shortcut:

   {[
   #<Debugvar:VAR>
   ]}

   The [:VAR] in subsequent uses of [#<If>] can be omitted.

   Guidelines :
      + all variables are documented in alphabetic order !!
      + all variable begin with {e MLSTATE_} when you export it in shell
      + all variable are UPPERCASED
      + all corresponding variable name in this file are lowercased and does not contains MLSTATE_
*)

(**
   The type of all debug_var.
   [None] if the variable is not defined, [Some value] if there is a value.

   <!> Env variables are checked only once, when this module is loaded.
*)
type debug_var = string option

* 
    { b MLSTATE_ADD_STDLIB } 
    Be verbose to explain what files are added , and what files are rejected 
    during the pass [  ] , in presence and in absence of option 
    [ --no - embedded - stdlib ] 

    Level 1 : print only rejected files 
    Level 2 : print only keeped files 
    Level 3 : print both 

   {b MLSTATE_ADD_STDLIB}
   Be verbose to explain what files are added, and what files are rejected
   during the pass [AddStdlibFiles], in presence and in absence of option
   [--no-embedded-stdlib]

   Level 1: print only rejected files
   Level 2: print only keeped files
   Level 3: print both
*)
val add_stdlib : debug_var

(**
   {b BADOP_DEBUG}
   Adds some experimental options to servers related to badop modes.
*)
val badop_debug : debug_var

(**
   {b MLSTATE_BSL_LOADING}
   Use with level.
   Be more verbose during loading.
   1: plugins
   *: undocumented
*)
val bsl_loading : debug_var

* 
    { b  } 
    When set , disables restriction checks on bypasses . 
    Useful when recompiling printed code containing 
    such bypasses . 

   {b MLSTATE_BSL_NO_RESTRICTION}
   When set, disables restriction checks on bypasses.
   Useful when recompiling printed code containing
   such bypasses.
*)
val bsl_no_restriction : debug_var

* 
    { b MLSTATE_BSL_PROJECTION } 
    Use with level , print some debug logs about external bypass 
    projection . ( libbsl , backend , qmltop , etc ... ) 

   {b MLSTATE_BSL_PROJECTION}
   Use with level, print some debug logs about external bypass
   projection. (libbsl, backend, qmltop, etc...)
*)
val bsl_projection : debug_var

(**
   {b MLSTATE_BSL_REGISTER}
   Use with level. Be more verbose during building maps, and dealing
   with keys. Used during bslregister.
*)
val bsl_register : debug_var

* 
    { b MLSTATE_BSL_SL } 
    Use with level . Be more verbose during some operations implemented 
    in the ServerLib . 
    This happens at runtime , during projections . 

   {b MLSTATE_BSL_SL}
   Use with level. Be more verbose during some operations implemented
   in the ServerLib.
   This happens at runtime, during projections.
*)
val bsl_sl : debug_var

* 
    { b MLSTATE_BYPASS_HOISTING } 
    insert debug message during the bypass hoisting pass . Works with level . 
    Level 1 : 
    Level 2 : 

   {b MLSTATE_BYPASS_HOISTING}
   insert debug message during the bypass hoisting pass. Works with level.
   Level 1 :
   Level 2 :
*)
val bypass_hoisting : debug_var

* 
    { b  } 

   {b MLSTATE_BUFFER_POOL_DEBUG}
*)
val buffer_pool_debug : debug_var

(**
   {b MLSTATE_BUFFER_POOL_ENABLE}
*)
val buffer_pool_enable : debug_var


* 
    One variable to check them all ! 

    If you do nt spell correctly a debug variable by exporting it before running your application , 
    you may be surprised that the execution has not the expected behavior , and you may loose time until 
    you notice that it is because of a variable not correctly defined . 

    If ( and only if ) [ " MLSTATE_CHECK_VARS " ] is set with a value different than [ " 0 " ] , 
    this module will do a check on the global process environment . 
    For each env variable starting with [ " MLSTATE _ " ] which are not defined ( and documented :)) 
    in this module , a warning will be printed on the standard error . 

    Strong mode : [ MLSTATE_CHECK_VARS="s " ] will make an error rather than a warning , stopping the execution . 

   One variable to check them all !

   If you dont spell correctly a debug variable by exporting it before running your application,
   you may be surprised that the execution has not the expected behavior, and you may loose time until
   you notice that it is because of a variable not correctly defined.

   If (and only if) ["MLSTATE_CHECK_VARS"] is set with a value different than ["0"],
   this module will do a check on the global process environment.
   For each env variable starting with ["MLSTATE_"] which are not defined (and documented :))
   in this module, a warning will be printed on the standard error.

   Strong mode : [MLSTATE_CHECK_VARS="s"] will make an error rather than a warning, stopping the execution.
*)
val check_vars : debug_var

(** {9 Closure} *)

* 
    { b  } 
    Works only for caml bypasses ( with  only , but this could be fixed ) 
    Level 0 : runtime assertions were typing is not possible 
              - qml closures actually have a compatible structure 
              - imported functions must be exported closures 
    Level 1 : previous + 
              tells when applying and receiving magic arguments ( to check bsl projections ) 
    Level 2 : previous + 
              tells when applying closures to arguments and show the resulting value ( very verbose ) 

   {b MLSTATE_CLOSURE_DEBUG}
   Works only for caml bypasses (with qmlflat only, but this could be fixed)
   Level 0 : runtime assertions were typing is not possible
             - qml closures actually have a compatible structure
             - imported functions must be exported closures
   Level 1 : previous +
             tells when applying and receiving magic arguments (to check bsl projections)
   Level 2 : previous +
             tells when applying closures to arguments and show the resulting value (very verbose)
*)
val closure_debug : debug_var
val closure_opt : debug_var (** generate optimized applyX functions *)
val closure_stat : debug_var (** display a few statistics in the passes uncurry/closure *)

* { 9   }

(**
   Debug vars used for activating/desactivating some sharing during the passes client/server
   constant sharing. Variable are read only if the pass is activated.
*)

val const_sharing_client_float : debug_var
val const_sharing_client_record : debug_var
val const_sharing_client_remove_coerce : debug_var
val const_sharing_client_string : debug_var

val const_sharing_server_float : debug_var
val const_sharing_server_record : debug_var
val const_sharing_server_remove_coerce : debug_var
val const_sharing_server_string : debug_var

* { 9 Cps }

* 
    { b MLSTATE_CPS_DEBUG } : 
    insert debug introspection message to trace the execution flow of continuations . 

    < ! > Note that CPS_DEBUG env var should also be set at runtime of the compiled 
    application for logs to be produced , with a given level . 

    < ! > At compile time , the level is not read , everything is generated . 

    { [ 
    export  
    // compile 
    // run 
    unset   MLSTATE_CPS_DEBUG 
    // run without debug logs 
    // compile 
    // run without debug logs 
    export  
    // run without debug logs ( need a recompilation ) 
    ] } 

    This var is used with increasing levels from 1 to 100 : 
    + 1   : print a few things 
    + 10 : cont tracer ( print every cont application ) 
    + 100 : backtraces display function arguments ( WIP ) 

   {b MLSTATE_CPS_DEBUG} :
   insert debug introspection message to trace the execution flow of continuations.

   <!> Note that CPS_DEBUG env var should also be set at runtime of the compiled
   application for logs to be produced, with a given level.

   <!> At compile time, the level is not read, everything is generated.

   {[
   export MLSTATE_CPS_DEBUG=1
   // compile
   // run
   unset  MLSTATE_CPS_DEBUG
   // run without debug logs
   // compile
   // run without debug logs
   export MLSTATE_CPS_DEBUG=1
   // run without debug logs (need a recompilation)
   ]}

   This var is used with increasing levels from 1 to 100 :
   + 1  : print a few things
   + 10 : cont tracer (print every cont application)
   + 100: backtraces display function arguments (WIP)
*)
val cps_debug : debug_var

(**
   {b MLSTATE_CPS_KEEP_LETCONT} :
   do not remove continuation directly applied. Used just for benchmark purpose.
*)
val cps_keep_letcont : debug_var

(**
   {b MLSTATE_CPS_NOSKIP} :
   do not use the skip optimisation. Used just for benchmark and tracability purpose.
*)
val cps_noskip : debug_var

(**
   {b MLSTATE_STACK_TRACE}
   When set, activates the printing of backtraces
   If it contains "tr", then transaction contexts are printed
   If it contains "th", then thread contexts are printed
   If it contains "args", then fonction arguments are printed
*)
val cps_stack_trace : debug_var

* 
    { b MLSTATE_CPS_VERBOSE } : 
    compile time only : let the cps rewriting pass be more verbose . Used with level . 
    No changes to the generated code , affect only the transformation pass . 
    + Level 1 : 
                + print some timing 
    + Level 2 : 
                + previous 
                + print optimisation logs 

   {b MLSTATE_CPS_VERBOSE} :
   compile time only : let the cps rewriting pass be more verbose. Used with level.
   No changes to the generated code, affect only the transformation pass.
   + Level 1 :
               + print some timing
   + Level 2 :
               + previous
               + print optimisation logs
*)
val cps_verbose : debug_var

* 
    { b  } : 
    Print blocking stack 

   {b MLSTATE_CPS_BLOCKING_WAIT} :
   Print blocking stack
*)
val cps_blocking_wait : debug_var

* { b MLSTATE_DATABASE_RECONNECT } 
   Set up a delay in seconds for automatic reconnection when a database server is lost . 
   ( if unset , never reconnect automatically ) 

  Set up a delay in seconds for automatic reconnection when a database server is lost.
  (if unset, never reconnect automatically)
*)
val database_reconnect : debug_var

(**
   {b MLSTATE_DB3_NO_FINAL_SNAPSHOT}
   When activated, when the database is closed, we do not make a snapshot of the current revision,
   meaning that the transaction file will not be empty, and pending transaction reapplied at
   reopen time.
   This is used for debugging the binary format of transaction (or queries) because if we
   always use a snapshot when we close the database, the transaction binary file is always empty.
*)
val db3_no_final_snapshot : debug_var

* 
    { b MLSTATE_DB3_TRANSACTION_LIMIT } 
    Defines a limit of operations within a transaction , after which it is automatically aborted . 
    Default is 10000 . This is a safeguard against exploding the memory by spamming a 
    transaction with too many writes . 0 disables the limit . 

   {b MLSTATE_DB3_TRANSACTION_LIMIT}
   Defines a limit of operations within a transaction, after which it is automatically aborted.
   Default is 10000. This is a safeguard against exploding the memory by spamming a
   transaction with too many writes. 0 disables the limit.
*)
val db3_transaction_limit : debug_var

(**
   {b DbGen obscure toggles}
*)
(** run-time *)
val dbgen_always_upgrade : debug_var (** Force upgrade of incompatible databases when they are opened *)
val dbgen_butcher : debug_var (** When updating a field from an old db is not possible, remove and re-create *)

val dbgen_debug : debug_var (** Enables some compile-time debug messages, adds debug prints in the
                                generated code and, at run-time, dbGen-related debug messages *)
(** compile-time only: *)
val dbgen_flags : debug_var
* comma - separated list of flags for DbGen , compile - time : 
       - ' fullcoerce ' : put coerces almost everywhere . Useful for checks 
       - ' source ' : instead of normal fresh identifiers , insert " source " textual ones in 
                   AST . Useful to manual checks from toplevel . 
       - ' nodefault ' : do n't complain about missing default values , use by - default defaults . 
       - ' nolazy ' : disable lazy records 
       - ' sharing ' : enable full - sharing in the DB ( enabled in db tests , temporary 
                    disabled for now to workaround a bug in the low - level implementation 
                    of copy - on - write ) 
       - ' noflatten ' : do n't flatten recursive types , store them as deep trees 
       - ' copylink ' : when flattening recursive types , link them with internal copy - nodes instead of link - nodes 
  
      - 'fullcoerce': put coerces almost everywhere. Useful for checks
      - 'source': instead of normal fresh identifiers, insert "source" textual ones in
                  AST. Useful to manual checks from toplevel.
      - 'nodefault': don't complain about missing default values, use by-default defaults.
      - 'nolazy': disable lazy records
      - 'sharing': enable full-sharing in the DB (enabled in db tests, temporary
                   disabled for now to workaround a bug in the low-level implementation
                   of copy-on-write)
      - 'noflatten': don't flatten recursive types, store them as deep trees
      - 'copylink': when flattening recursive types, link them with internal copy-nodes instead of link-nodes
  *)


(**
   {b MLSTATE_DEBUG_DB}
   Enables debug messages from the low-level database
   You can specify :
     - a debug level (int): to display low level db debug
     - 'migration': to display only migration debug
     - 'io': to display only io debug

*)
val debug_db : debug_var

(**
   {b MLSTATE_DEBUG_DB_INDEX}
   Enables debug messages from the index handling in the database
*)
val debug_db_index : debug_var

(**
   {b MLSTATE_DEBUG_DB_MAX_DELTA}
   How many delta nodes are permitted between full nodes; 0 means all are full.
*)
val debug_db_max_delta : debug_var

* 
    { b MLSTATE_DEBUG_PAXOS } 
    Enables debug messages for the Paxos algorithm ( including leader election ) 

   {b MLSTATE_DEBUG_PAXOS}
   Enables debug messages for the Paxos algorithm (including leader election)
*)
val libnet_cluster : debug_var
val debug_paxos : debug_var
val debug_paxos_le : debug_var
val debug_paxos_rbr : debug_var
val debug_paxos_consensus : debug_var
val debug_paxos_sched : debug_var


(**
  {b MLSTATE_DEBUG_XML}
  Enables debug messages for Xml module (libbase)
*)
val debug_xml: debug_var

* 
    { b MLSTATE_DIFFING } 
    Try to remove as much as possible any diff not due to the input of the compiler , 
    such as date , git sha , etc ... for comparing the output of the compiler using 
    2 branches . 

    < ! > This variable is also used in [ generated_buildinfos.sh ] 

   {b MLSTATE_DIFFING}
   Try to remove as much as possible any diff not due to the input of the compiler,
   such as date, git sha, etc... for comparing the output of the compiler using
   2 branches.

   <!> This variable is also used in [generated_buildinfos.sh]
*)
val diffing : debug_var

* 
    { b MLSTATE_EFFECTS_SHOW } 
    Show the types and effects as they are being inferred by QmlEffects 

   {b MLSTATE_EFFECTS_SHOW}
   Show the types and effects as they are being inferred by QmlEffects
*)
val effects_show : debug_var

(**
  {b Explicit Instantiation debug}
*)
val expl_inst_opt_debug : debug_var
* print debug info when inserting ExplInst directives
val expl_inst_debug : debug_var
* print debug info when eliminating ExplInst directives
val expl_inst_no_memo : debug_var
* deactive sharing of types and typeschemes generated by ty_to_opaty and tsc_to_opatsc
val expl_inst_normalize : debug_var
* 0 : no normalization of type variables in types and typeschemes 
      1 : no normalization of unbound type variables 
      other : full normalization 
 
     1: no normalization of unbound type variables
     other: full normalization
 *)

(**
   print debug info when generating and inserting typename (type_def_map registration).
*)
val expl_inst_typename : debug_var

(**
   {b MLSTATE_HLNET_DEBUG}
*)
val hlnet_debug : debug_var
val hldir_debug : debug_var

* 
    { b  } 

   {b MLSTATE_HTTP_DEBUG}
*)
val http_debug : debug_var

(**
   {b MLSTATE_HTTP_NO_COOKIE}
*)
val http_no_cookie : debug_var

* 
    { b   

   {b MLSTATE_HTTP_CLIENT_DEBUG}
*)
val http_client_debug : debug_var

(**
   {b Javascript Compilation}
*)

(**
   General debug information about the qmljsimp compiler
*)
val js_imp : debug_var

(**
   Works with function name, and contains checker.
*)
val js_match_compilation : debug_var

* 
    Do not apply the ' split ' pass of JsPasses . 

   Do not apply the 'split' pass of JsPasses.
*)
val js_no_split : debug_var

(**
   Do not optimize the call with the tailcall manager.
   Used for debuging only, and getting a correct stack trace
   with firebug.
   The generated code will stack overflow in practice.
*)
val js_no_tailcall : debug_var

(**
   Debug for the local renaming of qmljsimp only
*)
val js_renaming : debug_var

(**
   Debuging the serialization of javascript from compiler to runtime.
*)
val js_serialize : debug_var

(**
   {b Lambda lifting toggles}
*)
val lambda_debug : debug_var (** random stuff toggle *)
val lambda_coerce : debug_var (** adds explicit coercions in the code to check
                                  the well-typedness of the code *)
* check that the code is well lambda lifted 
                                    this is not really a postcondition since nobody cares if 
                                    [ val f(x ) = let g(y ) = y in  ] is transformed as 
                                    [ val g(x , y ) = y val f(x ) = g(x ) ] but the lambda lifting 
                                    is not correct anyway 
                               
                                   this is not really a postcondition since nobody cares if
                                   [val f(x) = let g(y) = y in g] is transformed as
                                   [val g(x,y) = y val f(x) = g(x)] but the lambda lifting
                                   is not correct anyway
                               *)

(**
   {b MLSTATE_LOW_LEVEL_DB_LOG}
   When activated, the low level database will print disk writing related log in
   'low_level_db.log' file.
*)
val low_level_db_log : debug_var

* 
    { b  } 
    display the debug on mimetype resolution 

   {b MLSTATE_MIMETYPE_DEBUG}
   display the debug on mimetype resolution
*)
val mimetype_debug : debug_var

(**
   {b MLSTATE_MONGO_DEBUG}
   display the debug for MongoDB
*)
val mongo_debug : debug_var

(**
   {b MLSTATE_NO_ACCESS_LOG}
*)
val no_access_log : debug_var

(**
   {b MLSTATE_NO_DATABASE_UPGRADE}
   disables the database migration engine altogether,
   _assuming the db has been generated with the exact same database definition_
*)
val no_database_upgrade : debug_var

(**
   {b MLSTATE_NO_FLOOD_PREVENTION}
*)
val no_flood_prevention : debug_var

(**
   {b MLSTATE_NO_SERVER_INFO}
   Disable "starting" and "terminating" message
   when using the scheduler.
*)
val no_server_info : debug_var

(**
   {b MLSTATE_NO_FLOOD_PREVENTION}
*)
val no_flood_prevention : debug_var

(**
   {b MLSTATE_NOCACHE}
   Changes the HTTP header of responses to clients to prevent caching. Useful
   when you debug your server and re-run it often
*)
val nocache : debug_var

* 
    { b  } 
    Various printing while loading / managing object files 

   {b MLSTATE_OBJECT_DEBUG}
   Various printing while loading/managing object files
*)
val object_debug : debug_var

(**
   {b MLSTATE_OCAMLDEP_SHOW_LOGS}
   Do not logs the ocamldep output in a file, but print it on
   the console instead
*)
val ocamldep_show_logs : debug_var

(**
   {b MLSTATE_OMANAGER_debug}
   Replaces ansi escape by xml-like tags
*)
val omanager_debug : debug_var

* 
    { b MLSTATE_OPACAPI_LOOSE } 
    Do not fail if somewhere during the compilation a lookup is done via [ OpaMapToIdent ] 
    on an ident which is not defined in opacapi . 

   {b MLSTATE_OPACAPI_LOOSE}
   Do not fail if somewhere during the compilation a lookup is done via [OpaMapToIdent]
   on an ident which is not defined in opacapi.
*)
val opacapi_loose : debug_var

(**
   {b MLSTATE_OPADOC}
   Prints some information while generating opa documentation
*)
val opadoc : debug_var

* 
    { b MLSTATE_OPATOP_ANNOT } 
    Combined with MLSTATE_OPATOP_EXPR : 
    will use the annotation printer for 
    printing expressions before evaluation . 
     in case of annot not found . 

   {b MLSTATE_OPATOP_ANNOT}
   Combined with MLSTATE_OPATOP_EXPR:
   will use the annotation printer for
   printing expressions before evaluation.
   Usefull in case of annot not found.
*)
val opatop_annot : debug_var

* 
    { b MLSTATE_OPATOP_EXPR } 
    Print expressions before evaluation . 
     for using it in case of a segfault for example . 

   {b MLSTATE_OPATOP_EXPR}
   Print expressions before evaluation.
   Usefull for using it in case of a segfault for example.
*)
val opatop_expr : debug_var

(**
   {b MLSTATE_OPATOP_HOOK}
   Print hook from the source code for tracing the flow of execution.
   This variable is meant to be used for a better comprehension of the interpreter
*)
val opatop_hook : debug_var

(**
   {b MLSTATE_OPATOP_UNVALREC}
   Print the unvalrec used for evaluation.
*)
val opatop_unvalrec : debug_var


(**
   {b MLSTATE_PARSER_CACHE_DEBUG}
   Outputs whether there are cache hits or cache misses in the opa parser
*)
val parser_cache_debug : debug_var

(**
   {b MLSTATE_PATTERNS_NORMALIZE}
   Activating the pattern normalization.
*)
val patterns_normalize : debug_var

* 
    { b MLSTATE_PATTERNS_REAL_PATAS } 
    Traduces opa patas into qml patas instead 
    of performing a duplication of code . 

    This will of course be the final behavior of the compiler , 
    but currently , the PatAs is still not implemented everywhere 
    in qml , so we can play with this debug var for debuging , 
    and completing the implementation of patas in qml . 

   {b MLSTATE_PATTERNS_REAL_PATAS}
   Traduces opa patas into qml patas instead
   of performing a duplication of code.

   This will of course be the final behavior of the compiler,
   but currently, the PatAs is still not implemented everywhere
   in qml, so we can play with this debug var for debuging,
   and completing the implementation of patas in qml.
*)
val patterns_real_patas : debug_var

* 
     { b  } 
     Debug printing on ping loop system 

    {b MLSTATE_PING_DEBUG}
    Debug printing on ping loop system
*)
val ping_debug : debug_var

* 
     { b MLSTATE_BUF_DEBUG } 
     Debug printing on buf 

    {b MLSTATE_BUF_DEBUG}
    Debug printing on buf
*)
val buf_debug : debug_var

* 
   The env var { b  } 

  The env var {b MLSTATE_PPDEBUG}
*)
val ppdebug : debug_var

* 
    { b  } Debug statements from DSL - generated code . 

   {b MLSTATE_PROTOCOL_DEBUG} Debug statements from DSL-generated code.
*)
val protocol_debug : debug_var

* 
    { b MLSTATE_QMLC_NO_MAGIC } : during the cps - pass , do not insert magic functions . 
    The code may then not type check during   . 
    This env var is also used by the standard ML - back - ends . 

    This var is used essentially for debugging , to help to track 
    runtime seg - fault , because in some case it may come from a 
    bug introduced by a pass , and can be detected by the ocaml type 
    checker , but not in presence of too much magie . 

   {b MLSTATE_QMLC_NO_MAGIC} : during the cps-pass, do not insert magic functions.
   The code may then not type check during ocaml compilation.
   This env var is also used by the standard ML-back-ends.

   This var is used essentially for debugging, to help to track
   runtime seg-fault, because in some case it may come from a
   bug introduced by a pass, and can be detected by the ocaml type
   checker, but not in presence of too much magie.
*)
val qmlc_no_magic : debug_var

(**
   {b MLSTATE_QMLTOP_TIME} : in qmltop, print the timing for each top level value
   computation (typing and eval)
*)
val qmltop_time : debug_var

(**
   {b MLSTATE_REDUNDANCY}
   Sets the redundancy parameter of the distributed database
*)
val redundancy : debug_var

* 
    { b MLSTATE_REORDER } 
    Debug QmlDependencies module ( reorder passes ) 

   {b MLSTATE_REORDER}
   Debug QmlDependencies module (reorder passes)
*)
val reorder : debug_var

(**
    {b MLSTATE_RPC_DEBUG}
    [rpc_debug] Debug the Opa_InsertRemote module (ResolveRemoteCalls pass)
    [rpc_alt_skeleton] Alternative implementation of skeletons
                       Activating this flag factorises the code produced
                       and should improve compilation time with neglectable
                       runtime hit
*)
val rpc_debug : debug_var
val rpc_alt_skeleton : debug_var

* 
    { b Debug on SurfaceAst passes } 

   {b Debug on SurfaceAst passes}
*)
val sa_dependencies : debug_var (** for the debug of the reordering algorithm *)
val sa_printer_annot : debug_var (** to print the annotations while printing asts *)
val sa_printer_ty : debug_var (** to print the directive types while printing asts *)
val sa_trx : debug_var (** print the code of the parsers after they were rewritten *)
val sa_xml_pattern : debug_var (** print the ast of the xml patterns that is being rewritten *)

* 
    { b MLSTATE_SESSION_DEBUG } 

   {b MLSTATE_SESSION_DEBUG}
*)
val session_debug : debug_var

(**
   {b MLSTATE_SCHEDULER_DEBUG}
*)
val scheduler_debug : debug_var

(**
   {b MLSTATE_SERVER_SERIALIZE}
*)
val server_serialize : debug_var

(**
   {b MLSTATE_SERVER_STATS}
   Controls whether to (collect and) print server statistics upon
   server termination.
*)
val server_stats : debug_var

(**
   {b MLSTATE_SHOW_LOGS}
   If set, all Logger logs will be output on stderr
   Consequently, no logs will be output in files
*)
val show_logs : debug_var (** show all logs on stderr *)

(**
   {b Pass_SimplifyMagic}
*)
val simplifymagic_disable : debug_var (** don't specialize any identifier *)
val simplifymagic_failures : debug_var (** show specializable identifiers that weren't specialized *)

(**
  {b Slicer debug}
*)
val slicer_cond : debug_var (** checks slicer precondition and postcondition *)
val slicer_debug : debug_var (** ?? *)
val slicer_time : debug_var (** shows where the time of the slicing pass is spent *)

val no_async : debug_var (** no async rewriting *)

(**
   {b MLSTATE_SSL_DEBUG}
*)
val ssl_debug : debug_var

(**
   {b MLSTATE_TESTING}
   This is set by the reftester. Only use to disable the printing of some
   messages that are normally useful but very troublesome for the stability
   of refs (eg. database locations...).
*)
val testing : debug_var

* 
    { b MLSTATE_TYPER } 
    Works with level . 
    Debugging the type checker . 

   {b MLSTATE_TYPER}
   Works with level.
   Debugging the type checker.
*)
val typer : debug_var

(**
   {b MLSTATE_RESOURCE_TRACKER_DEBUG}
*)
val resource_tracker_debug : debug_var


* 
    { b  } 

   {b MLSTATE_WEBLIB_DEBUG}
*)
val weblib_debug : debug_var (** takes a level *)

(**
   {b BADOP_XML_IMPORT_COMMIT_TRANSACTION_STEP}
   Only used by db3/Xml_import, to decide the frequency of commits.
*)
val badop_xml_import_commit_transaction_step : debug_var


* { 6 Variables Testers }

(**
   Variables tester are used to extend the macro [#<If>] of the ppdebug.
   You can acces them by using the following syntax :
   [#<If:MYVAR $tester args>]
   where args is some optional arguments.

   The meaning of this is that the corresponding tester is applied to all the
   given arguments, and finally to the value of the debug_var,
   returning a bool indicating what part of the control structure should
   be executed.

   Note :
   You can also used inline tester in your code, but it is not recommanded (remember,
   that must fit on the same line)
*)

(**
   The type of all testers
*)
type debug_tester = debug_var -> bool

(** the default tester.
    [#<If:MYVAR>] : returns [true] if and only if the variable is defined and doesn't have value "0" *)
val default : debug_tester

(**
   the negation of the default tester.
   [#<If:MYVAR $null>] : returns [true] if and only if the variable is undefined or have value "0"
*)
val null : debug_tester

(** the defined tester.
    [#<If:MYVAR$defined>] : returns [true] if and only if the variable is defined *)
val defined : debug_tester

(**
   the undefined tester.
   [#<If:MYVAR $undefined>] : returns [true] if and only if the variable is undefined
*)
val undefined : debug_tester

(** the toggle tester.
    [#<If:MYVAR$toggle>] : returns [true] if and only if the variable is set to ["1"] *)
val toggle : debug_tester

(** the equals tester.
    [#<If:MYVAR $equals "VALUE">] : returns [true] if and only if the variable is set to ["VALUE"] *)
val equals : string -> debug_tester

(** the level tester :
    [#<If:MYVAR $level fct>] : returns [true] if and only if the variable is set to an int
    value [n], and [fct n] returns [true] *)
val level : (int -> bool) -> debug_tester

* 
    A specific level . 
    [ # < If : MYVAR $ islevel 42 > ] : returns [ true ] if and only if the variable is set to " 42 " 

   A specific level.
   [#<If:MYVAR $islevel 42>] : returns [true] if and only if the variable is set to "42"
*)
val islevel : int -> debug_tester

(** the maxlevel tester :
    [#<If:MYVAR $maxlevel n>] : like [#<If: MYVAR $level (fun i -> i <= n)>] *)
val maxlevel : int -> debug_tester

(** the minlevel tester :
    [#<If:MYVAR $minlevel n>] : like [#<If: MYVAR $level (fun i -> i >= n)>] *)
val minlevel : int -> debug_tester

(** the contains tester :
    checks that the given string is included in the environment variable *)
val contains : string -> debug_tester

(**
   free debug_tester, for complex conditions
*)
val cont : debug_tester -> debug_tester

(** checks that the environment variable is included in the given string *)
val is_contained : string -> debug_tester

(** contents of the variable is taken as a comma-separated list; checks if
    the given flag exists in this list *)
val flag : string -> debug_tester

 utilisation 
*
   The preprocessor {b ppdebug} is used whenever you want to add logs or computation
   in your application (assert, etc...), but you do not want the code to be embedded in release mode.

   {[
   let g =
       #<Debug>
         print_endline "this is a log" ;
       #<End>
       5
   ]}

   will be either
   {[
   let g =
(*        #<Debug> 
        #<End> 
        #<Debug> 

         print_endline "this is a log" ;

        #<End> 
       #<If> 
       #<End> 
       #<End> 
       #<If> 
       #<End> 
*
   The type of all debug_var.
   [None] if the variable is not defined, [Some value] if there is a value.

   <!> Env variables are checked only once, when this module is loaded.

*
   {b BADOP_DEBUG}
   Adds some experimental options to servers related to badop modes.

*
   {b MLSTATE_BSL_LOADING}
   Use with level.
   Be more verbose during loading.
   1: plugins
   *: undocumented

*
   {b MLSTATE_BSL_REGISTER}
   Use with level. Be more verbose during building maps, and dealing
   with keys. Used during bslregister.

*
   {b MLSTATE_BUFFER_POOL_ENABLE}

* {9 Closure} 
* generate optimized applyX functions 
* display a few statistics in the passes uncurry/closure 
*
   Debug vars used for activating/desactivating some sharing during the passes client/server
   constant sharing. Variable are read only if the pass is activated.

*
   {b MLSTATE_CPS_KEEP_LETCONT} :
   do not remove continuation directly applied. Used just for benchmark purpose.

*
   {b MLSTATE_CPS_NOSKIP} :
   do not use the skip optimisation. Used just for benchmark and tracability purpose.

*
   {b MLSTATE_STACK_TRACE}
   When set, activates the printing of backtraces
   If it contains "tr", then transaction contexts are printed
   If it contains "th", then thread contexts are printed
   If it contains "args", then fonction arguments are printed

*
   {b MLSTATE_DB3_NO_FINAL_SNAPSHOT}
   When activated, when the database is closed, we do not make a snapshot of the current revision,
   meaning that the transaction file will not be empty, and pending transaction reapplied at
   reopen time.
   This is used for debugging the binary format of transaction (or queries) because if we
   always use a snapshot when we close the database, the transaction binary file is always empty.

*
   {b DbGen obscure toggles}

* run-time 
* Force upgrade of incompatible databases when they are opened 
* When updating a field from an old db is not possible, remove and re-create 
* Enables some compile-time debug messages, adds debug prints in the
                                generated code and, at run-time, dbGen-related debug messages 
* compile-time only: 
*
   {b MLSTATE_DEBUG_DB}
   Enables debug messages from the low-level database
   You can specify :
     - a debug level (int): to display low level db debug
     - 'migration': to display only migration debug
     - 'io': to display only io debug


*
   {b MLSTATE_DEBUG_DB_INDEX}
   Enables debug messages from the index handling in the database

*
   {b MLSTATE_DEBUG_DB_MAX_DELTA}
   How many delta nodes are permitted between full nodes; 0 means all are full.

*
  {b MLSTATE_DEBUG_XML}
  Enables debug messages for Xml module (libbase)

*
  {b Explicit Instantiation debug}

*
   print debug info when generating and inserting typename (type_def_map registration).

*
   {b MLSTATE_HLNET_DEBUG}

*
   {b MLSTATE_HTTP_NO_COOKIE}

*
   {b Javascript Compilation}

*
   General debug information about the qmljsimp compiler

*
   Works with function name, and contains checker.

*
   Do not optimize the call with the tailcall manager.
   Used for debuging only, and getting a correct stack trace
   with firebug.
   The generated code will stack overflow in practice.

*
   Debug for the local renaming of qmljsimp only

*
   Debuging the serialization of javascript from compiler to runtime.

*
   {b Lambda lifting toggles}

* random stuff toggle 
* adds explicit coercions in the code to check
                                  the well-typedness of the code 
*
   {b MLSTATE_LOW_LEVEL_DB_LOG}
   When activated, the low level database will print disk writing related log in
   'low_level_db.log' file.

*
   {b MLSTATE_MONGO_DEBUG}
   display the debug for MongoDB

*
   {b MLSTATE_NO_ACCESS_LOG}

*
   {b MLSTATE_NO_DATABASE_UPGRADE}
   disables the database migration engine altogether,
   _assuming the db has been generated with the exact same database definition_

*
   {b MLSTATE_NO_FLOOD_PREVENTION}

*
   {b MLSTATE_NO_SERVER_INFO}
   Disable "starting" and "terminating" message
   when using the scheduler.

*
   {b MLSTATE_NO_FLOOD_PREVENTION}

*
   {b MLSTATE_NOCACHE}
   Changes the HTTP header of responses to clients to prevent caching. Useful
   when you debug your server and re-run it often

*
   {b MLSTATE_OCAMLDEP_SHOW_LOGS}
   Do not logs the ocamldep output in a file, but print it on
   the console instead

*
   {b MLSTATE_OMANAGER_debug}
   Replaces ansi escape by xml-like tags

*
   {b MLSTATE_OPADOC}
   Prints some information while generating opa documentation

*
   {b MLSTATE_OPATOP_HOOK}
   Print hook from the source code for tracing the flow of execution.
   This variable is meant to be used for a better comprehension of the interpreter

*
   {b MLSTATE_OPATOP_UNVALREC}
   Print the unvalrec used for evaluation.

*
   {b MLSTATE_PARSER_CACHE_DEBUG}
   Outputs whether there are cache hits or cache misses in the opa parser

*
   {b MLSTATE_PATTERNS_NORMALIZE}
   Activating the pattern normalization.

*
   {b MLSTATE_QMLTOP_TIME} : in qmltop, print the timing for each top level value
   computation (typing and eval)

*
   {b MLSTATE_REDUNDANCY}
   Sets the redundancy parameter of the distributed database

*
    {b MLSTATE_RPC_DEBUG}
    [rpc_debug] Debug the Opa_InsertRemote module (ResolveRemoteCalls pass)
    [rpc_alt_skeleton] Alternative implementation of skeletons
                       Activating this flag factorises the code produced
                       and should improve compilation time with neglectable
                       runtime hit

* for the debug of the reordering algorithm 
* to print the annotations while printing asts 
* to print the directive types while printing asts 
* print the code of the parsers after they were rewritten 
* print the ast of the xml patterns that is being rewritten 
*
   {b MLSTATE_SCHEDULER_DEBUG}

*
   {b MLSTATE_SERVER_SERIALIZE}

*
   {b MLSTATE_SERVER_STATS}
   Controls whether to (collect and) print server statistics upon
   server termination.

*
   {b MLSTATE_SHOW_LOGS}
   If set, all Logger logs will be output on stderr
   Consequently, no logs will be output in files

* show all logs on stderr 
*
   {b Pass_SimplifyMagic}

* don't specialize any identifier 
* show specializable identifiers that weren't specialized 
*
  {b Slicer debug}

* checks slicer precondition and postcondition 
* ?? 
* shows where the time of the slicing pass is spent 
* no async rewriting 
*
   {b MLSTATE_SSL_DEBUG}

*
   {b MLSTATE_TESTING}
   This is set by the reftester. Only use to disable the printing of some
   messages that are normally useful but very troublesome for the stability
   of refs (eg. database locations...).

*
   {b MLSTATE_RESOURCE_TRACKER_DEBUG}

* takes a level 
*
   {b BADOP_XML_IMPORT_COMMIT_TRANSACTION_STEP}
   Only used by db3/Xml_import, to decide the frequency of commits.

*
   Variables tester are used to extend the macro [#<If>] of the ppdebug.
   You can acces them by using the following syntax :
   [#<If:MYVAR $tester args>]
   where args is some optional arguments.

   The meaning of this is that the corresponding tester is applied to all the
   given arguments, and finally to the value of the debug_var,
   returning a bool indicating what part of the control structure should
   be executed.

   Note :
   You can also used inline tester in your code, but it is not recommanded (remember,
   that must fit on the same line)

*
   The type of all testers

* the default tester.
    [#<If:MYVAR>] : returns [true] if and only if the variable is defined and doesn't have value "0" 
*
   the negation of the default tester.
   [#<If:MYVAR $null>] : returns [true] if and only if the variable is undefined or have value "0"

* the defined tester.
    [#<If:MYVAR$defined>] : returns [true] if and only if the variable is defined 
*
   the undefined tester.
   [#<If:MYVAR $undefined>] : returns [true] if and only if the variable is undefined

* the toggle tester.
    [#<If:MYVAR$toggle>] : returns [true] if and only if the variable is set to ["1"] 
* the equals tester.
    [#<If:MYVAR $equals "VALUE">] : returns [true] if and only if the variable is set to ["VALUE"] 
* the level tester :
    [#<If:MYVAR $level fct>] : returns [true] if and only if the variable is set to an int
    value [n], and [fct n] returns [true] 
* the maxlevel tester :
    [#<If:MYVAR $maxlevel n>] : like [#<If: MYVAR $level (fun i -> i <= n)>] 
* the minlevel tester :
    [#<If:MYVAR $minlevel n>] : like [#<If: MYVAR $level (fun i -> i >= n)>] 
* the contains tester :
    checks that the given string is included in the environment variable 
*
   free debug_tester, for complex conditions

* checks that the environment variable is included in the given string 
* contents of the variable is taken as a comma-separated list; checks if
    the given flag exists in this list 

     Copyright © 2011 , 2012 MLstate 

     This file is part of  . 

      is free software : you can redistribute it and/or modify it under the 
     terms of the GNU Affero General Public License , version 3 , as published by 
     the Free Software Foundation . 

      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 Affero General Public License for 
     more details . 

     You should have received a copy of the GNU Affero General Public License 
     along with  . If not , see < / > . 

    Copyright © 2011, 2012 MLstate

    This file is part of Opa.

    Opa is free software: you can redistribute it and/or modify it under the
    terms of the GNU Affero General Public License, version 3, as published by
    the Free Software Foundation.

    Opa 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 Affero General Public License for
    more details.

    You should have received a copy of the GNU Affero General Public License
    along with Opa. If not, see </>.
*)

* 
    Debug Variables Management for the MLstate debug preprocessor ( ppdebug ) 

    @see " opalang / utils / ppdebug.pl " the script which implement the preprocess 
    @author   
    @author   

   Debug Variables Management for the MLstate debug preprocessor (ppdebug)

   @see "opalang/utils/ppdebug.pl" the script which implement the preprocess
   @author Louis Gesbert
   @author Mathieu Barbin
*)

* { 6 Template }

* 
    Template for a module name [ MyId ] 
    { [ 
    # < Debugvar : MY_VAR > 
    let debug fmt = 
      OManager.printf ( " @{<cyan>[MyId]@}@ @[<2>"^^fmt^^"@]@. " ) 

    ( * utilisation
   Template for a module name [MyId]
   {[
   #<Debugvar:MY_VAR>
   let debug fmt =
     OManager.printf ("@{<cyan>[MyId]@}@ @[<2>"^^fmt^^"@]@.")

   ...
   let _ =
     #<If:MY_VAR $minlevel 4>
       debug "do some %s of level %d@\n" "debug" 4
     #<End>
   in
   ...
   ]}
*)

* { 6 Static Debug Preprocessor -- Syntax Documentation }

         print_endline "this is a log" ;
       5
   ]}
   or
   {[
   let g =
       5
   ]}
   depending of the env var {b MLSTATE_PPDEBUG}, or on the tag used with ocamlbuild.

   Note that you can also have a different code instead of nothing, using [#else]
   {[
   let g =
       #<Debug>
         print_endline "this is BIG BIG LOG" ;
         print_endline "with so many details that I'd like to keep it only in debug mode" ;
       #<Else>
         print_endline "short log" ;
       #<End>
       5
   ]}

   <!> The code should be totally recompiled with/without option PPDEBUG to make changes be taken in consideration.
   For dynamic toggle behavior, cf the next part.

   For other compile-time checks (eg. target-dependent code), you can use the directive [#<Ifstatic:VAR REGEXP>].
   A check will be done by the preprocessor on environment variable [VAR] with the regexp [REGEXP], and comment
   the unappropriate code:

   {[
   let build_arch =
       #<Ifstatic:ARCH Win.*> "Windows" #<Else> "Other" #<End>
   ]}

   will define [build_arch] to be ["Windows"] whenever a variable [ARCH] is defined with a value beginning with
   ["Win"]
*)

* { 6 Dynamic Debug Preprocessor -- Syntax Documentation }

* 
    This preprocessor allows you to do some extra logs and computation without recompiling 
    everything with a different preprocess configuration , but assures you that your code will 
    not be embeded in release mode . 

    { [ 
    let g = 
       # < If : MY_SPLENDID_VARIABLE > 
          print_endline " this is a log " 
       # < End > ; 
       5 
    ] } 

    will be transformed in : 
    { [ 
    let g = 
 ( *        # < If >
   This preprocessor allows you to do some extra logs and computation without recompiling
   everything with a different preprocess configuration, but assures you that your code will
   not be embeded in release mode.

   {[
   let g =
      #<If:MY_SPLENDID_VARIABLE>
         print_endline "this is a log"
      #<End> ;
      5
   ]}

   will be transformed in :
   {[
   let g =
         print_endline "this is a log"
      5
   ]}
   or if release-mode is activated :
   {[
   let g =
  if ( DebugVariables.default )  then begin 
          print_endline " this is a log " 
 ( *        # < End >
         print_endline "this is a log"
      5
   ]}

   Note that the [;] at the end is kept: the ppdebug-[#<If>] should be an ocaml expression.

   You can find in this file all documented variables available.
   You should add your variables there, and documenting it following the guidelines.

   Extension of the test condition :

   By default, we check that the env var is defined and doesn't have value ["0"] (test is [DebugVariables.default]).
   You can use a different test using the following syntax :

   {[
   let g =
      #<If:MY_SPLENDID_VARIABLE $equals "MY_SPLENDID_VALUE">
         print_endline "this is a log"
      #<End> ;
      5
   ]}

   processed as :

   {[
   let  =
         print_endline "this is a log"
      5
   ]}

   Matching on debug variables :

   If you have a multi-state debug variable, you can add a runtime-match as long as it's enclosed
   in a [#<If$defined>] :

   {[
   let verb x =
     #<If:MY_VERBOSE$defined> match DebugVariables.my_verbose with
       | Some "quite verbose" -> print_debug x
       | Some "very verbose" -> print_debug_very_verbose x
       | _ -> failwith "Bad value for debug variable MY_VERBOSE"
     #<End>
   ]}

   Warnings :
   + <!> This preprocessor should not be used for top-level definitions.
   + <!> Such a preprocessed part is a Ocaml.expr, necessary of type [unit] if there is no [#<Else>]
   + <!> Remember that in release, you're always in the Else case. Keep this for debug !

   You can find some tester available in this module.
   You can add any tester you want editing this module, and following guidelines.
*)


* { 6 Variables }

* 
    When you do lots of tests on the same debug - variable , you can use the shortcut : 

    { [ 
    # < Debugvar : VAR > 
    ] } 

    The [: VAR ] in subsequent uses of [ # < If > ] can be omitted . 

    Guidelines : 
       + all variables are documented in alphabetic order ! ! 
       + all variable begin with { e MLSTATE _ } when you export it in shell 
       + all variable are UPPERCASED 
       + all corresponding variable name in this file are lowercased and does not contains MLSTATE _ 

   When you do lots of tests on the same debug-variable, you can use the shortcut:

   {[
   #<Debugvar:VAR>
   ]}

   The [:VAR] in subsequent uses of [#<If>] can be omitted.

   Guidelines :
      + all variables are documented in alphabetic order !!
      + all variable begin with {e MLSTATE_} when you export it in shell
      + all variable are UPPERCASED
      + all corresponding variable name in this file are lowercased and does not contains MLSTATE_
*)

type debug_var = string option

* 
    { b MLSTATE_ADD_STDLIB } 
    Be verbose to explain what files are added , and what files are rejected 
    during the pass [  ] , in presence and in absence of option 
    [ --no - embedded - stdlib ] 

    Level 1 : print only rejected files 
    Level 2 : print only keeped files 
    Level 3 : print both 

   {b MLSTATE_ADD_STDLIB}
   Be verbose to explain what files are added, and what files are rejected
   during the pass [AddStdlibFiles], in presence and in absence of option
   [--no-embedded-stdlib]

   Level 1: print only rejected files
   Level 2: print only keeped files
   Level 3: print both
*)
val add_stdlib : debug_var

val badop_debug : debug_var

val bsl_loading : debug_var

* 
    { b  } 
    When set , disables restriction checks on bypasses . 
    Useful when recompiling printed code containing 
    such bypasses . 

   {b MLSTATE_BSL_NO_RESTRICTION}
   When set, disables restriction checks on bypasses.
   Useful when recompiling printed code containing
   such bypasses.
*)
val bsl_no_restriction : debug_var

* 
    { b MLSTATE_BSL_PROJECTION } 
    Use with level , print some debug logs about external bypass 
    projection . ( libbsl , backend , qmltop , etc ... ) 

   {b MLSTATE_BSL_PROJECTION}
   Use with level, print some debug logs about external bypass
   projection. (libbsl, backend, qmltop, etc...)
*)
val bsl_projection : debug_var

val bsl_register : debug_var

* 
    { b MLSTATE_BSL_SL } 
    Use with level . Be more verbose during some operations implemented 
    in the ServerLib . 
    This happens at runtime , during projections . 

   {b MLSTATE_BSL_SL}
   Use with level. Be more verbose during some operations implemented
   in the ServerLib.
   This happens at runtime, during projections.
*)
val bsl_sl : debug_var

* 
    { b MLSTATE_BYPASS_HOISTING } 
    insert debug message during the bypass hoisting pass . Works with level . 
    Level 1 : 
    Level 2 : 

   {b MLSTATE_BYPASS_HOISTING}
   insert debug message during the bypass hoisting pass. Works with level.
   Level 1 :
   Level 2 :
*)
val bypass_hoisting : debug_var

* 
    { b  } 

   {b MLSTATE_BUFFER_POOL_DEBUG}
*)
val buffer_pool_debug : debug_var

val buffer_pool_enable : debug_var


* 
    One variable to check them all ! 

    If you do nt spell correctly a debug variable by exporting it before running your application , 
    you may be surprised that the execution has not the expected behavior , and you may loose time until 
    you notice that it is because of a variable not correctly defined . 

    If ( and only if ) [ " MLSTATE_CHECK_VARS " ] is set with a value different than [ " 0 " ] , 
    this module will do a check on the global process environment . 
    For each env variable starting with [ " MLSTATE _ " ] which are not defined ( and documented :)) 
    in this module , a warning will be printed on the standard error . 

    Strong mode : [ MLSTATE_CHECK_VARS="s " ] will make an error rather than a warning , stopping the execution . 

   One variable to check them all !

   If you dont spell correctly a debug variable by exporting it before running your application,
   you may be surprised that the execution has not the expected behavior, and you may loose time until
   you notice that it is because of a variable not correctly defined.

   If (and only if) ["MLSTATE_CHECK_VARS"] is set with a value different than ["0"],
   this module will do a check on the global process environment.
   For each env variable starting with ["MLSTATE_"] which are not defined (and documented :))
   in this module, a warning will be printed on the standard error.

   Strong mode : [MLSTATE_CHECK_VARS="s"] will make an error rather than a warning, stopping the execution.
*)
val check_vars : debug_var


* 
    { b  } 
    Works only for caml bypasses ( with  only , but this could be fixed ) 
    Level 0 : runtime assertions were typing is not possible 
              - qml closures actually have a compatible structure 
              - imported functions must be exported closures 
    Level 1 : previous + 
              tells when applying and receiving magic arguments ( to check bsl projections ) 
    Level 2 : previous + 
              tells when applying closures to arguments and show the resulting value ( very verbose ) 

   {b MLSTATE_CLOSURE_DEBUG}
   Works only for caml bypasses (with qmlflat only, but this could be fixed)
   Level 0 : runtime assertions were typing is not possible
             - qml closures actually have a compatible structure
             - imported functions must be exported closures
   Level 1 : previous +
             tells when applying and receiving magic arguments (to check bsl projections)
   Level 2 : previous +
             tells when applying closures to arguments and show the resulting value (very verbose)
*)
val closure_debug : debug_var

* { 9   }


val const_sharing_client_float : debug_var
val const_sharing_client_record : debug_var
val const_sharing_client_remove_coerce : debug_var
val const_sharing_client_string : debug_var

val const_sharing_server_float : debug_var
val const_sharing_server_record : debug_var
val const_sharing_server_remove_coerce : debug_var
val const_sharing_server_string : debug_var

* { 9 Cps }

* 
    { b MLSTATE_CPS_DEBUG } : 
    insert debug introspection message to trace the execution flow of continuations . 

    < ! > Note that CPS_DEBUG env var should also be set at runtime of the compiled 
    application for logs to be produced , with a given level . 

    < ! > At compile time , the level is not read , everything is generated . 

    { [ 
    export  
    // compile 
    // run 
    unset   MLSTATE_CPS_DEBUG 
    // run without debug logs 
    // compile 
    // run without debug logs 
    export  
    // run without debug logs ( need a recompilation ) 
    ] } 

    This var is used with increasing levels from 1 to 100 : 
    + 1   : print a few things 
    + 10 : cont tracer ( print every cont application ) 
    + 100 : backtraces display function arguments ( WIP ) 

   {b MLSTATE_CPS_DEBUG} :
   insert debug introspection message to trace the execution flow of continuations.

   <!> Note that CPS_DEBUG env var should also be set at runtime of the compiled
   application for logs to be produced, with a given level.

   <!> At compile time, the level is not read, everything is generated.

   {[
   export MLSTATE_CPS_DEBUG=1
   // compile
   // run
   unset  MLSTATE_CPS_DEBUG
   // run without debug logs
   // compile
   // run without debug logs
   export MLSTATE_CPS_DEBUG=1
   // run without debug logs (need a recompilation)
   ]}

   This var is used with increasing levels from 1 to 100 :
   + 1  : print a few things
   + 10 : cont tracer (print every cont application)
   + 100: backtraces display function arguments (WIP)
*)
val cps_debug : debug_var

val cps_keep_letcont : debug_var

val cps_noskip : debug_var

val cps_stack_trace : debug_var

* 
    { b MLSTATE_CPS_VERBOSE } : 
    compile time only : let the cps rewriting pass be more verbose . Used with level . 
    No changes to the generated code , affect only the transformation pass . 
    + Level 1 : 
                + print some timing 
    + Level 2 : 
                + previous 
                + print optimisation logs 

   {b MLSTATE_CPS_VERBOSE} :
   compile time only : let the cps rewriting pass be more verbose. Used with level.
   No changes to the generated code, affect only the transformation pass.
   + Level 1 :
               + print some timing
   + Level 2 :
               + previous
               + print optimisation logs
*)
val cps_verbose : debug_var

* 
    { b  } : 
    Print blocking stack 

   {b MLSTATE_CPS_BLOCKING_WAIT} :
   Print blocking stack
*)
val cps_blocking_wait : debug_var

* { b MLSTATE_DATABASE_RECONNECT } 
   Set up a delay in seconds for automatic reconnection when a database server is lost . 
   ( if unset , never reconnect automatically ) 

  Set up a delay in seconds for automatic reconnection when a database server is lost.
  (if unset, never reconnect automatically)
*)
val database_reconnect : debug_var

val db3_no_final_snapshot : debug_var

* 
    { b MLSTATE_DB3_TRANSACTION_LIMIT } 
    Defines a limit of operations within a transaction , after which it is automatically aborted . 
    Default is 10000 . This is a safeguard against exploding the memory by spamming a 
    transaction with too many writes . 0 disables the limit . 

   {b MLSTATE_DB3_TRANSACTION_LIMIT}
   Defines a limit of operations within a transaction, after which it is automatically aborted.
   Default is 10000. This is a safeguard against exploding the memory by spamming a
   transaction with too many writes. 0 disables the limit.
*)
val db3_transaction_limit : debug_var


val dbgen_flags : debug_var
* comma - separated list of flags for DbGen , compile - time : 
       - ' fullcoerce ' : put coerces almost everywhere . Useful for checks 
       - ' source ' : instead of normal fresh identifiers , insert " source " textual ones in 
                   AST . Useful to manual checks from toplevel . 
       - ' nodefault ' : do n't complain about missing default values , use by - default defaults . 
       - ' nolazy ' : disable lazy records 
       - ' sharing ' : enable full - sharing in the DB ( enabled in db tests , temporary 
                    disabled for now to workaround a bug in the low - level implementation 
                    of copy - on - write ) 
       - ' noflatten ' : do n't flatten recursive types , store them as deep trees 
       - ' copylink ' : when flattening recursive types , link them with internal copy - nodes instead of link - nodes 
  
      - 'fullcoerce': put coerces almost everywhere. Useful for checks
      - 'source': instead of normal fresh identifiers, insert "source" textual ones in
                  AST. Useful to manual checks from toplevel.
      - 'nodefault': don't complain about missing default values, use by-default defaults.
      - 'nolazy': disable lazy records
      - 'sharing': enable full-sharing in the DB (enabled in db tests, temporary
                   disabled for now to workaround a bug in the low-level implementation
                   of copy-on-write)
      - 'noflatten': don't flatten recursive types, store them as deep trees
      - 'copylink': when flattening recursive types, link them with internal copy-nodes instead of link-nodes
  *)


val debug_db : debug_var

val debug_db_index : debug_var

val debug_db_max_delta : debug_var

* 
    { b MLSTATE_DEBUG_PAXOS } 
    Enables debug messages for the Paxos algorithm ( including leader election ) 

   {b MLSTATE_DEBUG_PAXOS}
   Enables debug messages for the Paxos algorithm (including leader election)
*)
val libnet_cluster : debug_var
val debug_paxos : debug_var
val debug_paxos_le : debug_var
val debug_paxos_rbr : debug_var
val debug_paxos_consensus : debug_var
val debug_paxos_sched : debug_var


val debug_xml: debug_var

* 
    { b MLSTATE_DIFFING } 
    Try to remove as much as possible any diff not due to the input of the compiler , 
    such as date , git sha , etc ... for comparing the output of the compiler using 
    2 branches . 

    < ! > This variable is also used in [ generated_buildinfos.sh ] 

   {b MLSTATE_DIFFING}
   Try to remove as much as possible any diff not due to the input of the compiler,
   such as date, git sha, etc... for comparing the output of the compiler using
   2 branches.

   <!> This variable is also used in [generated_buildinfos.sh]
*)
val diffing : debug_var

* 
    { b MLSTATE_EFFECTS_SHOW } 
    Show the types and effects as they are being inferred by QmlEffects 

   {b MLSTATE_EFFECTS_SHOW}
   Show the types and effects as they are being inferred by QmlEffects
*)
val effects_show : debug_var

val expl_inst_opt_debug : debug_var
* print debug info when inserting ExplInst directives
val expl_inst_debug : debug_var
* print debug info when eliminating ExplInst directives
val expl_inst_no_memo : debug_var
* deactive sharing of types and typeschemes generated by ty_to_opaty and tsc_to_opatsc
val expl_inst_normalize : debug_var
* 0 : no normalization of type variables in types and typeschemes 
      1 : no normalization of unbound type variables 
      other : full normalization 
 
     1: no normalization of unbound type variables
     other: full normalization
 *)

val expl_inst_typename : debug_var

val hlnet_debug : debug_var
val hldir_debug : debug_var

* 
    { b  } 

   {b MLSTATE_HTTP_DEBUG}
*)
val http_debug : debug_var

val http_no_cookie : debug_var

* 
    { b   

   {b MLSTATE_HTTP_CLIENT_DEBUG}
*)
val http_client_debug : debug_var


val js_imp : debug_var

val js_match_compilation : debug_var

* 
    Do not apply the ' split ' pass of JsPasses . 

   Do not apply the 'split' pass of JsPasses.
*)
val js_no_split : debug_var

val js_no_tailcall : debug_var

val js_renaming : debug_var

val js_serialize : debug_var

* check that the code is well lambda lifted 
                                    this is not really a postcondition since nobody cares if 
                                    [ val f(x ) = let g(y ) = y in  ] is transformed as 
                                    [ val g(x , y ) = y val f(x ) = g(x ) ] but the lambda lifting 
                                    is not correct anyway 
                               
                                   this is not really a postcondition since nobody cares if
                                   [val f(x) = let g(y) = y in g] is transformed as
                                   [val g(x,y) = y val f(x) = g(x)] but the lambda lifting
                                   is not correct anyway
                               *)

val low_level_db_log : debug_var

* 
    { b  } 
    display the debug on mimetype resolution 

   {b MLSTATE_MIMETYPE_DEBUG}
   display the debug on mimetype resolution
*)
val mimetype_debug : debug_var

val mongo_debug : debug_var

val no_access_log : debug_var

val no_database_upgrade : debug_var

val no_flood_prevention : debug_var

val no_server_info : debug_var

val no_flood_prevention : debug_var

val nocache : debug_var

* 
    { b  } 
    Various printing while loading / managing object files 

   {b MLSTATE_OBJECT_DEBUG}
   Various printing while loading/managing object files
*)
val object_debug : debug_var

val ocamldep_show_logs : debug_var

val omanager_debug : debug_var

* 
    { b MLSTATE_OPACAPI_LOOSE } 
    Do not fail if somewhere during the compilation a lookup is done via [ OpaMapToIdent ] 
    on an ident which is not defined in opacapi . 

   {b MLSTATE_OPACAPI_LOOSE}
   Do not fail if somewhere during the compilation a lookup is done via [OpaMapToIdent]
   on an ident which is not defined in opacapi.
*)
val opacapi_loose : debug_var

val opadoc : debug_var

* 
    { b MLSTATE_OPATOP_ANNOT } 
    Combined with MLSTATE_OPATOP_EXPR : 
    will use the annotation printer for 
    printing expressions before evaluation . 
     in case of annot not found . 

   {b MLSTATE_OPATOP_ANNOT}
   Combined with MLSTATE_OPATOP_EXPR:
   will use the annotation printer for
   printing expressions before evaluation.
   Usefull in case of annot not found.
*)
val opatop_annot : debug_var

* 
    { b MLSTATE_OPATOP_EXPR } 
    Print expressions before evaluation . 
     for using it in case of a segfault for example . 

   {b MLSTATE_OPATOP_EXPR}
   Print expressions before evaluation.
   Usefull for using it in case of a segfault for example.
*)
val opatop_expr : debug_var

val opatop_hook : debug_var

val opatop_unvalrec : debug_var


val parser_cache_debug : debug_var

val patterns_normalize : debug_var

* 
    { b MLSTATE_PATTERNS_REAL_PATAS } 
    Traduces opa patas into qml patas instead 
    of performing a duplication of code . 

    This will of course be the final behavior of the compiler , 
    but currently , the PatAs is still not implemented everywhere 
    in qml , so we can play with this debug var for debuging , 
    and completing the implementation of patas in qml . 

   {b MLSTATE_PATTERNS_REAL_PATAS}
   Traduces opa patas into qml patas instead
   of performing a duplication of code.

   This will of course be the final behavior of the compiler,
   but currently, the PatAs is still not implemented everywhere
   in qml, so we can play with this debug var for debuging,
   and completing the implementation of patas in qml.
*)
val patterns_real_patas : debug_var

* 
     { b  } 
     Debug printing on ping loop system 

    {b MLSTATE_PING_DEBUG}
    Debug printing on ping loop system
*)
val ping_debug : debug_var

* 
     { b MLSTATE_BUF_DEBUG } 
     Debug printing on buf 

    {b MLSTATE_BUF_DEBUG}
    Debug printing on buf
*)
val buf_debug : debug_var

* 
   The env var { b  } 

  The env var {b MLSTATE_PPDEBUG}
*)
val ppdebug : debug_var

* 
    { b  } Debug statements from DSL - generated code . 

   {b MLSTATE_PROTOCOL_DEBUG} Debug statements from DSL-generated code.
*)
val protocol_debug : debug_var

* 
    { b MLSTATE_QMLC_NO_MAGIC } : during the cps - pass , do not insert magic functions . 
    The code may then not type check during   . 
    This env var is also used by the standard ML - back - ends . 

    This var is used essentially for debugging , to help to track 
    runtime seg - fault , because in some case it may come from a 
    bug introduced by a pass , and can be detected by the ocaml type 
    checker , but not in presence of too much magie . 

   {b MLSTATE_QMLC_NO_MAGIC} : during the cps-pass, do not insert magic functions.
   The code may then not type check during ocaml compilation.
   This env var is also used by the standard ML-back-ends.

   This var is used essentially for debugging, to help to track
   runtime seg-fault, because in some case it may come from a
   bug introduced by a pass, and can be detected by the ocaml type
   checker, but not in presence of too much magie.
*)
val qmlc_no_magic : debug_var

val qmltop_time : debug_var

val redundancy : debug_var

* 
    { b MLSTATE_REORDER } 
    Debug QmlDependencies module ( reorder passes ) 

   {b MLSTATE_REORDER}
   Debug QmlDependencies module (reorder passes)
*)
val reorder : debug_var

val rpc_debug : debug_var
val rpc_alt_skeleton : debug_var

* 
    { b Debug on SurfaceAst passes } 

   {b Debug on SurfaceAst passes}
*)

* 
    { b MLSTATE_SESSION_DEBUG } 

   {b MLSTATE_SESSION_DEBUG}
*)
val session_debug : debug_var

val scheduler_debug : debug_var

val server_serialize : debug_var

val server_stats : debug_var





val ssl_debug : debug_var

val testing : debug_var

* 
    { b MLSTATE_TYPER } 
    Works with level . 
    Debugging the type checker . 

   {b MLSTATE_TYPER}
   Works with level.
   Debugging the type checker.
*)
val typer : debug_var

val resource_tracker_debug : debug_var


* 
    { b  } 

   {b MLSTATE_WEBLIB_DEBUG}
*)

val badop_xml_import_commit_transaction_step : debug_var


* { 6 Variables Testers }


type debug_tester = debug_var -> bool

val default : debug_tester

val null : debug_tester

val defined : debug_tester

val undefined : debug_tester

val toggle : debug_tester

val equals : string -> debug_tester

val level : (int -> bool) -> debug_tester

* 
    A specific level . 
    [ # < If : MYVAR $ islevel 42 > ] : returns [ true ] if and only if the variable is set to " 42 " 

   A specific level.
   [#<If:MYVAR $islevel 42>] : returns [true] if and only if the variable is set to "42"
*)
val islevel : int -> debug_tester

val maxlevel : int -> debug_tester

val minlevel : int -> debug_tester

val contains : string -> debug_tester

val cont : debug_tester -> debug_tester

val is_contained : string -> debug_tester

val flag : string -> debug_tester
