]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/Utils/common/eCosTrace.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / Utils / common / eCosTrace.cpp
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //#####DESCRIPTIONBEGIN####
26 //
27 // Author(s):     sdf
28 // Contributors:  sdf
29 // Date:          1999-04-01
30 // Description:   Standard include file for trace functions
31 // Usage:
32 //
33 //####DESCRIPTIONEND####
34
35 #include "eCosThreadUtils.h"
36 #include "eCosTrace.h"
37
38 CeCosTrace::TraceLevel CeCosTrace::nVerbosity=CeCosTrace::TRACE_LEVEL_ERRORS;
39 bool CeCosTrace::bInteractive=false;
40 LPCTSTR CeCosTrace::arpszDow[7]={_T("Su"),_T("M"),_T("Tu"),_T("W"),_T("Th"),_T("F"),_T("Sa")};
41
42 LogFunc *CeCosTrace::pfnOut=StreamLogFunc;
43 void *CeCosTrace::pOutParam=(void *)stdout;
44 LogFunc *CeCosTrace::pfnError=StreamLogFunc;
45 void *CeCosTrace::pErrorParam=(void *)stderr;
46
47 CeCosTrace::StreamInfo CeCosTrace::OutInfo(_T(""),stdout);
48 CeCosTrace::StreamInfo CeCosTrace::ErrInfo(_T(""),stderr);
49
50 void CALLBACK CeCosTrace::StreamLogFunc(void *pParam, LPCTSTR psz) 
51 {
52   // Interactive mode
53   ENTERCRITICAL;
54   _fputts(psz,(FILE *)pParam);
55   fflush((FILE *)pParam);
56   LEAVECRITICAL;
57 }
58
59 bool CeCosTrace::SetOutput(LPCTSTR pszFilename)
60 {
61   FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
62   if(f){
63     if(!OutInfo.strFilename.empty()){
64       fclose(OutInfo.f);
65     }
66     if(nVerbosity>=TRACE_LEVEL_TRACE){
67       _ftprintf(stderr,_T("Output -> %s (%08x)\n"),pszFilename,(unsigned int)f);
68     }
69     OutInfo.f=f;
70     OutInfo.strFilename=pszFilename;
71     SetOutput(StreamInfoFunc,&OutInfo);
72   }
73   return (NULL!=f);
74 }
75
76 bool CeCosTrace::SetError(LPCTSTR pszFilename)
77 {
78   FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
79   if(f){
80     if(!ErrInfo.strFilename.empty()){
81       fclose(ErrInfo.f);
82     }
83     ErrInfo.f=f;
84     ErrInfo.strFilename=pszFilename;
85     SetError(StreamInfoFunc,&ErrInfo);
86   }
87   return (NULL!=f);
88 }
89
90 void CALLBACK CeCosTrace::StreamInfoFunc(void *pParam, LPCTSTR psz) 
91 {
92   StreamInfo *pInfo=(StreamInfo *)pParam;
93   ENTERCRITICAL;
94   _fputts(psz,pInfo->f);
95   if(!pInfo->strFilename.empty() && Now()-pInfo->tLastReopen>20*1000){
96     // SAMBA clients will not honor fflush(), so we do this:
97     fclose(pInfo->f);
98     do {
99       pInfo->f=_tfopen(pInfo->strFilename,_T("a") MODE_TEXT);
100       if(NULL==pInfo->f){
101         _ftprintf(stderr,_T("Failed to reopen %s\n"),(LPCTSTR)pInfo->strFilename);
102         CeCosThreadUtils::Sleep(1000);
103       }
104     } while (NULL==pInfo->f);
105     pInfo->tLastReopen=Now();
106   } else {
107     fflush(pInfo->f);
108   }
109   LEAVECRITICAL;
110 }
111
112 void CeCosTrace::TimeStampedErr(LPCTSTR pszFormat,...)
113 {
114   va_list marker;
115   va_start (marker, pszFormat);
116         String str;
117   str.vFormat(pszFormat,marker);
118   va_end (marker);
119
120   Err(String::SFormat(_T("%s %s"),(LPCTSTR)Timestamp(),(LPCTSTR)str));
121 }
122
123 const String CeCosTrace::Timestamp()
124 {
125   time_t ltime;
126   time(&ltime);
127   struct tm *now=localtime( &ltime );
128   
129   bool bInCriticalSection=CeCosThreadUtils::CS::InCriticalSection();
130   TCHAR c1=bInCriticalSection?_TCHAR('<'):_TCHAR('[');
131   TCHAR c2=bInCriticalSection?_TCHAR('>'):_TCHAR(']');
132   return String::SFormat(_T("%c%3x %s %02d:%02d:%02d%c"),c1,CeCosThreadUtils::GetThreadId(),
133     arpszDow[now->tm_wday],now->tm_hour,now->tm_min,now->tm_sec,c2);
134
135 }