1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //#####DESCRIPTIONBEGIN####
30 // Description: Standard include file for trace functions
33 //####DESCRIPTIONEND####
35 #include "eCosThreadUtils.h"
36 #include "eCosTrace.h"
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")};
42 LogFunc *CeCosTrace::pfnOut=StreamLogFunc;
43 void *CeCosTrace::pOutParam=(void *)stdout;
44 LogFunc *CeCosTrace::pfnError=StreamLogFunc;
45 void *CeCosTrace::pErrorParam=(void *)stderr;
47 CeCosTrace::StreamInfo CeCosTrace::OutInfo(_T(""),stdout);
48 CeCosTrace::StreamInfo CeCosTrace::ErrInfo(_T(""),stderr);
50 void CALLBACK CeCosTrace::StreamLogFunc(void *pParam, LPCTSTR psz)
54 _fputts(psz,(FILE *)pParam);
55 fflush((FILE *)pParam);
59 bool CeCosTrace::SetOutput(LPCTSTR pszFilename)
61 FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
63 if(!OutInfo.strFilename.empty()){
66 if(nVerbosity>=TRACE_LEVEL_TRACE){
67 _ftprintf(stderr,_T("Output -> %s (%08x)\n"),pszFilename,(unsigned int)f);
70 OutInfo.strFilename=pszFilename;
71 SetOutput(StreamInfoFunc,&OutInfo);
76 bool CeCosTrace::SetError(LPCTSTR pszFilename)
78 FILE *f=_tfopen(pszFilename,_T("a") MODE_TEXT);
80 if(!ErrInfo.strFilename.empty()){
84 ErrInfo.strFilename=pszFilename;
85 SetError(StreamInfoFunc,&ErrInfo);
90 void CALLBACK CeCosTrace::StreamInfoFunc(void *pParam, LPCTSTR psz)
92 StreamInfo *pInfo=(StreamInfo *)pParam;
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:
99 pInfo->f=_tfopen(pInfo->strFilename,_T("a") MODE_TEXT);
101 _ftprintf(stderr,_T("Failed to reopen %s\n"),(LPCTSTR)pInfo->strFilename);
102 CeCosThreadUtils::Sleep(1000);
104 } while (NULL==pInfo->f);
105 pInfo->tLastReopen=Now();
112 void CeCosTrace::TimeStampedErr(LPCTSTR pszFormat,...)
115 va_start (marker, pszFormat);
117 str.vFormat(pszFormat,marker);
120 Err(String::SFormat(_T("%s %s"),(LPCTSTR)Timestamp(),(LPCTSTR)str));
123 const String CeCosTrace::Timestamp()
127 struct tm *now=localtime( <ime );
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);