c++ - What is the most efficient way of logging very small amount of data? -


suppose logging 1 integer when function called in multi-threaded environment, best design implement mechanism ? example:

void foo1 () {   log(1);   ... } void foo2 () {   log(2);   ... } 

following possible ways:

  1. simply log file using fprintf(). problem: isn't expensive operation call function log 1 integer ? correct me if wrong.
  2. store logged integers array buffer; , flush periodically file. problem: if thread crashes, process stop threads. possibly, may loose lot of last log info.

any more suggestion efficient logging mechanism ?

well, "simple" logging isn't. fprintf make jump kernel (context switch), program (also context switch). ain't fast, if speed need. you'd need very, expensive sync() make sure logging data makes disk in case of power failure. don't want go there :)

i'd buffered method fastest , reasonable tradeoff between speed , reliability. i'd have buffer, synchronized safely written multiple threads. concurrently i'd run disk-writer thread flush data disk once in while (depends lot on kind of data have). i'd use basic language feature, going more plain c land, because features (exception handling, multiple inheritance..) prone break in special circumstances.

one thing maybe don't know, programs have when crash. can subscribe program killing signals (some signals can cancelled program, killing signal isn't 1 of them). while you're in signal handling, can flush log buffer 1 last time , save more data. , there atexit().


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -