RafaGago / mini-async-log

Non bloated asynchronous logger
Other
220 stars 20 forks source link

QUESTION: Way to log string variables #20

Closed bellekci closed 6 years ago

bellekci commented 6 years ago

Which way is better?

std::string str = "THIS IS A TEST";

log_debug("Example string log: {}", mal::lit(str.c_str()));

or

log_debug("Example string log: {}", mal::deep_copy(str));

Thanks in advance!

bellekci commented 6 years ago

One thing I've observed with the first method (print c-string) I cannot print empty strings. I get weird characters instead.

RafaGago commented 6 years ago

The first method (lit) is to log string literals or any other type of string that is guaranteed to outlive the logger thread destruction. That method is very lightweight as it just passes a pointer to the queue.

What is happening is that your "std::string" goes out of scope before the logger thread has processed the entry, so it prints garbage.

To pass a string reference counting would be necessary. This is implementable but I decided to cut the complexity just there.

You need to use the second method, "deep_copy", which embeds the string in the same log entry (the logger just does one internal allocation on an intrusive linked list and copies memory contiguosly).

If you are satisfied with the answer we can close this.

bellekci commented 6 years ago

Thanks for the detailed explanation. Long story short, I better use deep_copy then.

Yes, you can close the issue.