Subversion Repositories libraries.mycontrols

Rev

Rev 6 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log


#pragma warning( disable : 4996 )

#include "stdafx.h"
#include "DbgView.h"

#include "nteventlogsource.h"

//int DbgView::m_iLog2File;                 //enable debug logging to file
//int DbgView::m_iLog2EventViewer;          //enable logging to event viewer
//int DbgView::m_iLog2Screen;               //enable logging to cmd window
//int DbgView::m_iLog2DebugViewer;          //enable logging to standard debugger
//int DbgView::m_iDebugLevel;               //debug level 1 .. 4
//int DbgView::m_iFirstCodeTrace;
//CString DbgView::m_sLogFile;              //log file name, including directory
CNTEventLogSource *DbgView::EventLog;


DbgView::DbgView( void)
{

  m_iDebugLevel = 10;
  m_iLog2Screen = 0;
  m_iLog2DebugViewer = 1;
  EventLog = NULL;

  m_iCopy = 1;
}

DbgView::DbgView( char *AppName)
{

  m_iDebugLevel = 10;
  m_iLog2Screen = 0;
  m_iLog2DebugViewer = 1;
  EventLog = NULL;
  //EventLog = new CNTEventLogSource( AppName, AppName);
  CString Cstr = AppName;
  Cstr.Append( ".txt");
  char temp[ _MAX_PATH];
  //strcat( strcat( strcpy( m_sLogFile.GetBuffer(), _getcwd( temp, _MAX_PATH )), "\\"), Cstr.GetBuffer());
  m_sLogFile.Empty();
  m_sLogFile.Append( _getcwd( temp, _MAX_PATH ));
  m_sLogFile.Append( "\\");
  m_sLogFile += Cstr;
  m_iCopy = 1;
}

DbgView::~DbgView( void)
{

  //reduce the count of copies, the last one will free our resources
  if (m_iCopy)
    if(--m_iCopy) return;
}


// Copy constructor
DbgView::DbgView( const DbgView &obj)
{

  //copy the values
  m_sCommandLine = obj.m_sCommandLine;
  m_sIniFile = obj.m_sCommandLine;
  m_sIniFileApp = obj.m_sIniFileApp;

  m_iCopy++;
}


void DbgView::SetEventLogging( char *AppName)
{

  EventLog = new CNTEventLogSource( NULL, AppName);
  EventLog->Install( AppName, "D:\\Projects\\alcan\\datataker\\source\\vs2012\\datataker\\datataker.dll", EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE);
  CString Cstr = AppName;
  Cstr.Append( ".txt");
  char temp[ _MAX_PATH];
  //strcat( strcat( strcpy( m_sLogFile.GetBuffer(), _getcwd( temp, _MAX_PATH )), "\\"), Cstr.GetBuffer());
  m_sLogFile.Empty();
  m_sLogFile.Append( _getcwd( temp, _MAX_PATH ));
  m_sLogFile.Append( "\\");
  m_sLogFile += Cstr;
}

/////////////////////////////////////////////////////////////////////////////
// Descripton: The Debug() member function writes a message to the screen
// and/or the log file. The requested debug-level, the 'level' parameter is
// checked against the level retrieved from the ini-file during start of the
// task
// Internally the fl_xlate function is used to convert from a key-string to a
// language dependent message. The function accepts variable number of
// parameters. The format of these parameters must be consistent with the
// format in the language tranlsation file.
//
// Parameters: codetracetext - text to log, casn include format specifiers.
// level - required debug level neede to log this message.
// event - log indication for eventviewer: EVENTLOG_SUCCESS, EVENTLOG_INFORMATION_TYPE
// or EVENTLOG_WARNING_TYPE.
// type - type of message: TRACE_TEXT, TRACE_START or TRACE_STOP. Message will
// indicate  normal text (if given binairy data is included), or starting or
// stopping.
// bin - binaire data, e.g. rx or tx data of a driver.
// binlen - legth of binairy buffer.
//
// Returns:
//
// Comment:
//
/////////////////////////////////////////////////////////////////////////////
void DbgView::CodeTrace( char *codetracetext, int level, int event, int type,
                        char *bin, int binlen, ...)
{

  va_list arg_ptr; //pointer to argument list
  char    m[ MAX_MSG];
  char    dbg_date[15],
          dbg_time[15];


  //for debugging the level should be greater or equal to one
  if (m_iDebugLevel < level) return;

  //set the argument pointer to the last argument
  va_start( arg_ptr, binlen);

  if (codetracetext)
    vsprintf( m, codetracetext, arg_ptr); //print the string in message
  else
    m[ 0] = '\0';

  if (bin && binlen) 
    bin[ binlen] = '\0';

  //print "Date + Time + Mess."
  if (m_iLog2Screen)
  {

    printf( "%s %s %s\n", _strdate( dbg_date), _strtime( dbg_time), m);
    if (bin && binlen) printf( "                  %s\n", bin);
  }

  //windows debugger
  if (m_iLog2DebugViewer)
  {

    strcat( m, "\n");
    OutputDebugString( (LPCTSTR)m);
    if (bin && binlen) 
    {

      bin[ binlen++] = '\n';
      bin[ binlen] = '\0';

      OutputDebugString( (LPCTSTR)bin);
    }
  }

  //only report state for log facility
  if (m_iLog2File && (m_sLogFile.GetLength() >0))
  {

    FILE *file;

    if (m_iFirstCodeTrace)
    {
      file = fopen( m_sLogFile.GetBuffer(), "a+"); //m_sLogFile.GetBuffer()

      if (!file->_ptr)
      {
        int win_error = GetLastError();
        char MsgBuf[4096];
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
      
        //remove line feed
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
      }

    }
    else
    {

      int win_error = GetLastError();
      file = fopen( m_sLogFile.GetBuffer(), "w+");

      if (!file->_ptr)
      {
        int win_error = GetLastError();
        char MsgBuf[4096];
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
      
        //remove line feed
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
      }

      m_iFirstCodeTrace++;
    }

    //write "Date + Time + Mess."
    //if (file)
    {

      fprintf( file, "%s %s %s", _strdate( dbg_date), _strtime( dbg_time), m);

      //write binairy data
      if (bin && binlen)
      {

        fprintf( file, "                  ");

        if (0)
          for( int i = 1; i < binlen+1; i++)
          {
            char buf[20];
            sprintf( buf, "%2.2X ", (int)bin[ i]);

            fprintf( file, buf + strlen( buf) - 3, (unsigned int)bin[ i]);
            if (!(i % 20)) fprintf( file, "\r\n                  ");
          }
        else fprintf( file, bin);

        //end with new line
        //fprintf( file, "\r\n");
      }

      fclose( file);
    }
  }

  //log message with event viewer
  if (m_iLog2EventViewer && EventLog)
  {
     
    //several types of messages are supported
    switch (type)
    {

      case TRACE_START: //application start
        EventLog->Report( event, CNT_MSG_STARTED, m);
        break;
      case TRACE_STOP: //application stop
        EventLog->Report( event, CNT_MSG_STOPPED, m);
        break;
      case TRACE_TEXT: //normal text message, cna be with binairy data
      default:
         
        if (bin && binlen)
          EventLog->Report( event, CNT_MSG_FULLSTRING, m, bin, binlen);
        else
          EventLog->Report( (WORD)event, (DWORD)CNT_MSG_FULLSTRING, (LPCTSTR)m);
        break;
    }
  }

  //set the argument pointer to NULL
  va_end( arg_ptr);
}


int DbgView::Log2EventViewer(int Type, char *msg)
{
  if ( EventLog) 
    return EventLog->Report( Type, CNT_MSG_FULLSTRING, msg);

  return (-1);
}