Skip to content

Commit

Permalink
improve syslogbuf code
Browse files Browse the repository at this point in the history
  • Loading branch information
yurial committed Nov 18, 2020
1 parent 4f5f4bc commit 6d6d82e
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/syslogpp.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
/*
libsyslogpp - c++ wrapper for standard syslog interface
copyright (c) 2013 yuri diachenko
Expand Down Expand Up @@ -49,9 +50,13 @@ class syslogbuf:
protected:
int m_prio;

inline int sync();
public:
inline syslogbuf(int prio);
inline void setprio(int prio);
inline int sync();
inline size_t size() const;
inline bool empty() const;
inline void clear();
};

class syslog_t:
Expand All @@ -73,6 +78,7 @@ inline void operator () (int prio, const char* fmt, ...) const;
};

syslogbuf::syslogbuf(int prio):
std::stringbuf( std::ios_base::out ),
m_prio( prio )
{
}
Expand All @@ -83,14 +89,28 @@ pubsync();
m_prio = prio;
}

size_t syslogbuf::size() const
{
return pptr() - pbase();
}

bool syslogbuf::empty() const
{
return 0 == size();
}

void syslogbuf::clear()
{
setp( pbase(), epptr() );
}

int syslogbuf::sync()
{
if ( in_avail() )
if ( !empty() )
{
sputc( '\0' );
syslog( m_prio, "%s", gptr() );
seekoff( 0, std::ios_base::beg, std::ios_base::in | std::ios_base::out );
setg( pbase(), pbase(), pbase() );
syslog( m_prio, "%s", pbase() );
clear();
}
return 0; //success
}
Expand Down

0 comments on commit 6d6d82e

Please sign in to comment.