Moved Printer class into Out class
[openstranded.git] / src / output.cc
blobbe1185cf3c0c830b85eb5fae7d9d63cb2b4f099b
1 /*
2 * See output.hh for more information about this file
4 * Copyright (C) 2009 David Kolossa
6 * This file is part of OpenStranded.
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <cstdarg>
26 #include <cstdio>
27 #include <cstring>
29 #include "output.hh"
31 // This constant is used for allocating memory in Printer::printf
32 #define FORMAT_BUFSIZE 100
35 // Printer class
38 Out::Printer::Printer(void printer(std::string))
40 this->printer = printer;
43 Out::Printer&
44 Out::Printer::operator<<(std::string msg)
46 this->printer(msg);
47 return *this;
50 Out::Printer&
51 Out::Printer::operator<<(const char *msg)
53 this->printer(msg);
54 return *this;
57 Out::Printer&
58 Out::Printer::operator<<(char msg)
60 this->printer(std::string(1, msg));
61 return *this;
64 Out::Printer&
65 Out::Printer::operator<<(int msg)
67 std::ostringstream tmp;
68 tmp << msg;
69 this->printer(tmp.str());
70 return *this;
73 Out::Printer&
74 Out::Printer::operator<<(short msg)
76 std::ostringstream tmp;
77 tmp << msg;
78 this->printer(tmp.str());
79 return *this;
82 Out::Printer&
83 Out::Printer::operator<<(long msg)
85 std::ostringstream tmp;
86 tmp << msg;
87 this->printer(tmp.str());
88 return *this;
91 Out::Printer&
92 Out::Printer::operator<<(long long msg)
94 std::ostringstream tmp;
95 tmp << msg;
96 this->printer(tmp.str());
97 return *this;
100 Out::Printer&
101 Out::Printer::operator<<(unsigned int msg)
103 std::ostringstream tmp;
104 tmp << msg;
105 this->printer(tmp.str());
106 return *this;
109 Out::Printer&
110 Out::Printer::operator<<(unsigned short msg)
112 std::ostringstream tmp;
113 tmp << msg;
114 this->printer(tmp.str());
115 return *this;
118 Out::Printer&
119 Out::Printer::operator<<(unsigned long msg)
121 std::ostringstream tmp;
122 tmp << msg;
123 this->printer(tmp.str());
124 return *this;
127 Out::Printer&
128 Out::Printer::operator<<(unsigned long long msg)
130 std::ostringstream tmp;
131 tmp << msg;
132 this->printer(tmp.str());
133 return *this;
136 Out::Printer&
137 Out::Printer::operator<<(float msg)
139 std::ostringstream tmp;
140 tmp << msg;
141 this->printer(tmp.str());
142 return *this;
145 Out::Printer&
146 Out::Printer::operator<<(double msg)
148 std::ostringstream tmp;
149 tmp << msg;
150 this->printer(tmp.str());
151 return *this;
155 Out::Printer::vprintf(const char *format, va_list args)
157 int result;
158 int length = std::strlen(format) + FORMAT_BUFSIZE;
159 char *buffer = new char[length];
161 result = vsprintf(buffer, format, args);
163 if (result)
165 std::ostringstream tmp;
166 tmp << buffer;
167 this->printer(tmp.str());
169 delete[] buffer;
170 return result;
174 Out::Printer::printf(const char *format, ...)
176 int result;
177 va_list args;
178 va_start(args, format);
179 result = this->vprintf(format, args);
180 va_end(args);
181 return result;
187 // Out class
190 Out::Printer Out::fatal = Printer(Out::fatalPrinter);
191 Out::Printer Out::error = Printer(Out::errorPrinter);
192 Out::Printer Out::warning = Printer(Out::warningPrinter);
193 Out::Printer Out::msg = Printer(Out::msgPrinter);
194 Out::Printer Out::debug = Printer(Out::debugPrinter);
196 int Out::isWriting = 0;
197 int Out::flags = 0;
200 Out::Out()
205 Out::getFlags()
207 return flags;
210 void
211 Out::setFlags(int newflags)
213 flags = newflags;
216 void
217 Out::fatalPrinter(std::string msg)
219 if (!(flags & FATAL))
220 return;
222 // If another command didn't finish its line, fix it for it
223 if((isWriting | FATAL) != FATAL)
225 std::cerr << std::endl;
226 isWriting = 0;
227 debug << "Printed an unfinished line" << endl;
230 // Only write prefix one time per line
231 if((isWriting & FATAL) == 0)
233 std::cerr << "FATAL: ";
236 // If msg is a finished line reset flags, else set flags
237 if(msg.find('\n') == std::string::npos)
239 isWriting = FATAL;
241 else
243 isWriting = 0;
246 std::cerr << msg;
249 void
250 Out::errorPrinter(std::string msg)
252 if (!(flags & ERROR))
253 return;
255 // If another command didn't finish its line, fix it for it
256 if((isWriting | ERROR) != ERROR)
258 std::cerr << std::endl;
259 debug << "Printed an unfinished line" << endl;
260 isWriting = 0;
263 // Only write prefix one time per line
264 if((isWriting & ERROR) == 0)
266 std::cerr << "ERROR: ";
269 // If msg is a finished line reset flags, else set flags
270 if(msg.find('\n') == std::string::npos)
272 isWriting = ERROR;
274 else
276 isWriting = 0;
279 std::cerr << msg;
282 void
283 Out::warningPrinter(std::string msg)
285 if (!(flags & WARNING))
286 return;
288 // If another command didn't finish its line, fix it for it
289 if((isWriting | WARNING) != WARNING)
291 std::cerr << std::endl;
292 isWriting = 0;
293 debug << "Printed an unfinished line" << endl;
296 // Only write prefix one time per line
297 if((isWriting & WARNING) == 0)
299 std::cerr << "WARNING: ";
302 // If msg is a finished line reset flags, else set flags
303 if(msg.find('\n') == std::string::npos)
305 isWriting = WARNING;
307 else
309 isWriting = 0;
312 std::cerr << msg;
315 void
316 Out::msgPrinter(std::string msg)
318 // If another command didn't finish its line, fix it for it
319 if((isWriting | MSG) != MSG)
321 std::cerr << std::endl;
322 isWriting = 0;
323 debug << "Printed an unfinished line" << endl;
326 // If msg is a finished line reset flags, else set flags
327 if(msg.find('\n') == std::string::npos)
329 isWriting = MSG;
331 else
333 isWriting = 0;
336 std::cout << msg;
339 void
340 Out::debugPrinter(std::string msg)
342 if (!(flags & DEBUG))
343 return;
345 // If another command didn't finish its line, fix it for it
346 if((isWriting | DEBUG) != DEBUG)
348 std::cerr << std::endl;
349 isWriting = 0;
350 debug << "Printed an unfinished line" << endl;
353 // Only write prefix one time per line
354 if((isWriting & DEBUG) == 0)
356 std::cerr << "DEBUG: ";
359 // If msg is a finished line reset flags, else set flags
360 if(msg.find('\n') == std::string::npos)
362 isWriting = DEBUG;
364 else
366 isWriting = 0;
369 std::cerr << msg;