iammilind
iammilind

Reputation: 69988

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

Suppose I am logging only 1 integer when a function is called in multi-threaded environment, then what is the best design to implement this mechanism ? Example:

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

Following are the possible ways:

  1. Simply log into the file using fprintf(). Problem: Isn't it an expensive operation to call a function just to log 1 integer ? Correct me if I am wrong.
  2. Store logged integers into an array buffer; and flush periodically into a file. Problem: If a thread crashes, then process would stop all the threads. So possibly, I may loose lot of last log info.

Any more suggestion for efficient logging mechanism ?

Upvotes: 3

Views: 1036

Answers (2)

111111
111111

Reputation: 16148

You could either take a look at a logging library like boost log or alternatively look at wrapping up std::cout, cerr, cin (or the file you log too) with mutexes, because there are buffered then it shouldn't continuously be writing small amounts to the file.

Upvotes: 0

Pasi Savolainen
Pasi Savolainen

Reputation: 2500

Well, "simple" logging isn't. fprintf will make a jump to kernel (context switch), then back to program (also context switch). Ain't fast, if speed is what you need. You'd probably also need a very, very expensive sync() to make sure the logging data actually makes it to the disk in case of power failure. You really don't want to go there :)

I'd say that the buffered method is actually the fastest and most reasonable tradeoff between speed and reliability. What I'd do is have that buffer, synchronized to be safely written by multiple threads. Concurrently I'd run a disk-writer thread that would flush data to disk once in a while (depends a lot on kind of data you have). I'd use very basic language feature, going more into the plain C land, just because some features (exception handling, multiple inheritance..) are just too prone to break in special circumstances.

One thing you maybe don't know, is that programs do have a say when they crash. You can subscribe to program killing signals (some signals can be cancelled by program, but killing signal isn't one of them). While you're in signal handling, you can flush the log buffer one last time and save more data. And there is also atexit().

Upvotes: 2

Related Questions