iipsrv 1.2
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
Logger.h
1/*
2 Basic Header-Only Logging Class
3
4 Copyright (C) 2019-2023 Ruven Pillay
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19*/
20
21
22#ifndef _LOGGER_H
23#define _LOGGER_H
24
25#include <ostream>
26#include <fstream>
27#include <streambuf>
28#include <string>
29
30
31
32#ifdef HAVE_SYSLOG_H
33
34#include <syslog.h>
35
37class SyslogStream : public std::streambuf {
38
39 private:
40 std::string _buf;
41 int _level;
42
43
44 public:
45
47 SyslogStream() : _level( LOG_DEBUG ) { };
48
50 void open(){
51 openlog( "iipsrv", LOG_NDELAY | LOG_PID, LOG_USER );
52 }
53
55 void close(){ closelog(); };
56
58 int sync(){
59 if (_buf.size()) {
60 syslog( _level, "%s", _buf.c_str() );
61 _buf.erase();
62 }
63 return 0;
64 }
65
67 int_type overflow( int_type c ){
68 if( c == traits_type::eof() ) sync();
69 else _buf += static_cast<char>(c);
70 return c;
71 }
72
73};
74
75#endif
76
77
79class Logger : public std::ostream {
80
81 private:
82
83#ifdef HAVE_SYSLOG_H
85 SyslogStream _syslogStream;
86#endif
87
89 std::ofstream _fstream;
90
92 enum Type {
93#ifdef HAVE_SYSLOG_H
94 SYSLOG,
95#endif
96 FILE
97 };
98 Type _type;
99
100
101 public:
102
104 Logger() : std::ostream( NULL ) {};
105
106
108 ~Logger() { this->close(); };
109
110
112
114 void open( const std::string& file ){
115
116#ifdef HAVE_SYSLOG_H
117 // Open a syslog connection - assign syslog stream to our stream buffer
118 if( file == "syslog" ){
119 _type = SYSLOG;
120 this->rdbuf( &_syslogStream );
121 _syslogStream.open();
122 }
123 // Create an output file stream and assign it to our stream buffer
124 else{
125#endif
126 _type = FILE;
127 _fstream.open( file.c_str(), ios_base::app );
128 std::streambuf *buffer = _fstream.rdbuf();
129 this->rdbuf( buffer );
130#ifdef HAVE_SYSLOG_H
131 }
132#endif
133 };
134
135
137 void close(){
138 switch( _type ){
139#ifdef HAVE_SYSLOG_H
140 case SYSLOG:
141 _syslogStream.close();
142 break;
143#endif
144 default:
145 _fstream.close();
146 }
147 };
148
149
151
153 std::string types(){
154 std::string types = "file";
155#ifdef HAVE_SYSLOG_H
156 types += ", syslog";
157#endif
158 return types;
159 };
160
161};
162
163#endif
Logger class - handles ofstreams and syslog.
Definition: Logger.h:79
std::string types()
Provide a list of available logging types.
Definition: Logger.h:153
Logger()
Constructor - derived from std::ostream.
Definition: Logger.h:104
~Logger()
Destructor - close our logging stream.
Definition: Logger.h:108
void open(const std::string &file)
Open our logging output.
Definition: Logger.h:114
void close()
Close depending on type.
Definition: Logger.h:137