SNAP Library , Developer Reference  2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ut.cpp
Go to the documentation of this file.
00001 
00002 // Notifications
00003 void TNotify::OnNotifyFmt(const TNotifyType& Type, const char *FmtStr, ...) {
00004   char Bf[10*1024];
00005   va_list valist;
00006   va_start(valist, FmtStr);
00007   const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
00008   va_end(valist);
00009   if (RetVal!=-1) { OnNotify(Type, TStr(Bf)); }
00010 }
00011 
00012 void TNotify::OnStatusFmt(const char *FmtStr, ...) {
00013   char Bf[10*1024];
00014   va_list valist;
00015   va_start(valist, FmtStr);
00016   const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
00017   va_end(valist);
00018   if (RetVal!=-1) { OnStatus(TStr(Bf)); }
00019 }
00020 
00021 void TNotify::OnLnFmt(const char *FmtStr, ...) {
00022   char Bf[10*1024];
00023   va_list valist;
00024   va_start(valist, FmtStr);
00025   const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
00026   va_end(valist);
00027   if (RetVal!=-1) { OnLn(TStr(Bf)); }
00028 }
00029 
00030 void TNotify::OnTxtFmt(const char *FmtStr, ...) {
00031   char Bf[10*1024];
00032   va_list valist;
00033   va_start(valist, FmtStr);
00034   const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
00035   va_end(valist);
00036   if (RetVal!=-1) { OnTxt(TStr(Bf)); }
00037 }
00038 
00039 TStr TNotify::GetTypeStr(
00040  const TNotifyType& Type, const bool& Brief){
00041   static TStr InfoSStr="I"; static TStr InfoLStr="Information";
00042   static TStr WarnSStr="W"; static TStr WarnLStr="Warning";
00043   static TStr ErrSStr="E"; static TStr ErrLStr="Error";
00044   static TStr StatSStr=""; static TStr StatLStr="Status";
00045   switch (Type){
00046     case ntInfo: if (Brief){return InfoSStr;} else {return InfoLStr;}
00047     case ntWarn: if (Brief){return WarnSStr;} else {return WarnLStr;}
00048     case ntErr: if (Brief){return ErrSStr;} else {return ErrLStr;}
00049     case ntStat: if (Brief){return StatSStr;} else {return StatLStr;}
00050     default: Fail; return TStr();
00051   }
00052 }
00053 
00054 void TNotify::DfOnNotify(const TNotifyType& Type, const TStr& MsgStr){
00055   switch (Type){
00056     case ntInfo: InfoNotify(MsgStr); break;
00057     case ntWarn: WarnNotify(MsgStr); break;
00058     case ntErr: ErrNotify(MsgStr); break;
00059     case ntStat: StatNotify(MsgStr); break;
00060     default: Fail;
00061   }
00062 }
00063 
00064 const PNotify TNotify::NullNotify=TNullNotify::New();
00065 const PNotify TNotify::StdNotify=TStdNotify::New();
00066 const PNotify TNotify::StdErrNotify=TStdErrNotify::New();
00067 
00069 // Standard-Notifier
00070 void TStdNotify::OnNotify(const TNotifyType& Type, const TStr& MsgStr){
00071   if (Type==ntInfo){
00072     printf("%s\n", MsgStr.CStr());
00073   } else {
00074     TStr TypeStr=TNotify::GetTypeStr(Type, false);
00075     printf("%s: %s\n", TypeStr.CStr(), MsgStr.CStr());
00076   }
00077 }
00078 
00079 void TStdNotify::OnStatus(const TStr& MsgStr){
00080   printf("%s", MsgStr.CStr());
00081   // print '\n' if message not overlayed
00082   if ((!MsgStr.Empty())&&(MsgStr.LastCh()!='\r')){
00083     printf("\n");}
00084 }
00085 
00087 // Standard-Error-Notifier
00088 void TStdErrNotify::OnNotify(const TNotifyType& Type, const TStr& MsgStr){
00089   if (Type==ntInfo){
00090     fprintf(stderr, "%s\n", MsgStr.CStr());
00091   } else {
00092     TStr TypeStr=TNotify::GetTypeStr(Type, false);
00093     fprintf(stderr, "%s: %s\n", TypeStr.CStr(), MsgStr.CStr());
00094   }
00095 }
00096 
00097 void TStdErrNotify::OnStatus(const TStr& MsgStr){
00098   fprintf(stderr, "%s", MsgStr.CStr());
00099   // print '\n' if message not overlayed
00100   if ((!MsgStr.Empty())&&(MsgStr.LastCh()!='\r')){
00101      fprintf(stderr, "\n");}
00102 }
00103 
00105 // Log-Notify
00106 void TLogNotify::OnStatus(const TStr& MsgStr) {
00107         TTm NowTm = TTm::GetCurLocTm();
00108         Notify->OnStatus(TStr::Fmt("[%s %s] %s", 
00109                 NowTm.GetYMDDashStr().CStr(), 
00110                 NowTm.GetHMSTColonDotStr(true, false).CStr(), 
00111                 MsgStr.CStr()));
00112 }
00113 
00115 // Exception
00116 TExcept::TOnExceptF TExcept::OnExceptF=NULL;