Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00046
00047
00048 #ifndef DBG_h_
00049 #define DBG_h_
00050
00051 #include "sdhlibrary_settings.h"
00052
00053 #if SDH_USE_VCC
00054 # pragma warning(disable : 4996)
00055 #endif
00056
00057
00058
00059
00060
00061 #include <iostream>
00062 #include <iomanip>
00063 #include <string>
00064 #include <stdarg.h>
00065 #include <cstring>
00066 #include <stdlib.h>
00067 #include <cstdio>
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 NAMESPACE_SDH_START
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00130 class VCC_EXPORT cDBG
00131 {
00132 protected:
00133 char const* debug_color;
00134 char const* normal_color;
00135 std::ostream *output;
00136 bool debug_flag;
00137 std::streamsize mywidth;
00138 public:
00139
00148 cDBG(bool flag = false, char const* color = "red", std::ostream *fd = &std::cerr)
00149 {
00150 debug_flag = flag;
00151 SetColor(color);
00152 output = fd;
00153 mywidth = output->width();
00154 }
00155
00156
00157 ~cDBG()
00158 {
00159 output->flush();
00160 }
00161
00166 void SetFlag(bool flag)
00167 {
00168 debug_flag = flag;
00169 }
00170
00171
00175 bool GetFlag(void) const
00176 {
00177 return debug_flag;
00178 }
00179
00187 void SetColor(char const* color)
00188 {
00189 debug_color = GetColor(color);
00190 if (!strcmp(debug_color, ""))
00191
00192 normal_color = debug_color;
00193 else
00194
00195 normal_color = GetColor("normal");
00196 }
00197
00198
00202 void SetOutput(std::ostream *fd)
00203 {
00204 output = fd;
00205 }
00206
00207
00212 void PDM(char const* fmt, ...) SDH__attribute__((format(printf, 2, 3)))
00213
00214
00215
00216
00217
00218
00219
00220 {
00221 if (!debug_flag) return;
00222
00223 char buffer[ 256 ];
00224
00225 va_list arglist;
00226 va_start(arglist, fmt);
00227 #if SDH_USE_VCC
00228 vsnprintf_s(buffer, 256, 256, fmt, arglist);
00229 #else
00230 vsnprintf(buffer, 256, fmt, arglist);
00231 #endif
00232 va_end(arglist);
00233
00234 *output << debug_color << buffer << normal_color << std::flush;
00235 }
00236
00237
00249 char const* GetColor(char const* c)
00250 {
00251 char* sdh_no_color = getenv("SDH_NO_COLOR");
00252 if (sdh_no_color != NULL)
00253 return "";
00254
00255 char* os = getenv("OS");
00256 char* ostype = getenv("OSTYPE");
00257 if (os && (!strncmp(os, "WIN", 3) || !strncmp(os, "Win", 3)) && (! ostype || (ostype && strcmp(ostype, "cygwin"))))
00258 return "";
00259
00260 if (!strcmp(c, "normal")) return "\x1b[0m";
00261 if (!strcmp(c, "bold")) return "\x1b[1m";
00262 if (!strcmp(c, "red")) return "\x1b[31m";
00263 if (!strcmp(c, "green")) return "\x1b[32m";
00264 if (!strcmp(c, "yellow")) return "\x1b[33m";
00265 if (!strcmp(c, "blue")) return "\x1b[34m";
00266 if (!strcmp(c, "magenta")) return "\x1b[35m";
00267 if (!strcmp(c, "cyan")) return "\x1b[36m";
00268 if (!strcmp(c, "white")) return "\x1b[37m";
00269 if (!strcmp(c, "black")) return "\x1b[39m";
00270 if (!strcmp(c, "black_back")) return "\x1b[40m";
00271 if (!strcmp(c, "red_back")) return "\x1b[41m";
00272 if (!strcmp(c, "green_back")) return "\x1b[42m";
00273 if (!strcmp(c, "yellow_back")) return "\x1b[43m";
00274 if (!strcmp(c, "blue_back")) return "\x1b[44m";
00275 if (!strcmp(c, "cyan_back")) return "\x1b[45m";
00276 if (!strcmp(c, "magenta_back")) return "\x1b[46m";
00277 if (!strcmp(c, "white_back")) return "\x1b[47m";
00278
00279
00280 return "";
00281 }
00282
00290 template <typename T>
00291 cDBG& operator<<(T const& v)
00292 {
00293 if (!debug_flag) return *this;
00294
00295 output->width(0);
00296 *output << debug_color;
00297 output->width(mywidth);
00298 *output << v;
00299 mywidth = output->width();
00300 output->width(0);
00301 *output << normal_color << std::flush;
00302 return *this;
00303 }
00304
00305
00306 std::ostream& flush()
00307 {
00308 return output->flush();
00309 }
00310
00311
00312
00326 # define VAR( _d, _var ) \
00327 (_d) << #_var << "='" << _var << "'\n"
00328
00341 # define VAL( _var ) \
00342 #_var << "='" << _var << "' "
00343 };
00344
00346 class VCC_EXPORT cHexByteString
00347 {
00348 char const* bytes;
00349 int len;
00350 public:
00352 cHexByteString(char const* _bytes, int _len) :
00353 bytes(_bytes),
00354 len(_len)
00355 {};
00356
00357 friend VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s);
00358 };
00359
00361 inline VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s)
00362 {
00363
00364
00365 bool is_all_printable_ascii = true;
00366 for (int i = 0; i < s.len; i++)
00367 {
00368 stream << std::hex << std::setw(2) << std::setfill('0') << int(((unsigned char const*)s.bytes)[i]) << " ";
00369 if (s.bytes[i] < 0x20 || ((unsigned char) s.bytes[i]) >= 0x80)
00370 is_all_printable_ascii = false;
00371 }
00372
00373
00374
00375
00376 if (is_all_printable_ascii)
00377 stream << "= \"" << std::string(s.bytes, s.len) << "\"";
00378
00379
00380 return stream << std::dec;
00381 };
00382
00383 NAMESPACE_SDH_END
00384
00385 #endif
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398