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.

6
  • 1
    I'm not clear what you want to know Commented Oct 21, 2022 at 17:01
  • 1
    Can you elaborate more on what your question is? Commented Oct 21, 2022 at 17:01
  • 2
    What's wrong with the old interface? Especially considering that first_part_message+ second_part_message will likely not work very well for all but a few cases. Commented Oct 21, 2022 at 17:02
  • The question is missing... Commented Oct 21, 2022 at 17:31
  • Did you ask the question previously, something about turning the log function into a macro, or I am just having deja vu? I very particularly remember responding to a question also with the variable name Log_INFO... Commented Oct 21, 2022 at 17:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.