automotive-message-broker  0.14.0
debugout.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2012 Intel Corporation
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8 
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef DEBUGOUT__H__
20 #define DEBUGOUT__H__
21 
22 #include <string>
23 #include <iostream>
24 #include <fstream>
25 #include <sstream>
26 #include <stdexcept>
27 #include "timestamp.h"
28 
29 using namespace std;
30 
31 void debugOut(const string &message);
32 
63 namespace amb
64 {
70 void deprecateMethod(const string &methodName, const std::string & version);
71 }
72 
73 class DebugOut
74 {
75 public:
76 
80  static const int Error;
81 
85  static const int Warning;
86 
87  DebugOut(int debugLevel = 4)
88  {
89  mDebugLevel = debugLevel;
90 
91  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
92  {
93  ostream out(buf);
94  out.precision(15);
95  out<<bufferTime(amb::currentTime())<<" | ";
96 
97  if(mDebugLevel == Error)
98  out<<"ERROR ";
99  if(mDebugLevel == Warning)
100  out<<"WARNING ";
101  }
102  }
103  DebugOut const& operator << (const string &message) const
104  {
105  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
106  {
107  ostream out(buf);
108  out.precision(15);
109  out<<message;
110  }
111  return *this;
112  }
113 
114  DebugOut const& operator << (ostream & (*manip)(std::ostream&)) const
115  {
116 
117 
118  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
119  {
120  ostream out(buf);
121  out.precision(15);
122  out<<endl;
123 
124  if((mDebugLevel == Error && throwErr))
125  {
126  throw std::runtime_error("Abort on Error is set");
127  }
128  else if ((mDebugLevel == Warning && throwWarn))
129  {
130  throw std::runtime_error("Abort on Warning is set");
131  }
132  }
133  return *this;
134  }
135 
136  DebugOut const & operator << (double val) const
137  {
138  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
139  {
140  ostream out(buf);
141  out.precision(15);
142  out<<val<<" ";
143  }
144  return *this;
145  }
146 
147  static void setDebugThreshhold(int th)
148  {
149  debugThreshhold = th;
150  }
151 
152  static void setOutput(ostream &o)
153  {
154  buf = o.rdbuf();
155  }
156 
157  static void setThrowWarn(bool v)
158  {
159  throwWarn = v;
160  }
161 
162  static void setThrowErr(bool v)
163  {
164  throwErr = v;
165  }
166 
167  static const int getDebugThreshhold()
168  {
169  return debugThreshhold;
170  }
171 
172 private:
173 
174  std::string bufferTime(double time)
175  {
176  ostringstream f;
177 
178  f.precision(15);
179 
180  f<<time;
181 
182  while(f.str().length() <= 15)
183  {
184  f<<" ";
185  }
186 
187  return f.str();
188  }
189 
190  static int debugThreshhold;
191  static std::streambuf *buf;
192  static bool throwWarn;
193  static bool throwErr;
194  int mDebugLevel;
195 };
196 
197 #endif
198 
Definition: picojson.h:1002
static const int Error
Error use when essential functionality is blocked.
Definition: debugout.h:80
static const int Warning
Warning use when non-essential functionality is bocked, or when workarounds exist.
Definition: debugout.h:85
Definition: abstractpropertytype.h:256
void deprecateMethod(const string &methodName, const std::string &version)
deprecateMethod prints warning if method is used. Throws if version >= PROJECT_SERIES ...
Definition: debugout.h:73