LCOV - code coverage report
Current view: top level - snaplogger - buffer_appender.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 28 100.0 %
Date: 2021-10-14 20:12:47 Functions: 12 14 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snaplogger
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : /** \file
      21             :  * \brief Appender used to bufferize all the log data.
      22             :  *
      23             :  * This appender is a memory buffer. It is primarily used by the tests
      24             :  * to be able to verify all the other features avalaible in the library,
      25             :  * such as the line formatter.
      26             :  *
      27             :  * It could be useful to you in case you have to create a network connection
      28             :  * and do so in parallel. While the connection is being created, you may
      29             :  * want to bufferize the logs using this appender.
      30             :  *
      31             :  * Keep in mind that a buffer uses RAM which you may need for other things.
      32             :  */
      33             : 
      34             : // self
      35             : //
      36             : #include    "snaplogger/buffer_appender.h"
      37             : 
      38             : #include    "snaplogger/guard.h"
      39             : 
      40             : 
      41             : // snapdev lib
      42             : //
      43             : #include    <snapdev/lockfile.h>
      44             : 
      45             : 
      46             : // C++ lib
      47             : //
      48             : #include    <iostream>
      49             : 
      50             : 
      51             : // C lib
      52             : //
      53             : #include    <fcntl.h>
      54             : #include    <sys/stat.h>
      55             : #include    <sys/types.h>
      56             : 
      57             : 
      58             : // last include
      59             : //
      60             : #include    <snapdev/poison.h>
      61             : 
      62             : 
      63             : 
      64             : namespace snaplogger
      65             : {
      66             : 
      67             : 
      68             : namespace
      69             : {
      70             : 
      71             : 
      72             : 
      73           9 : APPENDER_FACTORY(buffer);
      74             : 
      75             : 
      76             : 
      77             : }
      78             : // no name namespace
      79             : 
      80             : 
      81             : 
      82          20 : buffer_appender::buffer_appender(std::string const name)
      83          20 :     : appender(name, "buffer")
      84             : {
      85          20 : }
      86             : 
      87             : 
      88       42859 : void buffer_appender::process_message(message const & msg, std::string const & formatted_message)
      89             : {
      90       42859 :     snap::NOT_USED(msg);
      91             : 
      92       85718 :     guard g;
      93             : 
      94       42859 :     *this << formatted_message;
      95       42859 : }
      96             : 
      97             : 
      98       52035 : bool buffer_appender::empty() const
      99             : {
     100       52035 :     return rdbuf()->in_avail() == 0;
     101             : }
     102             : 
     103             : 
     104       94823 : void buffer_appender::clear(bool keep_buffer)
     105             : {
     106       94823 :     std::stringstream::clear();     // ios bits
     107       94823 :     if(keep_buffer)
     108             :     {
     109             :         // just rewind (i.e. this means all the buffers are kept)
     110             :         //
     111           1 :         seekp(0);
     112             :     }
     113             :     else
     114             :     {
     115       94822 :         std::stringstream::str(std::string());             // buffer
     116             :     }
     117       94823 : }
     118             : 
     119             : 
     120       42805 : std::string buffer_appender::str()
     121             : {
     122             :     // tellp() may not be the end of the file if clear(true) was called
     123             :     //
     124       42805 :     pos_type const pos(tellp());
     125       42805 :     std::string result(pos, '\0');
     126       42805 :     seekg(0);
     127       42805 :     read(const_cast<char *>(result.data()), pos);
     128       42805 :     return result;
     129             : }
     130             : 
     131             : 
     132           1 : void buffer_appender::str(std::string const & buf)
     133             : {
     134             :     // note: std::stringstream::str(buf) may reset all the buffers, here
     135             :     //       we make sure the buffers are kept and we just write buf in
     136             :     //       them after adjusting the output seek position. The STL may
     137             :     //       do it right for large `buf`, though, but we do not expect
     138             :     //       the `buf` parameter to ever be very large.
     139             :     //
     140           1 :     clear(true);
     141           1 :     *this << buf;
     142           1 : }
     143             : 
     144             : 
     145             : 
     146           6 : } // snaplogger namespace
     147             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13