123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566(*{{{ Copyright (c) 2012-2014 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
}}}*)let_debug_active=reffalseletdebug_active()=!_debug_activeletdefault_reporter()=(* Note: we want the logging operation to not block the other operation.
* Hence, the reporter creates Lwt promises. *)letfmtr,fmtr_flush=letb=Buffer.create512in(Fmt.with_buffer~like:Fmt.stdoutb,fun()->letm=Buffer.contentsbinBuffer.resetb;m)inletreport_src_level~overkmsgf=letk_=letwrite()=Lwt_io.writeLwt_io.stderr(fmtr_flush())inletunblock()=over();Lwt.return_unitinLwt.ignore_result@@Lwt.catch(fun()->(Lwt.finalizewriteunblock:unitLwt.t))(fune->Logs.warn(funf->f"Flushing stderr failed: %s"(Printexc.to_stringe));Lwt.return_unit);k()inmsgf@@fun?header:_?tags:_fmt->Format.kfprintfkfmtr("@["^^fmt^^"@]@.")in{Logs.report=report}letset_log=lazy((* If no reporter has been set by the application, set default one
that prints to stderr *)if(Logs.reporter())==Logs.nop_reporterthenLogs.set_level@@SomeLogs.Debug;Logs.set_reporter(default_reporter());)letactivate_debug()=Lazy.forceset_log;_debug_active:=true;Logs.debug(funf->f"Cohttp debugging output is active")let()=try(matchSys.getenv"COHTTP_DEBUG"with|"false"|"0"->()|_->activate_debug())withNot_found->()