]> granicus.if.org Git - icinga2/blob - lib/base/stdiostream.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / stdiostream.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/stdiostream.hpp"
4 #include "base/objectlock.hpp"
5
6 using namespace icinga;
7
8 /**
9  * Constructor for the StdioStream class.
10  *
11  * @param innerStream The inner stream.
12  * @param ownsStream Whether the new object owns the inner stream. If true
13  *                                       the stream's destructor deletes the inner stream.
14  */
15 StdioStream::StdioStream(std::iostream *innerStream, bool ownsStream)
16         : m_InnerStream(innerStream), m_OwnsStream(ownsStream)
17 { }
18
19 StdioStream::~StdioStream()
20 {
21         Close();
22 }
23
24 size_t StdioStream::Read(void *buffer, size_t size, bool allow_partial)
25 {
26         ObjectLock olock(this);
27
28         m_InnerStream->read(static_cast<char *>(buffer), size);
29         return m_InnerStream->gcount();
30 }
31
32 void StdioStream::Write(const void *buffer, size_t size)
33 {
34         ObjectLock olock(this);
35
36         m_InnerStream->write(static_cast<const char *>(buffer), size);
37 }
38
39 void StdioStream::Close()
40 {
41         Stream::Close();
42
43         if (m_OwnsStream) {
44                 delete m_InnerStream;
45                 m_OwnsStream = false;
46         }
47 }
48
49 bool StdioStream::IsDataAvailable() const
50 {
51         return !IsEof();
52 }
53
54 bool StdioStream::IsEof() const
55 {
56         return !m_InnerStream->good();
57 }