view host/tools/Utils/common/eCosTrace.cpp @ 76:435cced73e2f ecos-v1_3_1-release

eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
author jlarmour
date Tue, 28 Mar 2000 14:10:45 +0000
parents
children 6736c52df507
line wrap: on
line source

//####COPYRIGHTBEGIN####
//                                                                          
// ----------------------------------------------------------------------------
// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
//
// This program is part of the eCos host tools.
//
// This program is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the Free 
// Software Foundation; either version 2 of the License, or (at your option) 
// any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
// more details.
// 
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ----------------------------------------------------------------------------
//                                                                          
//####COPYRIGHTEND####
//#####DESCRIPTIONBEGIN####
//
// Author(s):     sdf
// Contributors:  sdf
// Date:          1999-04-01
// Description:   Standard include file for trace functions
// Usage:
//
//####DESCRIPTIONEND####

#include "eCosThreadUtils.h"
#include "eCosTrace.h"

bool CeCosTrace::bVerbose=false;
bool CeCosTrace::bInteractive=false;
LPCTSTR CeCosTrace::arpszDow[7]={_T("Su"),_T("M"),_T("Tu"),_T("W"),_T("Th"),_T("F"),_T("Sa")};

LogFunc *CeCosTrace::pfnOut=StreamLogFunc;
void *CeCosTrace::pOutParam=(void *)stdout;
LogFunc *CeCosTrace::pfnError=StreamLogFunc;
void *CeCosTrace::pErrorParam=(void *)stderr;

CeCosTrace::StreamInfo CeCosTrace::OutInfo(_T(""),stdout);
CeCosTrace::StreamInfo CeCosTrace::ErrInfo(_T(""),stderr);

void CALLBACK CeCosTrace::StreamLogFunc(void *pParam, LPCTSTR psz) 
{
  // Interactive mode
  ENTERCRITICAL;
  _fputts(psz,(FILE *)pParam);
  fflush((FILE *)pParam);
  LEAVECRITICAL;
}

bool CeCosTrace::SetOutput(LPCTSTR pszFilename)
{
  FILE *f=_tfopen(pszFilename,_T("at"));
  if(f){
    if(OutInfo.strFilename.GetLength()>0){
      fclose(OutInfo.f);
    }
    if(bVerbose){
      _ftprintf(stderr,_T("Output -> %s (%08x)\n"),pszFilename,(unsigned int)f);
    }
    OutInfo.f=f;
    OutInfo.strFilename=pszFilename;
    SetOutput(StreamInfoFunc,&OutInfo);
  }
  return (NULL!=f);
}

bool CeCosTrace::SetError(LPCTSTR pszFilename)
{
  FILE *f=_tfopen(pszFilename,_T("at"));
  if(f){
    if(ErrInfo.strFilename.GetLength()>0){
      fclose(ErrInfo.f);
    }
    ErrInfo.f=f;
    ErrInfo.strFilename=pszFilename;
    SetError(StreamInfoFunc,&ErrInfo);
  }
  return (NULL!=f);
}

void CALLBACK CeCosTrace::StreamInfoFunc(void *pParam, LPCTSTR psz) 
{
  StreamInfo *pInfo=(StreamInfo *)pParam;
  ENTERCRITICAL;
  _fputts(psz,pInfo->f);
  if(pInfo->strFilename.GetLength()>0 && Now()-pInfo->tLastReopen>20*1000){
    // SAMBA clients will not honor fflush(), so we do this:
    fclose(pInfo->f);
    do {
      pInfo->f=_tfopen(pInfo->strFilename,_T("at"));
      if(NULL==pInfo->f){
        _ftprintf(stderr,_T("Failed to reopen %s\n"),(LPCTSTR)pInfo->strFilename);
        Sleep(1000);
      }
    } while (NULL==pInfo->f);
    pInfo->tLastReopen=Now();
  } else {
    fflush(pInfo->f);
  }
  LEAVECRITICAL;
}

void CeCosTrace::Trace(LPCTSTR pszFormat, ...)
{
  if(bVerbose){
    va_list marker;
    va_start (marker, pszFormat);
	  String str;
    str.vFormat(pszFormat,marker);
    va_end (marker);
    Error(_T("%s"),(LPCTSTR)str);
  }
}

void CeCosTrace::Error(LPCTSTR pszFormat, ...)
{
  va_list marker;
  va_start (marker, pszFormat);
	String str;
  str.vFormat(pszFormat,marker);
  va_end (marker);

  time_t ltime;
  time(&ltime);
  struct tm *now=localtime( &ltime );
  
  String s;
  CeCosThreadUtils::THREAD_ID id=CeCosThreadUtils::GetThreadId();
  s.Format(_T("[%x %s %02d:%02d:%02d%s] %s"),id,
    arpszDow[now->tm_wday],now->tm_hour,now->tm_min,now->tm_sec,CeCosThreadUtils::CS::nCSOwner==id?_T("*"):_T(""),(LPCTSTR)str);
  if(_TCHAR('\n')!=s[s.GetLength()-1]){
    s+=_TCHAR('\n');
  }
  Err(s);
}