trantor icon indicating copy to clipboard operation
trantor copied to clipboard

trantor::LogStream <- std::ostream inheritance

Open mgorshkov opened this issue 1 year ago • 1 comments

Hello! It would be nice if trantor::LogStream class inherited std::ostream interface. Currently, in our project we have to implement 2 stream redirections, or use templatized implementation, for every model class, to cover both cases:

  • Redirection to trantor::LogStream
  • Redirection to std::ostream. We are using LogStream for logging with trantor, and std::ostream to be compatible with standard C++ library, ex, to be able to use std::stringstream. If trantor::LogStream inherited std::ostream interface, the users could implement only one redirection function per each model class.

We can propose 2 solutions:

  • Create new proxy classes, which would implement std::streambuf/std::ostream interfaces correspondingly and seamlessly redirect all the calls into trantor::LogStream or trantor::LogStream::Buffer (via 'append' method) like this (this is just an example):

trantor

This will require another logger, and a new implementation of LOG_INFO, LOG_DEBUG etc macros, since this utilises another stream implementation.

  • Inherit trantor::LogStream from std::ostream and implement forwarding into trantor's LogStream::Buffer in there (old interfaces are left where they are, but this option will require additional effort to maintain backward capability).

Actually, we have already implemented option 1 in our codebase, and it proved to work fine, but the drawback is that now we have to maintain 2 loggers - the old trantor's native logger, and the new std::ostream-compatible logger. If it was possible to implement either of options 1 or 2 in the trantor library directly, it would be nice. What does the community think about this idea?

mgorshkov avatar Jul 23 '24 09:07 mgorshkov

@an-tao Please comment on this

mgorshkov avatar Jul 23 '24 10:07 mgorshkov