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