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 43789 : void buffer_appender::process_message(message const & msg, std::string const & formatted_message)
89 : {
90 43789 : snapdev::NOT_USED(msg);
91 :
92 87578 : guard g;
93 :
94 43789 : *this << formatted_message;
95 43789 : }
96 :
97 :
98 54146 : bool buffer_appender::empty() const
99 : {
100 54146 : return rdbuf()->in_avail() == 0;
101 : }
102 :
103 :
104 97864 : void buffer_appender::clear(bool keep_buffer)
105 : {
106 97864 : std::stringstream::clear(); // ios bits
107 97864 : 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 97863 : std::stringstream::str(std::string()); // buffer
116 : }
117 97864 : }
118 :
119 :
120 43735 : std::string buffer_appender::str()
121 : {
122 : // tellp() may not be the end of the file if clear(true) was called
123 : //
124 43735 : pos_type const pos(tellp());
125 43735 : std::string result(pos, '\0');
126 43735 : seekg(0);
127 43735 : read(const_cast<char *>(result.data()), pos);
128 43735 : 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
|