IBR-DTNSuite  0.8
ibrcommon/ibrcommon/SyslogStream.h
Go to the documentation of this file.
00001 /*
00002  * File:   SyslogStream.h
00003  * Author: morgenro
00004  *
00005  * Created on 17. November 2009, 13:43
00006  *
00007  * Copyright 2011 Johannes Morgenroth, IBR, TU Braunschweig
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #ifndef IBRCOMMON_SYSLOGSTREAM_H
00023 #define IBRCOMMON_SYSLOGSTREAM_H
00024 
00025 #include <vector>
00026 #include <syslog.h>
00027 #include <iostream>
00028 #include <string.h>
00029 
00030 namespace ibrcommon
00031 {
00032         enum SyslogPriority
00033         {
00034                 SYSLOG_EMERG =  LOG_EMERG,      /* system is unusable */
00035                 SYSLOG_ALERT =  LOG_ALERT,      /* action must be taken immediately */
00036                 SYSLOG_CRIT =   LOG_CRIT,       /* critical conditions */
00037                 SYSLOG_ERR =    LOG_ERR,        /* error conditions */
00038                 SYSLOG_WARNING =    LOG_WARNING,        /* warning conditions */
00039                 SYSLOG_NOTICE = LOG_NOTICE,     /* normal but significant condition */
00040                 SYSLOG_INFO =   LOG_INFO,       /* informational */
00041                 SYSLOG_DEBUG =  LOG_DEBUG       /* debug-level messages */
00042         };
00043 
00044         std::ostream &operator<<(std::ostream &stream, const SyslogPriority &prio);
00045 
00046         class SyslogStream : public std::streambuf
00047         {
00048         private:
00049                 enum { BUF_SIZE = 256 };
00050 
00051         public:
00052                 SyslogStream();
00053                 virtual ~SyslogStream();
00054 
00055                 static std::ostream& getStream();
00056                 static void open(char *name, int option, int facility);
00057 
00058                 void setPriority(const SyslogPriority &prio);
00059 
00060         protected:
00061                 virtual int sync();
00062 
00063         private:
00064                 void writeToDevice();
00065                 std::vector< char_type > m_buf;
00066                 SyslogPriority _prio;
00067         };
00068 
00069         extern std::ostream &slog;
00070 }
00071 
00072 #endif  /* _SYSLOGSTREAM_H */
00073