0

I am new to Boost.Log and Using it to develop a logger library as a wrapper on top of Boost.Log. My Problem is finding a convenient way to set a counter for number of consecutive repeated log messages instead of printing the same log multiple times.

for example: (ADD_LOG() method is in my library and do BOOST_LOG_SEV(...))

ADD_LOG("Hello");
ADD_LOG("Hello");
ADD_LOG("Hello");
ADD_LOG("Some Different Hello");

I want to be the output log file like this: (sample_0.log)

........................................................

[TimeStamp] [Filter] Hello

[TimeStamp] [Filter] Skipped 2 duplicate messages!

[TimeStamp] [Filter] Some Different Hello

.......................................................

I am using this example with Text File Backend and TimeStamp, Filter are Ok. My problem is about skipping duplicates. maybe with setting filters or anything else.

I think syslog in linux has this feature by some configurations.

Jafar Gh
  • 137
  • 2
  • 12
  • I think, I should have a buffer like model to save the last message logged to file. then checking with later messages? – Jafar Gh Mar 10 '20 at 06:47

1 Answers1

1

Boost.Log does not implement such log record accumulation, you will have to implement it yourself. You can do this by implementing a sink backend that would buffer the last log record message and compare it with the next one. Note that you should not buffer the whole record or formatted string because it will likely differ because of timestamps, record counters and other frequently changing attribute values that you might use.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27
  • because for example there are 10 consecutive same message and the logger should wait for all of them and then just print 1 line – Jafar Gh Mar 10 '20 at 09:00
  • No, it doesn't have to work asynchronously. The backend would check every log record it consumes. If it matches the previous one, it increments the counter and outputs nothing. If it is different then, if the counter is not zero, it outputs a line saying the number of duplicates skipped, resets the counter to zero and outputs the new log record. It also saves the message for comparing with the future records. – Andrey Semashev Mar 10 '20 at 09:28
  • I use text_file_backend to logging. as I read [this page](https://www.boost.org/doc/libs/1_58_0/libs/log/doc/html/log/extension.html) , it derives a class from `basic_formatted_sink_backend` and implement. But I want all of the functionality of text_file_backend and only adding a buffer. Is it possible to inherit from `text_file_backend` insted of `basic_formatted_sink_backend`? – Jafar Gh Apr 06 '20 at 18:19
  • And also my program runs in multi-thread env. how should i care about it? – Jafar Gh Apr 06 '20 at 18:52
  • Post a new question, with a code sample showing what you're doing and what isn't working. – Andrey Semashev Apr 06 '20 at 21:00