Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Iterate over logfile content by timestamp
    primarykey
    data
    text
    <p>I have a log file with log lines. Each line is composed of a timestamp and a message.</p> <pre><code>timestamp1 blablabla timestamp2 foo timestamp3 bar etc... </code></pre> <p>My class <code>LogFile</code> has a map as member to match each timestamp to an ifstream position.<br> Now I would like to create a custom iterator over these timestamps.</p> <p>example : </p> <pre class="lang-c prettyprint-override"><code>LogFile myFile("file.log"); for (LogFile::iterator it = myFile.begin(); it != myFile.end(); it++) std::cout &lt;&lt; it-&gt;message &lt;&lt; std::endl; </code></pre> <p>output :</p> <pre><code>blablabla foo bar </code></pre> <p>I also want the iterator to be able to get decremented.</p> <p>Now I don't really know how to implement it in an efficient way.</p> <p>The most simple would be to open-seek-read-close the file with each iterator increment. But is that efficient ? I read that open/seek/close was quite expensive.</p> <p>Maybe a better solution is to have a <code>LogFile::open()</code> method to open the file, keeping it open to do all the incrementation we want, and finally close the file with a <code>LogFile::close()</code> method.</p> <p>Do you have some tips over this ? I'm sure this not the first time someone has to deal this kind of problem.</p> <p>EDIT A few more details :</p> <p>My class LogFile has a member of type <code>std::map&lt;Time, std::streampos&gt;</code> in order to store the links between timestamps and stream position.<br> I need to increment <em>and</em> decrement the iterator. Hence I think a map would be more appropriate, since I will use <code>the std::map::find(Time)</code> a lot (complexity o(log(n)), instead of <code>std::vector::find(Time)</code> with a o(n) complexity).</p> <p>The log file is very big (~20Mo) and my application has to run on a limited resources embeded system. So I can't store the whole file in the ram, I have to bufferize and take only the part I need on a given time.</p> <p>So yes, I think I'm gonna deal with the "open() and close() once" method.</p> <p>In the case of increment, I dont have to <code>std::ifstream::seekg()</code> a lot. But in the case of decrement, I dont see another way to seekg for each timestamp. Is that really the best way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload