SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
ut.cpp
Go to the documentation of this file.
1 // Notifications
3 void TNotify::OnNotifyFmt(const TNotifyType& Type, const char *FmtStr, ...) {
4  char Bf[10*1024];
5  va_list valist;
6  va_start(valist, FmtStr);
7  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
8  va_end(valist);
9  if (RetVal!=-1) { OnNotify(Type, TStr(Bf)); }
10 }
11 
12 void TNotify::OnStatusFmt(const char *FmtStr, ...) {
13  char Bf[10*1024];
14  va_list valist;
15  va_start(valist, FmtStr);
16  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
17  va_end(valist);
18  if (RetVal!=-1) { OnStatus(TStr(Bf)); }
19 }
20 
21 void TNotify::OnLnFmt(const char *FmtStr, ...) {
22  char Bf[10*1024];
23  va_list valist;
24  va_start(valist, FmtStr);
25  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
26  va_end(valist);
27  if (RetVal!=-1) { OnLn(TStr(Bf)); }
28 }
29 
30 void TNotify::OnTxtFmt(const char *FmtStr, ...) {
31  char Bf[10*1024];
32  va_list valist;
33  va_start(valist, FmtStr);
34  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
35  va_end(valist);
36  if (RetVal!=-1) { OnTxt(TStr(Bf)); }
37 }
38 
40  const TNotifyType& Type, const bool& Brief){
41  static TStr InfoSStr="I"; static TStr InfoLStr="Information";
42  static TStr WarnSStr="W"; static TStr WarnLStr="Warning";
43  static TStr ErrSStr="E"; static TStr ErrLStr="Error";
44  static TStr StatSStr=""; static TStr StatLStr="Status";
45  switch (Type){
46  case ntInfo: if (Brief){return InfoSStr;} else {return InfoLStr;}
47  case ntWarn: if (Brief){return WarnSStr;} else {return WarnLStr;}
48  case ntErr: if (Brief){return ErrSStr;} else {return ErrLStr;}
49  case ntStat: if (Brief){return StatSStr;} else {return StatLStr;}
50  default: Fail; return TStr();
51  }
52 }
53 
54 void TNotify::DfOnNotify(const TNotifyType& Type, const TStr& MsgStr){
55  switch (Type){
56  case ntInfo: InfoNotify(MsgStr); break;
57  case ntWarn: WarnNotify(MsgStr); break;
58  case ntErr: ErrNotify(MsgStr); break;
59  case ntStat: StatNotify(MsgStr); break;
60  default: Fail;
61  }
62 }
63 
67 
69 // Standard-Notifier
70 void TStdNotify::OnNotify(const TNotifyType& Type, const TStr& MsgStr){
71  if (Type==ntInfo){
72  printf("%s\n", MsgStr.CStr());
73  } else {
74  TStr TypeStr=TNotify::GetTypeStr(Type, false);
75  printf("%s: %s\n", TypeStr.CStr(), MsgStr.CStr());
76  }
77 }
78 
79 void TStdNotify::OnStatus(const TStr& MsgStr){
80  printf("%s", MsgStr.CStr());
81  // print '\n' if message not overlayed
82  if ((!MsgStr.Empty())&&(MsgStr.LastCh()!='\r')){
83  printf("\n");}
84 }
85 
87 // Standard-Error-Notifier
88 void TStdErrNotify::OnNotify(const TNotifyType& Type, const TStr& MsgStr){
89  if (Type==ntInfo){
90  fprintf(stderr, "%s\n", MsgStr.CStr());
91  } else {
92  TStr TypeStr=TNotify::GetTypeStr(Type, false);
93  fprintf(stderr, "%s: %s\n", TypeStr.CStr(), MsgStr.CStr());
94  }
95 }
96 
97 void TStdErrNotify::OnStatus(const TStr& MsgStr){
98  fprintf(stderr, "%s", MsgStr.CStr());
99  // print '\n' if message not overlayed
100  if ((!MsgStr.Empty())&&(MsgStr.LastCh()!='\r')){
101  fprintf(stderr, "\n");}
102 }
103 
105 // Log-Notify
106 void TLogNotify::OnStatus(const TStr& MsgStr) {
107  TTm NowTm = TTm::GetCurLocTm();
108  Notify->OnStatus(TStr::Fmt("[%s %s] %s",
109  NowTm.GetYMDDashStr().CStr(),
110  NowTm.GetHMSTColonDotStr(true, false).CStr(),
111  MsgStr.CStr()));
112 }
113 
115 // Exception
static TStr GetTypeStr(const TNotifyType &Type, const bool &Brief=true)
Definition: ut.cpp:39
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:79
static void DfOnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.cpp:54
TStr GetYMDDashStr() const
Definition: tm.cpp:975
#define Fail
Definition: bd.h:238
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.cpp:88
static const PNotify StdErrNotify
Definition: ut.h:64
Definition: ut.h:28
static PNotify New()
Definition: ut.h:129
void OnStatusFmt(const char *FmtStr,...)
Definition: ut.cpp:12
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:106
void WarnNotify(const char *NotifyCStr)
Definition: bd.h:72
Definition: ut.h:28
virtual void OnTxt(const TStr &)
Definition: ut.h:41
virtual void OnLn(const TStr &)
Definition: ut.h:40
char LastCh() const
Definition: dt.h:487
void OnStatus(const TStr &MsgStr)
Definition: ut.cpp:97
static PNotify New()
Definition: ut.h:140
static TTm GetCurLocTm()
Definition: tm.cpp:1060
virtual void OnNotify(const TNotifyType &, const TStr &)
Definition: ut.h:38
static PNotify New()
Definition: ut.h:72
static const PNotify StdNotify
Definition: ut.h:63
Definition: ut.h:28
Definition: tm.h:213
void OnLnFmt(const char *FmtStr,...)
Definition: ut.cpp:21
void InfoNotify(const char *NotifyCStr)
Definition: bd.h:70
static const PNotify NullNotify
Definition: ut.h:62
PNotify Notify
Definition: ut.h:150
Definition: dt.h:412
bool Empty() const
Definition: dt.h:491
enum TNotifyType_ TNotifyType
virtual void OnStatus(const TStr &)
Definition: ut.h:39
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: ut.h:28
TStr GetHMSTColonDotStr(const bool &FullP=false, const bool &MSecP=true) const
Definition: tm.cpp:983
void OnTxtFmt(const char *FmtStr,...)
Definition: ut.cpp:30
void StatNotify(const char *NotifyCStr)
Definition: bd.h:76
static TOnExceptF OnExceptF
Definition: ut.h:181
char * CStr()
Definition: dt.h:479
void OnNotifyFmt(const TNotifyType &Type, const char *FmtStr,...)
Definition: ut.cpp:3
void OnNotify(const TNotifyType &Type, const TStr &MsgStr)
Definition: ut.cpp:70
void(* TOnExceptF)(const TStr &MsgStr)
Definition: ut.h:180