automotive-message-broker  0.13
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
debugout.h
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 
57 class DebugOut
58 {
59 public:
60 
64  static const int Error;
65 
69  static const int Warning;
70 
71  DebugOut(int debugLevel = 4)
72  {
73  mDebugLevel = debugLevel;
74 
75  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
76  {
77  ostream out(buf);
78  out.precision(15);
79  out<<bufferTime(amb::currentTime())<<" | ";
80 
81  if(mDebugLevel == Error)
82  out<<"ERROR ";
83  if(mDebugLevel == Warning)
84  out<<"WARNING ";
85  }
86  }
87  DebugOut const& operator << (const string &message) const
88  {
89  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
90  {
91  ostream out(buf);
92  out.precision(15);
93  out<<message<<" ";
94  }
95  return *this;
96  }
97 
98  DebugOut const& operator << (ostream & (*manip)(std::ostream&)) const
99  {
100 
101 
102  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
103  {
104  ostream out(buf);
105  out.precision(15);
106  out<<endl;
107 
108  if((mDebugLevel == Error && throwErr))
109  {
110  throw std::runtime_error("Abort on Error is set");
111  }
112  else if ((mDebugLevel == Warning && throwWarn))
113  {
114  throw std::runtime_error("Abort on Warning is set");
115  }
116  }
117  return *this;
118  }
119 
120  DebugOut const & operator << (double val) const
121  {
122  if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
123  {
124  ostream out(buf);
125  out.precision(15);
126  out<<val<<" ";
127  }
128  return *this;
129  }
130 
131  static void setDebugThreshhold(int th)
132  {
133  debugThreshhold = th;
134  }
135 
136  static void setOutput(ostream &o)
137  {
138  buf = o.rdbuf();
139  }
140 
141  static void setThrowWarn(bool v)
142  {
143  throwWarn = v;
144  }
145 
146  static void setThrowErr(bool v)
147  {
148  throwErr = v;
149  }
150 
151  static const int getDebugThreshhold()
152  {
153  return debugThreshhold;
154  }
155 
156 private:
157 
158  std::string bufferTime(double time)
159  {
160  ostringstream f;
161 
162  f.precision(15);
163 
164  f<<time;
165 
166  while(f.str().length() <= 15)
167  {
168  f<<" ";
169  }
170 
171  return f.str();
172  }
173 
174  static int debugThreshhold;
175  static std::streambuf *buf;
176  static bool throwWarn;
177  static bool throwErr;
178  int mDebugLevel;
179 };
180 
181 
182 
183 
184 
185 #endif
static const int Error
Error use when essential functionality is blocked.
Definition: debugout.h:64
static const int Warning
Warning use when non-essential functionality is bocked, or when workarounds exist.
Definition: debugout.h:69
The DebugOut class represents a class used for outputing debug information The specified debug level ...
Definition: debugout.h:57