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/>.
31 // This constant is used for allocating memory in Printer::printf
32 #define FORMAT_BUFSIZE 100
38 Out::Printer::Printer(void printer(std::string
))
40 this->printer
= printer
;
44 Out::Printer::operator<<(std::string msg
)
51 Out::Printer::operator<<(const char *msg
)
58 Out::Printer::operator<<(char msg
)
60 this->printer(std::string(1, msg
));
65 Out::Printer::operator<<(int msg
)
67 std::ostringstream tmp
;
69 this->printer(tmp
.str());
74 Out::Printer::operator<<(short msg
)
76 std::ostringstream tmp
;
78 this->printer(tmp
.str());
83 Out::Printer::operator<<(long msg
)
85 std::ostringstream tmp
;
87 this->printer(tmp
.str());
92 Out::Printer::operator<<(long long msg
)
94 std::ostringstream tmp
;
96 this->printer(tmp
.str());
101 Out::Printer::operator<<(unsigned int msg
)
103 std::ostringstream tmp
;
105 this->printer(tmp
.str());
110 Out::Printer::operator<<(unsigned short msg
)
112 std::ostringstream tmp
;
114 this->printer(tmp
.str());
119 Out::Printer::operator<<(unsigned long msg
)
121 std::ostringstream tmp
;
123 this->printer(tmp
.str());
128 Out::Printer::operator<<(unsigned long long msg
)
130 std::ostringstream tmp
;
132 this->printer(tmp
.str());
137 Out::Printer::operator<<(float msg
)
139 std::ostringstream tmp
;
141 this->printer(tmp
.str());
146 Out::Printer::operator<<(double msg
)
148 std::ostringstream tmp
;
150 this->printer(tmp
.str());
155 Out::Printer::vprintf(const char *format
, va_list args
)
158 int length
= std::strlen(format
) + FORMAT_BUFSIZE
;
159 char *buffer
= new char[length
];
161 result
= vsprintf(buffer
, format
, args
);
165 std::ostringstream tmp
;
167 this->printer(tmp
.str());
174 Out::Printer::printf(const char *format
, ...)
178 va_start(args
, format
);
179 result
= this->vprintf(format
, args
);
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;
211 Out::setFlags(int newflags
)
217 Out::fatalPrinter(std::string msg
)
219 if (!(flags
& FATAL
))
222 // If another command didn't finish its line, fix it for it
223 if((isWriting
| FATAL
) != FATAL
)
225 std::cerr
<< std::endl
;
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
)
250 Out::errorPrinter(std::string msg
)
252 if (!(flags
& ERROR
))
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
;
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
)
283 Out::warningPrinter(std::string msg
)
285 if (!(flags
& WARNING
))
288 // If another command didn't finish its line, fix it for it
289 if((isWriting
| WARNING
) != WARNING
)
291 std::cerr
<< std::endl
;
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
)
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
;
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
)
340 Out::debugPrinter(std::string msg
)
342 if (!(flags
& DEBUG
))
345 // If another command didn't finish its line, fix it for it
346 if((isWriting
| DEBUG
) != DEBUG
)
348 std::cerr
<< std::endl
;
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
)