00001 #include "Format.hh"
00002 #include "FormString.hh"
00003 #include <sys/time.h>
00004 #include <time.h>
00005
00006
00007 using namespace Logging;
00008
00009
00010 ClassImp(Format)
00011 ClassImp(FmtLevelString)
00012 ClassImp(FmtFileName)
00013 ClassImp(FmtFilePath)
00014 ClassImp(FmtLineNumber)
00015 ClassImp(FmtLogName)
00016 ClassImp(FmtDate)
00017 ClassImp(FmtTime)
00018 ClassImp(FmtLevelColor)
00019 ClassImp(FmtHtml)
00020 ClassImp(FmtSpace)
00021 ClassImp(FmtTab)
00022 ClassImp(FmtBar)
00023 ClassImp(FmtPadTo)
00024
00025 using std::string;
00026
00027 Logging::Format::~Format ()
00028 {
00029 }
00030
00031
00032
00033
00034
00035 const char kColor_Reset[] = { 0x1B, '[', '0', 'm', 0 };
00036 const char kColor_Bold[] = { 0x1B, '[', '1', 'm', 0 };
00037 const char kColor_Dim[] = { 0x1B, '[', '2', 'm', 0 };
00038 const char kColor_Underline[]= { 0x1B, '[', '3', 'm', 0 };
00039 const char kColor_Blink[] = { 0x1B, '[', '5', 'm', 0 };
00040 const char kColor_Reverse[] = { 0x1B, '[', '7', 'm', 0 };
00041
00042 const char kColor_Black[] = { 0x1B, '[', '3', '0', 'm', 0 };
00043 const char kColor_Red[] = { 0x1B, '[', '3', '1', 'm', 0 };
00044 const char kColor_Green[] = { 0x1B, '[', '3', '2', 'm', 0 };
00045 const char kColor_Yellow[] = { 0x1B, '[', '3', '3', 'm', 0 };
00046 const char kColor_Blue[] = { 0x1B, '[', '3', '4', 'm', 0 };
00047 const char kColor_Magenta[] = { 0x1B, '[', '3', '5', 'm', 0 };
00048 const char kColor_Cyan[] = { 0x1B, '[', '3', '6', 'm', 0 };
00049 const char kColor_White[] = { 0x1B, '[', '3', '7', 'm', 0 };
00050
00051 const char kColor_BgWhite[] = { 0x1B, '[', '4', '0', 'm', 0 };
00052 const char kColor_BgRed[] = { 0x1B, '[', '4', '1', 'm', 0 };
00053 const char kColor_BgGreen[] = { 0x1B, '[', '4', '2', 'm', 0 };
00054 const char kColor_BgYellow[] = { 0x1B, '[', '4', '3', 'm', 0 };
00055 const char kColor_BgBlue[] = { 0x1B, '[', '4', '4', 'm', 0 };
00056 const char kColor_BgMagenta[] = { 0x1B, '[', '4', '5', 'm', 0 };
00057 const char kColor_BgCyan[] = { 0x1B, '[', '4', '6', 'm', 0 };
00058 const char kColor_BgBlack[] = { 0x1B, '[', '4', '7', 'm', 0 };
00059
00060
00061 bool FmtDate::operator()( LogEntry& entry )
00062 {
00063 static struct timeval tv;
00064 gettimeofday(&tv,NULL);
00065 struct tm* ts = localtime(&(tv.tv_sec));
00066
00067 entry.mPrefix += FormString("%04d%02d%02d ",ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday);
00068
00069 return true;
00070 }
00071
00072 bool FmtTime::operator()( LogEntry& entry )
00073 {
00074 static struct timeval tv;
00075 gettimeofday(&tv,NULL);
00076 struct tm* ts = localtime(&(tv.tv_sec));
00077
00078 entry.mPrefix += FormString("%02d:%02d:%02d ",
00079 ts->tm_hour, ts->tm_min, ts->tm_sec);
00080 return true;
00081 }
00082
00083 bool FmtLevelColor::operator()( LogEntry& entry )
00084 {
00085 string s;
00086 if(entry.mLevel>=kFatal) { s += kColor_Reverse; }
00087 else if(entry.mLevel>=kError) { s += kColor_Bold; s += kColor_Magenta; }
00088 else if(entry.mLevel>=kWarning) { s += kColor_Bold; }
00089 else if(entry.mLevel>=kInfo) { s += kColor_Reset; }
00090 else if(entry.mLevel>=kMonitor) { s += kColor_Blue; }
00091 else if(entry.mLevel>=kDebug1) { s += kColor_Cyan; }
00092 else if(entry.mLevel>=kDebug2) { s += kColor_Blue; }
00093 else if(entry.mLevel>=kDebug3) { s += kColor_Dim; s+= kColor_Blue; }
00094 else { s += kColor_Dim; s+= kColor_Blue; }
00095
00096 entry.mPrefix += s;
00097 entry.mIndentAdj -= s.length();
00098 entry.mSuffix.insert(0,kColor_Reset);
00099 return true;
00100 }
00101
00102
00103 bool FmtPadTo::operator()( LogEntry& entry )
00104 {
00106 int len = entry.mPrefix.length() + entry.mIndentAdj;
00107 for(int i=0;i<mTo-len;i++) entry.mPrefix+=' ';
00108 return true;
00109 }
00110
00111
00112 string FmtHtml::Header()
00113 {
00114 string out;
00115 out += "<html>\n";
00116 out += "<head>\n";
00117 out += "<style type=\"text/css\">\n";
00118 out += ".Fatal { color:#C31317 }\n";
00119 out += ".Error { color:#CD6736 }\n";
00120 out += ".Warning{ color:#C5C215 }\n";
00121 out += ".Info { color:#000 }\n";
00122 out += ".Debug { color:#559914 }\n";
00123 out += ".Verbose{ color:#C3C3C3 }\n";
00124 out += "</style>\n";
00125 out += "</head>\n";
00126 out += "<body>\n";
00127 out += "<table bordersize=\"1\">\n";
00128 return out;
00129 }
00130
00131 string FmtHtml::Footer()
00132 {
00133 string out;
00134 out += "</table>\n";
00135 out += "</body>\n";
00136 out += "</html>\n";
00137 return out;
00138
00139 }
00140
00141 bool FmtHtml::operator()( LogEntry& entry )
00142 {
00144 const string kurl = "http://minimac1.phy.tufts.edu/~tagg/doc/";
00145
00146
00147 entry.mPrefix.insert(0,"<tr><td>");
00148 entry.mPrefix += "</td><td>";
00149
00150 if(entry.mFilename) {
00151 string filepath(entry.mFilename);
00152
00153 string::size_type n = filepath.length();
00154 string::size_type p1 = filepath.find_last_of('/');
00155 string::size_type p2 = filepath.find_last_of('/',p1-1);
00156 string::size_type p3 = filepath.find_last_of('/',p2-1);
00157 if(p1==string::npos) p1=0;
00158 if(p2==string::npos) p2=0;
00159 if(p3==string::npos) p3=0;
00160 string filename(filepath,p1+1,n-p1);
00161 string pkgname(filepath,p3+1,p2-p3);
00162 string link;
00163
00164 string filelink = filename;
00165 filelink.replace(filename.find("."),1,"_8");
00166 filelink += "-source.html";
00167
00168 link += "<a href=\"";
00169 link += kurl;
00170 link += pkgname;
00171 link += "/html/";
00172 link += filelink;
00173 link += "#";
00174 link += FormString("%d",10000 + entry.mLine);
00175 link += "\">";
00176 link += filename;
00177 link += ":";
00178 link += FormString("%d",entry.mLine);
00179 link += "</a></td><td>";
00180
00181 entry.mPrefix += link;
00182 } else {
00183 entry.mPrefix += "</td><td>";
00184 }
00185
00186 string classname;
00187 if(entry.mLevel>=kFatal) { classname = "Fatal"; }
00188 else if(entry.mLevel>=kError) { classname = "Error"; }
00189 else if(entry.mLevel>=kWarning) { classname = "Warning"; }
00190 else if(entry.mLevel>=kInfo) { classname = "Info"; }
00191 else if(entry.mLevel>=kMonitor) { classname = "Monitor"; }
00192 else if(entry.mLevel>=kDebug3) { classname = "Debug"; }
00193 else { classname = "Verbose"; }
00194
00195
00196 string::size_type p;
00197 while( (p = entry.mMsg.find('\n')) != string::npos ) {
00198 entry.mMsg.replace(p,1,"<br/>");
00199 }
00200
00201 entry.mPrefix += "</td><td class=\"" + classname + "\">";
00202 entry.mSuffix += "</td></tr>";
00203 return true;
00204 }
00205
00206