00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifdef _MSC_VER
00044 #pragma warning(disable:4786)
00045 #endif
00046
00047
00048 #include <limits.h>
00049 #include <stdarg.h>
00050 #include <string>
00051 #include <vector>
00052 #include <iostream>
00053 #include "abkcommon.h"
00054 #include "verbosity.h"
00055 Verbosity& Verbosity::operator=(const Verbosity& v)
00056 {
00057 abkwarn(v.getNumTypes()<=_levels.size(),
00058 " Losing debug levels while assigning Verbosities\n Use copy-construction");
00059 for(unsigned k=0;k<v.getNumTypes();k++)
00060 _levels[k]=v._levels[k];
00061 _levels.erase(_levels.begin()+v.getNumTypes(),
00062 _levels.begin()+getNumTypes());
00063 return *this;
00064 }
00065
00066 ostream& operator<<(ostream& os, const Verbosity& verbsty)
00067 {
00068 unsigned numLevels=verbsty._levels.size();
00069
00070 os << " Verbosity levels : " << numLevels << endl;
00071 os << " For actions : " << verbsty.forActions << ", "
00072 << " For sys resources : " << verbsty.forSysRes << ", "
00073 << " For major stats : " << verbsty.forMajStats << endl;
00074
00075 if (numLevels > 3)
00076 {
00077 os << " Other levels : " ;
00078 for (unsigned i=3; i!=numLevels; ++i)
00079 {
00080 if ( i % 10 == 0 )
00081 {
00082 if (i) os << endl;
00083 os <<" ";
00084 }
00085 os<<setw(3)<<verbsty._levels[i]<<" ";
00086 }
00087 os << endl;
00088 }
00089
00090 return os;
00091 }
00092
00093 void Verbosity::_ctructFromString(const char* levels)
00094 {
00095 if (levels==NULL ||
00096 !strcmp(levels,"0") || !strcasecmp(levels,"silent"))
00097 {
00098 _levels.erase(_levels.begin()+3,_levels.end());
00099 return;
00100 }
00101
00102 unsigned len=strlen(levels);
00103 char * levs=new char[len+1];
00104 for(unsigned c=0;c<len;c++)
00105 if (levels[c]=='_') levs[c]=' '; else levs[c]=levels[c];
00106 levs[len]='\0';
00107
00108 unsigned numLev=0;
00109 char *start =levs;
00110 char *finish=levs;
00111 while (start<levs+len)
00112 {
00113 unsigned tmp=strtoul(start,&finish,10);
00114 if (start==finish) start++;
00115 else
00116 {
00117 abkfatal(tmp<=INT_MAX,"Verbosity levels can't be >INT_MAX");
00118 abkfatal(numLev<100,"This ctor can't work with 100+ verbosity levels");
00119 _levels[numLev]=tmp;
00120 start=finish;
00121 numLev++;
00122 }
00123 }
00124 abkfatal(numLev>2," Can't ctruct Verbosity: need at least 3 levels\n ");
00125 _levels.erase(_levels.begin()+numLev,_levels.end());
00126 delete[] levs;
00127 }
00128
00129 Verbosity::Verbosity(const char* levels)
00130 // space or underscore-separated unsigneds
00131 :_levels(100,0),
00132 forActions(_levels[0]),forSysRes(_levels[1]),forMajStats(_levels[2])
00133 {
00134 _ctructFromString(levels);
00135 }
00136
00137 Verbosity::Verbosity(int argc, const char *argv[]) // catches -verb
00138 :_levels(100,0),
00139 forActions(_levels[0]),forSysRes(_levels[1]),forMajStats(_levels[2])
00140 {
00141 StringParam verb("verb",argc,argv);
00142 BoolParam helpRequest1("h",argc,argv);
00143 BoolParam helpRequest2("help",argc,argv);
00144
00145 if (helpRequest1.found() || helpRequest2.found())
00146 {
00147 cout << " -verb 1_1_1 | silent " << endl;
00148 _levels.erase(_levels.begin()+3,_levels.end());
00149 return;
00150 }
00151
00152 if (verb.found()) _ctructFromString(verb);
00153 else
00154 {
00155 _levels.erase(_levels.begin()+3,_levels.end());
00156 _levels[0]=_levels[1]=_levels[2]=1;
00157 }
00158 }
00159
00160 Verbosity::Verbosity():
00161 _levels(3,1),
00162 forActions(_levels[0]),forSysRes(_levels[1]),forMajStats(_levels[2])
00163 {
00164 }
00165
00166 Verbosity::Verbosity(const std::vector<unsigned>& levels):
00167 _levels(levels),
00168 forActions(_levels[0]),forSysRes(_levels[1]),forMajStats(_levels[2])
00169 {
00170 abkfatal(levels.size()>=3,
00171 "Can not initialize verbosity --- too few levels\n ");
00172 }
00173
00174
00175 Verbosity::Verbosity(unsigned numLevels, unsigned ForActions,
00176 unsigned ForSysRes, unsigned ForMajStats, ...):
00177 _levels(numLevels,0),
00178 forActions(_levels[0]),forSysRes(_levels[1]),forMajStats(_levels[2])
00179 {
00180 abkfatal(numLevels>=3,"Can not initialize verbosity --- too few levels\n ");
00181 _levels[0]=ForActions;
00182 _levels[1]=ForSysRes;
00183 _levels[2]=ForMajStats;
00184
00185 va_list ap;
00186 va_start(ap,ForMajStats);
00187
00188 for(unsigned k=3;k<numLevels;k++)
00189 {
00190 _levels[k]=va_arg(ap,unsigned);
00191 abkfatal(_levels[k]<INT_MAX/2," Verbosity levels can't be > INT_MAX /2");
00192 }
00193 }
00194
00195 unsigned & Verbosity::operator[](unsigned diagType)
00196 {
00197 abkfatal(diagType<_levels.size(),"Inexistant diagnostic type");
00198 return _levels[diagType];
00199 }
00200