IBR-DTNSuite  0.10
SyslogStream.h
Go to the documentation of this file.
1 /*
2  * SyslogStream.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef IBRCOMMON_SYSLOGSTREAM_H
23 #define IBRCOMMON_SYSLOGSTREAM_H
24 
25 #include <vector>
26 #include <syslog.h>
27 #include <iostream>
28 #include <string.h>
29 
30 namespace ibrcommon
31 {
33  {
34  SYSLOG_EMERG = LOG_EMERG, /* system is unusable */
35  SYSLOG_ALERT = LOG_ALERT, /* action must be taken immediately */
36  SYSLOG_CRIT = LOG_CRIT, /* critical conditions */
37  SYSLOG_ERR = LOG_ERR, /* error conditions */
38  SYSLOG_WARNING = LOG_WARNING, /* warning conditions */
39  SYSLOG_NOTICE = LOG_NOTICE, /* normal but significant condition */
40  SYSLOG_INFO = LOG_INFO, /* informational */
41  SYSLOG_DEBUG = LOG_DEBUG /* debug-level messages */
42  };
43 
44  std::ostream &operator<<(std::ostream &stream, const SyslogPriority &prio);
45 
46  class SyslogStream : public std::streambuf
47  {
48  private:
49  enum { BUF_SIZE = 256 };
50 
51  public:
52  SyslogStream();
53  virtual ~SyslogStream();
54 
55  static std::ostream& getStream();
56  static void open(char *name, int option, int facility);
57 
58  void setPriority(const SyslogPriority &prio);
59 
60  protected:
61  virtual int sync();
62 
63  private:
64  void writeToDevice();
65  std::vector< char_type > m_buf;
66  SyslogPriority _prio;
67  };
68 
69  extern std::ostream &slog;
70 }
71 
72 #endif /* _SYSLOGSTREAM_H */
73