0

I am working a big legacy project and need to redo the common logger. I tried to make same logger interface with before to avoiding changing ton of loggers. The reason I need to redo the logger is the old one is syslog UDP which was using built-in library functions, while the new one I'm using GELF UDP.

Suppose I have a log with two parts of message, severity is info. The old interface is like below:

Log_INFO<< "First part message" <<"Second part message"<< endl;

Log_INFO is like 'std::cout', but it has two functionality:

  1. Print out message in the command line.
  2. Collect it in Graylog.

My new function is like below:

//Severity = {debug,info,warning, error, critical}
Log(Severity, whole_message)

For the same example,

Log("info",first_part_message+ second_part_message)

My question is how can I make my function is able to read log like the old one.

Alanaa
  • 75
  • 8

1 Answers1

0

One common way of doing this is creating a custom streambuf-derived class, say LogStreambuf, and an ostream-derived class, say LogStream, that uses LogStreambuf (but is otherwise a plain jane ostream).

Then your log objects would be

LogStream Log_INFO("info");
LogStream Log_WARN("warn");

etc.

Your custom streambuf probably should call your Log function from its sync method.

See e.g. this for an example, and this for further guidance.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243