Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep track of the current position in std::stringstream

I am writing a custom logger where I buffer my log messages in a std::stringstream and flush it to a file (std::ofstream) whenever the std::stringstream is big enough(to save some IO latency) . sincestd::stringstream doesn't have a .size() method, I use seekg and tellg :

template <typename T>
MyClass & operator<< (const T& val)
{
    boost::unique_lock<boost::mutex> lock(mutexOutput);
    output << val;  //std::stringstream output;
    output.seekg(0, std::ios::end);
    if(output.tellg() > 1048576/*1MB*/){
        flushLog();
    }
    return *this;
}

Problem: It seems to me that, whenever I invoke this method, it uses seekg to start counting the bytes from the beginning all the way to the end and get the size using tellg. I came up with this design to save some IO time in the first place, but: isn't this continuous counting impose a larger cost(if the number of calls to this method is high and log messages are small as in most of the cases)?

is there a better way to do this?

And a side question: is 1MB a good number for buffer size in a normal nowadays computers?

Thank you

like image 952
rahman Avatar asked Sep 30 '22 07:09

rahman


1 Answers

You can just use ostringstream::tellp() to get the length of the string. Here's an example lifted from http://en.cppreference.com/w/cpp/io/basic_ostream/tellp.

#include <iostream>
#include <sstream>
int main()
{
    std::ostringstream s;
    std::cout << s.tellp() << '\n';
    s << 'h';
    std::cout << s.tellp() << '\n';
    s << "ello, world ";
    std::cout << s.tellp() << '\n';
    s << 3.14 << '\n';
    std::cout << s.tellp() << '\n' << s.str();
}

Output:

0
1
13
18
hello, world 3.14
like image 110
R Sahu Avatar answered Oct 19 '22 02:10

R Sahu