I'd like to add a timestamp to certain outputs to the std::cout / std::cerr ostreams, without using modified standard streams, like so:
std::cerr << timestamp << "Warning!\n";
or so:
std::cerr << timestamp() << "Warning!\n";
The output should look like this:
[2020-01-23 17:40:15 CET] Warning!
But I'm really not happy with what I've come up with:
class TimeStamp {};
std::ostream &operator<<(std::ostream &stream, const TimeStamp &ts)
{
std::time_t t = std::time(nullptr);
stream << "[" << std::put_time(std::localtime(&t), "%F %T %Z") << "] ";
return stream;
}
TimeStamp ts;
int main()
{
std::cerr << ts << "Warning!\n";
std::cerr << ts << "Another warning!\n";
}
So I'm basically defining an empty class, using a global declaration and overloading the '<<' operator. This feels wrong. A static function like timestamp() is probably better suited, but I'm not quite sure how to go on about this. All the examples I've found online used the overloaded '<<' operator, but it usually made more sense to do so, because some class state was output. Can I locally create an ostream and return that in the function?
There's nothing wrong with the way you've done it. But if you're looking for alternatives, you could create an ostream wrapper:
class Logger {
private:
std::ostream &stream;
void print_time() {
std::time_t t = std::time(nullptr);
stream << "[" << std::put_time(std::localtime(&t), "%F %T %Z") << "] ";
}
public:
//Maybe also take options for how to log?
Logger(std::ostream &stream) : stream(stream) { }
template <typename T>
std::ostream &operator<<(const T &thing) {
print_time();
return stream << thing;
}
};
int main()
{
Logger log(std::cerr);
log << "Warning!" << std::endl;
log << "Another warning!" << std::endl;
}
See it run here: https://ideone.com/YRawuQ
If you're just looking for a standalone function which is what I understood from a "static function like timestamp()" you can just return the date as a string:
std::string timeStamp(){
std::ostringstream strStream;
std::time_t t = std::time(nullptr);
strStream<< "[" << std::put_time(std::localtime(&t), "%F %T %Z") << "] ";
return strStream.str();
}
int main(){
std::cout<<timeStamp()<<" Testing!";
return 0;
}
Remember to include sstream
You can use standard std::chrono::time_point class from the date and time library to represent the timestamp. Then, you need to convert that timestamp to std::time_t type and, eventually, convert the date and time information from a given calendar time to a character string according to the format string.
auto const now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&now_time), "%F %T") << std::endl;
For those who want to know more...
You can use the source_location class that represents certain information about the source code, such as file names, line numbers, and function names. It is being merged into ISO C++ and is available for use.
Full code
#include <ctime>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <experimental/source_location>
void error(std::string_view const& message,
std::ostream& os = std::cout,
std::experimental::source_location const& location = std::experimental::source_location::current()) {
auto const now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
os << "[" << std::put_time(std::localtime(&now_time), "%F %T") << "] "
<< "[INFO] "
<< location.file_name() << ":"
<< location.line() << " "
<< message << '\n';
}
void info(std::string_view const& message,
std::ostream& os = std::cout,
std::experimental::source_location const& location = std::experimental::source_location::current()) {
auto const now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
os << "[" << std::put_time(std::localtime(&now_time), "%F %T") << "] "
<< "[INFO] "
<< location.file_name() << ":"
<< location.line() << " "
<< message << '\n';
}
int main() {
error("Some error");
info("Some info");
// or
error("Some error 2", std::cerr);
info("Some info 2", std::cerr);
return 0;
}
Check it out live
Related
I'm sorry the title may be inaccurate.I'm new to C++.
Here is my code and output...
#include <iostream>
#include <sstream>
using namespace std;
class LogLine {
private:
stringstream ss;
string message;
public:
~LogLine() {
ss << "\n";
message = ss.str();
cout << message;
message = "";
}
template <class T>
LogLine& operator<<(const T& thing) {
ss<< thing;
return *this;
}
LogLine& operator<<(std::ostream &(*manip)(std::ostream &)) {
manip(ss);
return *this;
}
};
int main(int argc, char *argv[])
{
LogLine log;
cout<< "Line One"<<endl;
log << "I'm " << 25 << " years old...."<<endl;
cout<<"Line Two"<<endl;
log << "I " << "Live in " << " Houston....";
return 0;
}
Current output:
Line One
Line Two
I'm 25 years old....
I Live in Houston....
Desired output:
Line One
I'm 25 years old....
Line Two
I Live in Houston....
I hope in each line of "log" be able to detect the end of that line and print out current message, and then clean itself. I know the reason of current output, but I can't figure out how should I modify my code to get desired output.("endl" can be missing)
Thanks a lot for any comments.
As described above...
When I understand you correctly, you want to detect the end of the statement, where log is used, and then append a std::endl.
My solution is similar to that one of #MarekR, but it forces a line break, when log is not rebound:
It does not detect "\n" and flushes it to std::cout, that would be contra productive on parallel std::cout calls.
#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
class LogLine {
std::stringstream ss;
public:
LogLine(LogLine&&) noexcept = default;
LogLine() = default;
~LogLine()
{
if(ss && ss.peek() != -1){
cout << ss.str() << std::endl;
}
}
template <class T>
friend LogLine operator<<(LogLine& lhs, const T& thing)
{
lhs.ss << thing;
return std::move(lhs);
}
template <class T>
friend LogLine&& operator<<(LogLine && lhs, const T& thing)
{
lhs.ss << thing;
return std::move(lhs);
}
LogLine&& operator<<(std::ostream& (*manip)(std::ostream&))
{
manip(ss);
return std::move(*this);
}
};
int main()
{
LogLine forced;
cout << "Line One" << endl;
forced << "I'm " << 25 << " years old....";
cout << "Line Two" << endl;
LogLine() << "I " << "Live in " << " Houston...." << endl << endl << endl;
forced << "forced 2";
std::cout << "End of the sausage" << std::endl;
return 0;
}
That what happens here is: every call to operator<< creates a temporary, which steals the resources of the original structure. Therefore, when it is not rebound, the destructor gets called directly after the line, flushing the stringstream.
I'm new in Boost.Log. I'd like to print out data in the following format:
[TimeStamp][Severity] Message.
I have such code
auto my_formatter = [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm)
{
strm << "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]";
strm << "[" << rec[boost::log::trivial::severity] << "] ";
strm << rec[boost::log::expressions::smessage];
};
void addFileSink()
{
using sinkType = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;
auto backend = boost::make_shared<boost::log::sinks::text_file_backend>(
boost::log::keywords::file_name = "log_%N.log",
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app
);
boost::shared_ptr<sinkType> sink(new sinkType(backend));
sink->set_formatter(my_formatter);
sink->set_filter(boost::log::trivial::severity >= boost::log::trivial::severity_level::info);
boost::log::core::get()->add_sink(sink);
}
void addConsoleSink()
{
using sinkType = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;
auto backend = boost::make_shared<boost::log::sinks::text_ostream_backend>();
backend->add_stream(boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter()));
backend->auto_flush(true);
boost::shared_ptr<sinkType> sink(new sinkType(backend));
sink->set_formatter(my_formatter);
sink->set_filter(boost::log::trivial::severity >= boost::log::trivial::severity_level::error);
boost::log::core::get()->add_sink(sink);
}
int main()
{
boost::log::add_common_attributes();
addFileSink();
addConsoleSink();
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::error) << "Message error";
BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::info) << "Message info";
return 0;
}
It works fine beside of TimeStamp attribute, I'm getting such output:
[[error] Message error
I've tried different ways from examples and so on, but I can't find solution for this problem..
boost::log::add_common_attributes(); doesn't help.
boost::log::expressions::format_date_time is not a function to format date and time. It is a formatter generator to be used in lambda formatting expressions. In other words, format_date_time creates a formatter (a function object with a specific signature), which, upon calling, will extract the attribute value from the log record and format it.
You should either use lambda expressions to build the formatter instead of your my_formatter function, in which case you can use format_date_time, or use one of the traditional methods of formatting date and time, like strftime or Boost.DateTime IO.
Example of using the lambda expressions:
namespace expr = boost::log::expressions;
sink->set_formatter
(
expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
<< "[" << boost::log::trivial::severity << "] "
<< expr::smessage
);
Example of using strftime:
auto my_formatter = [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm)
{
if (auto timestamp = boost::log::extract< boost::posix_time::ptime >("TimeStamp", rec))
{
std::tm ts = boost::posix_time::to_tm(*timestamp);
char buf[128];
if (std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ts) > 0)
strm << "[" << buf << "]";
}
strm << "[" << rec[boost::log::trivial::severity] << "] ";
strm << rec[boost::log::expressions::smessage];
};
I would like to create a class for logging purposes which will behave like std::cout, but will automatically insert additional information to the stream.
a sample usage that I want would be something like (lets not care about object and context type for now, just assume they are std::string) :
Logger l;
l << "Event with object : " << obj << " while in context : " << context;
Then the output would be :
[timestamp] Event with object : [obj_desc] while in context : [this context][eol][flush]
I've been trying with :
template<typename T>
Logger& operator << (const T& msg){
std::cout << timestamp() << msg << std::endl << std::flush;
return *this;
}
but it seems that std::cout cannot resolve the typename T and fails to output a std::string for example while segfaulting.
A possible solution would be to overload this for all types, but this is rather annoying and time consuming.
Is there a better option to decorate std::cout output with more information?
Edit :
I do realize now that endl and flush will be appended to every message which kind of defeat the purpose, but I'm still interested in the general idea. I care more about the monadic syntax to append an arbitrary number of messages than the <<overload
The reason your code does not work is because you have not implemented operator<< for everything you want to pass to it.
This statement:
Logger l;
l << "Event with object : " << obj << " while in context : " << context;
Is basically doing this (assuming operator<< is a member of Logger, which your implementation implies it is):
Logger l;
l.operator<<("Event with object : ").operator<<(obj).operaator<<(" while in context : ").operator<<(context);
So, you need separate overloads of operator<< for string, obj, context, etc. And you need a way to indicate when to flush the complete log message to std::cout.
I would suggest something more like this:
struct LoggerStream
{
std::ostringstream strm;
struct Timestamp
{
};
~LoggerStream()
{
std::string s = strm.str();
if (!s.empty())
std::cout << s << std::flush;
}
LoggerStream& operator<< (const Timestamp &t)
{
strm << "[timestamp] "; // format this however you need
return *this;
}
LoggerStream& operator<< (const object &obj)
{
strm << "[obj_desc]"; // format this however you need
return *this;
}
LoggerStream& operator<< (const context &ctx)
{
strm << "[this context]"; // format this however you need
return *this;
}
LoggerStream& operator<< (std::ostream&(*f)(std::ostream&))
{
if (f == (std::basic_ostream<char>& (*)(std::basic_ostream<char>&)) &std::flush)
{
std::string s = strm.str();
if (!s.empty())
std::cout << s << std::flush;
strm.str("");
strm.clear();
}
else
strm << f;
return *this;
}
template<typename T>
LoggerStream& operator<< (const T& value)
{
strm << value;
return *this;
}
};
class Logger
{
LoggerStream getStream()
{
LoggerStream strm;
strm << Timestamp;
return strm;
}
};
Then you can do things like this:
Logger l;
l.getStream() << "Event with object : " << obj << " while in context : " << context;
...
l.getStream() << "Event with object : " << obj << " while in context : " << context;
...
Logger l;
LoggerStream strm = l.getStream();
strm << "Event with object : " << obj << " while in context : " << context << std::flush;
...
strm << Logger::Timestamp << "Event with object : " << obj << " while in context : " << context << std::flush;
...
Alternatively:
struct Logger
{
std::ostringstream strm;
~Logger()
{
std::string s = strm.str();
if (!s.empty())
std::cout << "[timestamp] " << s << std::flush;
}
Logger& operator<< (const object &obj)
{
strm << "[obj_desc]"; // format this however you need
return *this;
}
Logger& operator<< (const context &ctx)
{
strm << "[this context]"; // format this however you need
return *this;
}
Logger& operator<< (std::ostream&(*f)(std::ostream&))
{
if (f == (std::basic_ostream<char>& (*)(std::basic_ostream<char>&)) &std::flush)
{
std::string s = strm.str();
if (!s.empty())
std::cout << "[timestamp] " << s << std::flush;
strm.str("");
strm.clear();
}
else
strm << f;
return *this;
}
template<typename T>
Logger& operator<< (const T& value)
{
strm << value;
return *this;
}
};
Logger() << "Event with object : " << obj << " while in context : " << context;
...
Logger() << "Event with object : " << obj << " while in context : " << context;
...
Logger l;
l << "Event with object : " << obj << " while in context : " << context << std::flush;
...
l << "Event with object : " << obj << " while in context : " << context << std::flush;
...
You can certainly overload the stream classes if you want, providing operator<< for all the data types you want to support (and that's probably the "correct" way to go) but, if all you're after is a quick way to add logging to a regular stream, there a simpler way:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctime>
#include <unistd.h>
#define logcout std::cout << timestamp()
std::string timestamp(void) {
time_t now = time(0);
struct tm *tmx = localtime(&now);
std::ostringstream oss;
oss << '['
<< (tmx->tm_year+1900)
<< '-'
<< std::setfill('0') << std::setw(2) << (tmx->tm_mon+1)
<< '-'
<< std::setfill('0') << std::setw(2) << (tmx->tm_mday)
<< ' '
<< std::setfill('0') << std::setw(2) << (tmx->tm_hour)
<< ':'
<< std::setfill('0') << std::setw(2) << (tmx->tm_min)
<< ':'
<< std::setfill('0') << std::setw(2) << (tmx->tm_sec)
<< "] ";
return oss.str();
}
int main (int argc, char *argv[]) {
logcout << "A slightly\n";
sleep (5);
logcout << "sneaky" << " solution\n";
return 0;
}
which outputs:
[2015-05-26 13:37:04] A slightly
[2015-05-26 13:37:09] sneaky solution
Don't be fooled by the size of the code, I just provided a complete compilable sample for testing. The crux of the matter is the single line:
#define logcout std::cout << timestamp()
where you can then use logcout instead of std::cout, and every occurrence prefixes the stream contents with an arbitrary string (the timestamp in this case, which accounts for the bulk of the code).
It's not what I would call the most pure C++ code but, if your needs are basically what you stated, it'll certainly do the trick.
I have been looking for a solution but couldn't find what I need/want.
All I want to do is pass a stream intended for std::cout to a function, which manipulates it. What I have used so far is a template function:
template<typename T>
void printUpdate(T a){
std::cout << "blabla" << a << std::flush;
}
int main( int argc, char** argv ){
std::stringstream str;
str << " hello " << 1 + 4 << " goodbye";
printUpdate<>( str.str() );
return 0;
}
What I would prefer is something like:
printUpdate << " hello " << 1 + 4 << " goodbye";
or
std::cout << printUpdate << " hello " << 1 + 4 << " goodbye";
I was trying to do:
void printUpdate(std::istream& a){
std::cout << "blabla" << a << std::flush;
}
but that gave me:
error: invalid operands of types ‘void(std::istream&) {aka void(std::basic_istream<char>&)}’ and ‘const char [5]’ to binary ‘operator<<’
You can't output data to an input stream, just not a good thing to do.
Change:
void printUpdate(std::istream& a){
std::cout << "blabla" << a << std::flush;
}
To:
void printUpdate(std::ostream& a){
std::cout << "blabla" << a << std::flush;
}
Note the stream type change.
Edit 1:
Also, you can't output a stream to another stream, at least std::cout.
The return value of << a is a type ostream.
The cout stream doesn't like being fed another stream.
Change to:
void printUpdate(std::ostream& a)
{
static const std::string text = "blabla";
std::cout << text << std::flush;
a << text << std::flush;
}
Edit 2:
You need to pass a stream to a function requiring a stream.
You can't pass a string to a function requiring a stream.
Try this:
void printUpdate(std::ostream& out, const std::string& text)
{
std::cout << text << std::flush;
out << text << std::flush;
}
int main(void)
{
std::ofstream my_file("test.txt");
printUpdate(my_file, "Apples fall from trees.\n");
return 0;
}
Chaining Output Streams
If you want to chain things to the output stream, like results from functions, the functions either have to return a printable (streamable object) or the same output stream.
Example:
std::ostream& Fred(std::ostream& out, const std::string text)
{
out << "--Fred-- " << text;
return out;
}
int main(void)
{
std::cout << "Hello " << Fred("World!\n");
return 0;
}
Below is the code i am running and corresponding output.
#include<iostream>
#include <sstream>
#include <strstream>
#include <streambuf>
template <typename char_type>
struct ostreambuf : public std::basic_streambuf<char_type,std::char_traits<char_type> >
{
ostreambuf(char_type* buffer, std::streamsize bufferLength)
{
// set the "put" pointer the start of the buffer and record it's length.
setp(buffer, buffer + bufferLength);
}
};
int main()
{
char strArr[] = "Before-1";
char stringArr[] = "Before-2";
std::strstream strStream(strArr,sizeof(strArr));
ostreambuf<char> ostreamBuffer(stringArr, sizeof(stringArr));
std::ostream stringStream(&ostreamBuffer);
const std::streampos posStringBefore = stringStream.tellp();
std::cout << "Before: "
<< "strArr = "
<< strArr
<< " & "
<< "stringArr = "
<< stringArr
<< std::endl;
std::cout << "Before: " << "posStringBefore = "
<< posStringBefore
<< std::endl;
// -------------------------
strStream << "After-1";
stringStream << "After-2";
const std::streampos posStringAfter = stringStream.tellp();
std::cout << "After : "
<< "strArr = "
<< strArr
<< " & "
<< "stringArr = "
<< stringArr
<< std::endl;
std::cout << "After : " << "posStringAfter = "
<< posStringAfter
<< std::endl;
return 0;
}
This is the o/p on VS2010 :
Before: strArr = Before-1 & stringArr = Before-2
Before: posStringBefore = -1
After : strArr = After-11 & stringArr = After-22
After : posStringAfter = -1
In reference to link
Setting the internal buffer used by a standard stream (pubsetbuf)
How to get the size of std::ostream object created?
It doesn't give you a "wrong" output/value. tellp uses rdbuf()->pubseekoff which relays the call to virtual seekoff. The basic_streambuf implementation simply returns -1 as defined in the C++ standard. You need to provide an own implementation for this method in your ostreambuf class.
See cppreference: basic_streambuf::pubseekof