What is the most efficient way to log a very small amount of data?

Suppose I only register 1 integer when a function is called in a multithreaded environment, then what is the best construct to implement this mechanism? Example:

void foo1 () {
void foo2 () {


Possible ways are given below:

  • Just enter the file using fprintf()

    . Problem : Isn't it an expensive operation to call a function just to register 1 integer? Correct me if I am wrong.
  • Store integers to an array buffer; and periodically included in the file. Problem . If a thread crashes, the process will stop all threads. So maybe I could lose a ton of the latest log information.

Another suggestion for an efficient logging engine?


source to share

2 answers

Well, there is no "simple" logging. fprintf

will make a jump to the kernel (context switch), then back to the program (also context switch). Not fast if speed is what you want. You will probably also need a very expensive sync()

one to make sure the log data actually gets out to disk in the event of a power failure. You really don't want to go there :)

I would say the buffering method is the fastest and smartest trade-off between speed and reliability. What I would do is have the buffer in sync so that it can write safely across multiple threads. At the same time, I run a write to disk stream that occasionally writes data to disk (depends a lot on the type of data). I would use a very basic language feature, going more down to earth plain C

, simply because some features (exception handling, multiple inheritance ..) are just too prone to being interrupted in special circumstances.

One thing you may not know is that programs do have a say when they crash. You can subscribe to the kill signals program (some signals can be canceled by the program, but the kill signal is not one of them). While you are working with signals, you can flush the log buffer one last time and save more data. And there is atexit()




You can either take a look at a logging library like alarm log, or alternatively, look at a std :: cout, cerr, cin wrapper (or a file you write) with mutexes because it is buffered, then it is not should write small files to file continuously.



All Articles