STIR 6.4.0
TextWriter.h
1#ifndef TEXT_WRITER_TYPES
2#define TEXT_WRITER_TYPES
3
4#include <string.h>
5
6#include <fstream>
7#include <iostream>
8#include <string>
9
10#include "stir/common.h"
11
12#define DEFAULT_STREAM std::cerr
13
14START_NAMESPACE_STIR
15
16enum OUTPUT_CHANNEL
17{
18 INFORMATION_CHANNEL,
19 WARNING_CHANNEL,
20 ERROR_CHANNEL
21};
22
23class aTextWriter
24{
25public:
26 virtual ~aTextWriter() {}
27 virtual void write(const char* text) const = 0;
28};
29
30class TextPrinter : public aTextWriter
31{
32public:
33 TextPrinter(const char* s = 0)
34 : _stream(0)
35 {
36 if (s)
37 {
38 if (strcmp(s, "stdout") == 0 || strcmp(s, "cout") == 0)
39 _stream = 1;
40 else if (strcmp(s, "stderr") == 0 || strcmp(s, "cerr") == 0)
41 _stream = 2;
42 }
43 }
44 void write(const char* text) const override
45 {
46 switch (_stream)
47 {
48 case 1:
49 std::cout << text;
50 break;
51 case 2:
52 std::cerr << text;
53 break;
54 default:
55 DEFAULT_STREAM << text;
56 }
57 }
58
59private:
60 int _stream;
61};
62
63class TextWriter : public aTextWriter
64{
65public:
66 std::ostream* out;
67 TextWriter(std::ostream* os = 0)
68 : out(os)
69 {}
70 void write(const char* text) const override
71 {
72 if (out)
73 {
74 (*out) << text;
75 (*out).flush();
76 }
77 }
78};
79
80class TextWriterHandle
81{
82public:
83 TextWriterHandle() { init_(); }
84 void set_information_channel(aTextWriter* info) { information_channel_ = info; }
85 void* information_channel_ptr() { return (void*)information_channel_; }
86 void set_warning_channel(aTextWriter* warn) { warning_channel_ = warn; }
87 void* warning_channel_ptr() { return (void*)warning_channel_; }
88 void set_error_channel(aTextWriter* errr) { error_channel_ = errr; }
89 void* error_channel_ptr() { return (void*)error_channel_; }
90 void print_information(const char* text)
91 {
92 if (information_channel_)
93 information_channel_->write(text);
94 else
95 std::cout << text;
96 }
97 void print_warning(const char* text)
98 {
99 if (warning_channel_)
100 warning_channel_->write(text);
101 else
102 std::cerr << text;
103 }
104 void print_error(const char* text)
105 {
106 if (error_channel_)
107 error_channel_->write(text);
108 else
109 std::cerr << text;
110 }
111
112private:
113 static aTextWriter* information_channel_;
114 static aTextWriter* warning_channel_;
115 static aTextWriter* error_channel_;
116 static void init_()
117 {
118 static bool initialized = false;
119 if (!initialized)
120 {
121 information_channel_ = 0;
122 warning_channel_ = 0;
123 error_channel_ = 0;
124 initialized = true;
125 }
126 }
127};
128
129void writeText(const char* text, OUTPUT_CHANNEL channel = INFORMATION_CHANNEL);
130
131END_NAMESPACE_STIR
132
133#endif
basic configuration include file
void info(const STRING &string, const int verbosity_level=1)
Use this function for writing informational messages.
Definition info.h:51