logging - C++ spdlog: How to append output to last log message? - Stack Overflow

admin2025-02-14  4

I want to append more output to last output.

Something like that:

logger->info("Starting...");
//...something...
if(!fail)
    logger->append("OK");

But I didnt find any append options in spdlog wiki. Maybe not append, but output changing options exist? Like in systemd initialisation when [OK] at the end written after actual check. Or maybe I need to use another logging lib?

I want to append more output to last output.

Something like that:

logger->info("Starting...");
//...something...
if(!fail)
    logger->append("OK");

But I didnt find any append options in spdlog wiki. Maybe not append, but output changing options exist? Like in systemd initialisation when [OK] at the end written after actual check. Or maybe I need to use another logging lib?

Share Improve this question edited Jan 4 at 18:55 Supreme Machine asked Jan 4 at 16:04 Supreme MachineSupreme Machine 595 bronze badges 4
  • #define SPDLOG_EOL "" in tweakme.h? – Jarod42 Commented Jan 4 at 17:09
  • @Jarod42 but will it break all outputs? After that change I should manually add \n to every completed message? – Supreme Machine Commented Jan 4 at 17:40
  • Yes. It seems the only way to disable eol. – Jarod42 Commented Jan 4 at 17:45
  • You should disable EOL with a custom formatter. That way it will not break all outputs. – Weijun Zhou Commented Jan 4 at 18:39
Add a comment  | 

1 Answer 1

Reset to default 0

spdlog does not have an "append" option to modify an already-logged message in place, because logging libraries are typically designed to output complete log messages in an atomic way. However, there are workarounds to achieve similar functionality depending on your needs,

You could buffer the output and write it only after the status is determined:

std::ostringstream oss;
oss << "Starting...";
if (!fail) {
    oss << " OK";
} else {
    oss << " FAILED";
}
logger->info(oss.str());
转载请注明原文地址:http://anycun.com/QandA/1739508589a18062.html