123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687(*{{{ 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_activeopenLwt.Infixletreporterfile_descrppf=letppf,flush=letbuf=Buffer.create0x100in(Fmt.with_buffer~like:ppfbuf,fun()->letstr=Buffer.contentsbufinBuffer.resetbuf;str)inletreportsrclevel~overkmsgf=letk_=letwrite()=letbuf=Bytes.unsafe_of_string(flush())inletrecgoofflen=Lwt_unix.writefile_descrbufofflen>>=funlen'->iflen'=lenthenLwt.return_unitelsego(off+len')(len-len')ingo0(Bytes.lengthbuf)inletclean()=over();Lwt.return_unitinLwt.async(fun()->Lwt.catch(fun()->Lwt.finalizewriteclean)(funexn->Logs.warn(funm->m"Flushing error: %s."(Printexc.to_stringexn));Lwt.return_unit));k()inletwith_metadataheader_tagskppffmt=Format.kfprintfkppf("%a[%a]: "^^fmt^^"\n%!")Logs_fmt.pp_header(level,header)Fmt.(styled`Magentastring)(Logs.Src.namesrc)inmsgf@@fun?header?tagsfmt->with_metadataheadertagskppffmtin{Logs.report}letdefault_reporter=reporterLwt_unix.stderrFmt.stderrletset_logger=lazy(if(* If no reporter has been set by the application, set default one
that prints to stderr *)Logs.reporter()==Logs.nop_reporterthenLogs.set_reporterdefault_reporter)letactivate_debug()=ifnot!_debug_activethen(_debug_active:=true;Lazy.forceset_logger;Logs.set_level~all:true(SomeLogs.Debug);Logs.debug(funf->f"Cohttp debugging output is active"))let()=trymatchSys.getenv"COHTTP_DEBUG"with|"false"|"0"->()|_->activate_debug()withNot_found->()