123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235(**************************************************************************)(* The OUnit library *)(* *)(* Copyright (C) 2002-2008 Maas-Maarten Zeeman. *)(* Copyright (C) 2010 OCamlCore SARL *)(* Copyright (C) 2013 Sylvain Le Gall *)(* *)(* The package OUnit is copyright by Maas-Maarten Zeeman, OCamlCore SARL *)(* and Sylvain Le Gall. *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining *)(* a copy of this document and the OUnit software ("the Software"), to *)(* deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, *)(* sublicense, and/or sell copies of the Software, and to permit persons *)(* to whom the Software is furnished to do so, subject to the following *)(* conditions: *)(* *)(* The above copyright notice and this permission notice shall be *)(* included in all copies or substantial portions of the Software. *)(* *)(* The Software is provided ``as is'', without warranty of any kind, *)(* express or implied, including but not limited to the warranties of *)(* merchantability, fitness for a particular purpose and noninfringement. *)(* In no event shall Maas-Maarten Zeeman be liable for any claim, damages *)(* or other liability, whether in an action of contract, tort or *)(* otherwise, arising from, out of or in connection with the Software or *)(* the use or other dealings in the software. *)(* *)(* See LICENSE.txt for details. *)(**************************************************************************)(*
Logger for information and various OUnit events.
*)openOUnitUtils(* See OUnit.mli. *)typeposition={filename:string;line:int;}(** See OUnit.mli. *)typelog_severity=[`Error|`Warning|`Info](** See OUnit.mli. *)type'resulttest_event=|EStart|EEnd|EResultof'result|ELogoflog_severity*string|ELogRawofstringtype('path,'result)result_full=('path*'result*positionoption)(** Events which occur at the global level. *)type('path,'result)global_event=|GConfofstring*string(** Dump a configuration options. *)|GLogoflog_severity*string|GStart(** Start running the tests. *)|GEnd(** Finish running the tests. *)|GResultsof(float*('path,'result)result_fulllist*int)type('path,'result)log_event_t=|GlobalEventof('path,'result)global_event|TestEventof'path*'resulttest_eventtype('path,'result)log_event={shard:string;timestamp:float;event:('path,'result)log_event_t;}type('path,'result)logger={lshard:string;fwrite:('path,'result)log_event->unit;fpos:unit->positionoption;fclose:unit->unit;}letshard_default=OUnitUtils.shardf0letstring_of_eventev=letspffmt=Printf.sprintffmtinletstring_of_log_severity=function|`Error->"`Error"|`Warning->"`Warning"|`Info->"`Info"inmatchevwith|GlobalEvente->beginmatchewith|GConf(k,v)->spf"GConf (%S, %S)"kv|GLog(lvl,s)->spf"GLog (%s, %S)"(string_of_log_severitylvl)s|GStart->"GStart"|GEnd->"GEnd"|GResults_->"GResults"end|TestEvent(_,e)->beginmatchewith|EStart->"EStart"|EEnd->"EEnd"|EResult_->"EResult (_)"|ELog(lvl,str)->spf"ELog (%s, %S)"(string_of_log_severitylvl)str|ELogRawstr->spf"ELogRaw %S"strendletnull_logger={lshard=shard_default;fwrite=ignore;fpos=(fun()->None);fclose=ignore;}letfun_loggerfwritefclose={lshard=shard_default;fwrite=(funlog_ev->fwritelog_ev);fpos=(fun()->None);fclose=fclose;}letpost_loggerfpost=letdata=ref[]inletfwriteev=data:=ev::!datainletfclose()=fpost(List.rev!data)in{lshard=shard_default;fwrite=fwrite;fpos=(fun()->None);fclose=fclose;}letset_shardshardlogger={loggerwithlshard=shard}letreportloggerev=logger.fwrite{shard=logger.lshard;timestamp=now();event=ev;}letinfofloggerfmt=Printf.ksprintf(funstr->reportlogger(GlobalEvent(GLog(`Info,str))))fmtletwarningfloggerfmt=Printf.ksprintf(funstr->reportlogger(GlobalEvent(GLog(`Warning,str))))fmtleterrorfloggerfmt=Printf.ksprintf(funstr->reportlogger(GlobalEvent(GLog(`Error,str))))fmtletpositionlogger=logger.fpos()letcloselogger=logger.fclose()letcombinelst=letrecfpos=function|logger::tl->beginmatchpositionloggerwith|Some_aspos->pos|None->fpostlend|[]->Noneinletlshard=matchlstwithhd::_->hd.lshard|[]->shard_defaultin{lshard=lshard;fwrite=(funlog_ev->List.iter(funlogger->logger.fwritelog_ev)lst);fpos=(fun()->fposlst);fclose=(fun()->List.iter(funlogger->closelogger)(List.revlst));}moduleTest=structtype'resultt='resulttest_event->unitletcreateloggerpath=funev->logger.fwrite{shard=logger.lshard;timestamp=now();event=TestEvent(path,ev)}letraw_printftfmt=Printf.ksprintf(funs->t(ELogRaws))fmtletlogftlvlfmt=Printf.ksprintf(funs->t(ELog(lvl,s)))fmtend