comparison host/tools/Utils/common/eCosTrace.cpp @ 82:6736c52df507 ecos-sw-2000-04-14

Merge from eCos master repository on 2000-04-14-13:35:46-BST
author jlarmour
date Tue, 18 Apr 2000 21:51:55 +0000
parents 435cced73e2f
children 74dbf4c3f2e1
comparison
equal deleted inserted replaced
81:89fef2181d7d 82:6736c52df507
33 //####DESCRIPTIONEND#### 33 //####DESCRIPTIONEND####
34 34
35 #include "eCosThreadUtils.h" 35 #include "eCosThreadUtils.h"
36 #include "eCosTrace.h" 36 #include "eCosTrace.h"
37 37
38 bool CeCosTrace::bVerbose=false; 38 CeCosTrace::TraceLevel CeCosTrace::nVerbosity=CeCosTrace::TRACE_LEVEL_ERRORS;
39 bool CeCosTrace::bInteractive=false; 39 bool CeCosTrace::bInteractive=false;
40 LPCTSTR CeCosTrace::arpszDow[7]={_T("Su"),_T("M"),_T("Tu"),_T("W"),_T("Th"),_T("F"),_T("Sa")}; 40 LPCTSTR CeCosTrace::arpszDow[7]={_T("Su"),_T("M"),_T("Tu"),_T("W"),_T("Th"),_T("F"),_T("Sa")};
41 41
42 LogFunc *CeCosTrace::pfnOut=StreamLogFunc; 42 LogFunc *CeCosTrace::pfnOut=StreamLogFunc;
43 void *CeCosTrace::pOutParam=(void *)stdout; 43 void *CeCosTrace::pOutParam=(void *)stdout;
56 LEAVECRITICAL; 56 LEAVECRITICAL;
57 } 57 }
58 58
59 bool CeCosTrace::SetOutput(LPCTSTR pszFilename) 59 bool CeCosTrace::SetOutput(LPCTSTR pszFilename)
60 { 60 {
61 FILE *f=_tfopen(pszFilename,_T("at")); 61 FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
62 if(f){ 62 if(f){
63 if(OutInfo.strFilename.GetLength()>0){ 63 if(!OutInfo.strFilename.empty()){
64 fclose(OutInfo.f); 64 fclose(OutInfo.f);
65 } 65 }
66 if(bVerbose){ 66 if(nVerbosity>=TRACE_LEVEL_TRACE){
67 _ftprintf(stderr,_T("Output -> %s (%08x)\n"),pszFilename,(unsigned int)f); 67 _ftprintf(stderr,_T("Output -> %s (%08x)\n"),pszFilename,(unsigned int)f);
68 } 68 }
69 OutInfo.f=f; 69 OutInfo.f=f;
70 OutInfo.strFilename=pszFilename; 70 OutInfo.strFilename=pszFilename;
71 SetOutput(StreamInfoFunc,&OutInfo); 71 SetOutput(StreamInfoFunc,&OutInfo);
73 return (NULL!=f); 73 return (NULL!=f);
74 } 74 }
75 75
76 bool CeCosTrace::SetError(LPCTSTR pszFilename) 76 bool CeCosTrace::SetError(LPCTSTR pszFilename)
77 { 77 {
78 FILE *f=_tfopen(pszFilename,_T("at")); 78 FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
79 if(f){ 79 if(f){
80 if(ErrInfo.strFilename.GetLength()>0){ 80 if(!ErrInfo.strFilename.empty()){
81 fclose(ErrInfo.f); 81 fclose(ErrInfo.f);
82 } 82 }
83 ErrInfo.f=f; 83 ErrInfo.f=f;
84 ErrInfo.strFilename=pszFilename; 84 ErrInfo.strFilename=pszFilename;
85 SetError(StreamInfoFunc,&ErrInfo); 85 SetError(StreamInfoFunc,&ErrInfo);
90 void CALLBACK CeCosTrace::StreamInfoFunc(void *pParam, LPCTSTR psz) 90 void CALLBACK CeCosTrace::StreamInfoFunc(void *pParam, LPCTSTR psz)
91 { 91 {
92 StreamInfo *pInfo=(StreamInfo *)pParam; 92 StreamInfo *pInfo=(StreamInfo *)pParam;
93 ENTERCRITICAL; 93 ENTERCRITICAL;
94 _fputts(psz,pInfo->f); 94 _fputts(psz,pInfo->f);
95 if(pInfo->strFilename.GetLength()>0 && Now()-pInfo->tLastReopen>20*1000){ 95 if(!pInfo->strFilename.empty() && Now()-pInfo->tLastReopen>20*1000){
96 // SAMBA clients will not honor fflush(), so we do this: 96 // SAMBA clients will not honor fflush(), so we do this:
97 fclose(pInfo->f); 97 fclose(pInfo->f);
98 do { 98 do {
99 pInfo->f=_tfopen(pInfo->strFilename,_T("at")); 99 pInfo->f=_tfopen(pInfo->strFilename,_T("a") MODE_TEXT);
100 if(NULL==pInfo->f){ 100 if(NULL==pInfo->f){
101 _ftprintf(stderr,_T("Failed to reopen %s\n"),(LPCTSTR)pInfo->strFilename); 101 _ftprintf(stderr,_T("Failed to reopen %s\n"),(LPCTSTR)pInfo->strFilename);
102 Sleep(1000); 102 CeCosThreadUtils::Sleep(1000);
103 } 103 }
104 } while (NULL==pInfo->f); 104 } while (NULL==pInfo->f);
105 pInfo->tLastReopen=Now(); 105 pInfo->tLastReopen=Now();
106 } else { 106 } else {
107 fflush(pInfo->f); 107 fflush(pInfo->f);
108 } 108 }
109 LEAVECRITICAL; 109 LEAVECRITICAL;
110 } 110 }
111 111
112 void CeCosTrace::Trace(LPCTSTR pszFormat, ...) 112 void CeCosTrace::TimeStampedErr(LPCTSTR pszFormat,...)
113 {
114 if(bVerbose){
115 va_list marker;
116 va_start (marker, pszFormat);
117 String str;
118 str.vFormat(pszFormat,marker);
119 va_end (marker);
120 Error(_T("%s"),(LPCTSTR)str);
121 }
122 }
123
124 void CeCosTrace::Error(LPCTSTR pszFormat, ...)
125 { 113 {
126 va_list marker; 114 va_list marker;
127 va_start (marker, pszFormat); 115 va_start (marker, pszFormat);
128 String str; 116 String str;
129 str.vFormat(pszFormat,marker); 117 str.vFormat(pszFormat,marker);
130 va_end (marker); 118 va_end (marker);
131 119
120 Err(String::SFormat(_T("%s %s"),(LPCTSTR)Timestamp(),(LPCTSTR)str));
121 }
122
123 const String CeCosTrace::Timestamp()
124 {
132 time_t ltime; 125 time_t ltime;
133 time(&ltime); 126 time(&ltime);
134 struct tm *now=localtime( &ltime ); 127 struct tm *now=localtime( &ltime );
135 128
136 String s; 129 bool bInCriticalSection=CeCosThreadUtils::CS::InCriticalSection();
137 CeCosThreadUtils::THREAD_ID id=CeCosThreadUtils::GetThreadId(); 130 TCHAR c1=bInCriticalSection?_TCHAR('<'):_TCHAR('[');
138 s.Format(_T("[%x %s %02d:%02d:%02d%s] %s"),id, 131 TCHAR c2=bInCriticalSection?_TCHAR('>'):_TCHAR(']');
139 arpszDow[now->tm_wday],now->tm_hour,now->tm_min,now->tm_sec,CeCosThreadUtils::CS::nCSOwner==id?_T("*"):_T(""),(LPCTSTR)str); 132 return String::SFormat(_T("%c%3x %s %02d:%02d:%02d%c"),c1,CeCosThreadUtils::GetThreadId(),
140 if(_TCHAR('\n')!=s[s.GetLength()-1]){ 133 arpszDow[now->tm_wday],now->tm_hour,now->tm_min,now->tm_sec,c2);
141 s+=_TCHAR('\n'); 134
142 }
143 Err(s);
144 } 135 }
145
146