Subversion Repositories libraries.mycontrols

Rev

Rev 7 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
6 mjordaan 1
 
2
#pragma warning( disable : 4996 )
3
 
4
#include "stdafx.h"
5
#include "DbgView.h"
6
 
7
#include "nteventlogsource.h"
8
 
9
//int DbgView::m_iLog2File;                 //enable debug logging to file
10
//int DbgView::m_iLog2EventViewer;          //enable logging to event viewer
11
//int DbgView::m_iLog2Screen;               //enable logging to cmd window
12
//int DbgView::m_iLog2DebugViewer;          //enable logging to standard debugger
13
//int DbgView::m_iDebugLevel;               //debug level 1 .. 4
14
//int DbgView::m_iFirstCodeTrace;
15
//CString DbgView::m_sLogFile;              //log file name, including directory
16
CNTEventLogSource *DbgView::EventLog;
17
 
18
 
19
DbgView::DbgView( void)
20
{
21
 
22
  m_iDebugLevel = 10;
23
  m_iLog2Screen = 0;
24
  m_iLog2DebugViewer = 1;
25
  EventLog = NULL;
26
 
27
  m_iCopy = 1;
28
}
29
 
30
DbgView::DbgView( char *AppName)
31
{
32
 
33
  m_iDebugLevel = 10;
34
  m_iLog2Screen = 0;
35
  m_iLog2DebugViewer = 1;
36
  EventLog = NULL;
37
  //EventLog = new CNTEventLogSource( AppName, AppName);
38
  CString Cstr = AppName;
39
  Cstr.Append( ".txt");
40
  char temp[ _MAX_PATH];
41
  //strcat( strcat( strcpy( m_sLogFile.GetBuffer(), _getcwd( temp, _MAX_PATH )), "\\"), Cstr.GetBuffer());
42
  m_sLogFile.Empty();
43
  m_sLogFile.Append( _getcwd( temp, _MAX_PATH ));
44
  m_sLogFile.Append( "\\");
45
  m_sLogFile += Cstr;
46
  m_iCopy = 1;
47
}
48
 
49
DbgView::~DbgView( void)
50
{
51
 
52
  //reduce the count of copies, the last one will free our resources
53
  if (m_iCopy)
54
    if(--m_iCopy) return;
55
}
56
 
57
 
58
// Copy constructor
59
DbgView::DbgView( const DbgView &obj)
60
{
61
 
62
  //copy the values
63
  m_sCommandLine = obj.m_sCommandLine;
64
  m_sIniFile = obj.m_sCommandLine;
65
  m_sIniFileApp = obj.m_sIniFileApp;
66
 
67
  m_iCopy++;
68
}
69
 
70
/////////////////////////////////////////////////////////////////////////////
71
// Descripton: The Debug() member function writes a message to the screen
72
// and/or the log file. The requested debug-level, the 'level' parameter is
73
// checked against the level retrieved from the ini-file during start of the
74
// task
75
// Internally the fl_xlate function is used to convert from a key-string to a
76
// language dependent message. The function accepts variable number of
77
// parameters. The format of these parameters must be consistent with the
78
// format in the language tranlsation file.
79
//
80
// Parameters: codetracetext - text to log, casn include format specifiers.
81
// level - required debug level neede to log this message.
82
// event - log indication for eventviewer: EVENTLOG_SUCCESS, EVENTLOG_INFORMATION_TYPE
83
// or EVENTLOG_WARNING_TYPE.
84
// type - type of message: TRACE_TEXT, TRACE_START or TRACE_STOP. Message will
85
// indicate  normal text (if given binairy data is included), or starting or
86
// stopping.
87
// bin - binaire data, e.g. rx or tx data of a driver.
88
// binlen - legth of binairy buffer.
89
//
90
// Returns:
91
//
92
// Comment:
93
//
94
/////////////////////////////////////////////////////////////////////////////
95
void DbgView::CodeTrace( char *codetracetext, int level, int event, int type,
96
                        char *bin, int binlen, ...)
97
{
98
 
99
  va_list arg_ptr; //pointer to argument list
100
  char    m[ MAX_MSG];
101
  char    dbg_date[15],
102
          dbg_time[15];
103
 
104
 
105
  //for debugging the level should be greater or equal to one
106
  if (m_iDebugLevel < level) return;
107
 
108
  //set the argument pointer to the last argument
109
  va_start( arg_ptr, binlen);
110
 
111
  if (codetracetext)
112
    vsprintf( m, codetracetext, arg_ptr); //print the string in message
113
  else
114
    m[ 0] = '\0';
115
 
116
  if (bin && binlen) 
117
    bin[ binlen] = '\0';
118
 
119
  //print "Date + Time + Mess."
120
  if (m_iLog2Screen)
121
  {
122
 
123
    printf( "%s %s %s\n", _strdate( dbg_date), _strtime( dbg_time), m);
124
    if (bin && binlen) printf( "                  %s\n", bin);
125
  }
126
 
127
  //windows debugger
128
  if (m_iLog2DebugViewer)
129
  {
130
 
131
    strcat( m, "\n");
132
    OutputDebugString( (LPCTSTR)m);
133
    if (bin && binlen) 
134
    {
135
 
136
      bin[ binlen++] = '\n';
137
      bin[ binlen] = '\0';
138
 
139
      OutputDebugString( (LPCTSTR)bin);
140
    }
141
  }
142
 
143
  //only report state for log facility
144
  if (m_iLog2File && (m_sLogFile.GetLength() >0))
145
  {
146
 
147
    FILE *file;
148
 
149
    if (m_iFirstCodeTrace)
150
    {
151
      file = fopen( m_sLogFile.GetBuffer(), "a+"); //m_sLogFile.GetBuffer()
152
 
153
      if (!file->_ptr)
154
      {
155
        int win_error = GetLastError();
156
        char MsgBuf[4096];
157
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
158
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
159
 
160
        //remove line feed
161
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
162
      }
163
 
164
    }
165
    else
166
    {
167
 
168
      int win_error = GetLastError();
169
      file = fopen( m_sLogFile.GetBuffer(), "w+");
170
 
171
      if (!file->_ptr)
172
      {
173
        int win_error = GetLastError();
174
        char MsgBuf[4096];
175
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
176
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
177
 
178
        //remove line feed
179
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
180
      }
181
 
182
      m_iFirstCodeTrace++;
183
    }
184
 
185
    //write "Date + Time + Mess."
186
    //if (file)
187
    {
188
 
189
      fprintf( file, "%s %s %s", _strdate( dbg_date), _strtime( dbg_time), m);
190
 
191
      //write binairy data
192
      if (bin && binlen)
193
      {
194
 
195
        fprintf( file, "                  ");
196
 
197
        if (0)
198
          for( int i = 1; i < binlen+1; i++)
199
          {
200
            char buf[20];
201
            sprintf( buf, "%2.2X ", (int)bin[ i]);
202
 
203
            fprintf( file, buf + strlen( buf) - 3, (unsigned int)bin[ i]);
204
            if (!(i % 20)) fprintf( file, "\r\n                  ");
205
          }
206
        else fprintf( file, bin);
207
 
208
        //end with new line
209
        //fprintf( file, "\r\n");
210
      }
211
 
212
      fclose( file);
213
    }
214
  }
215
 
216
  //log message with event viewer
217
  if (m_iLog2EventViewer && EventLog)
218
  {
219
 
220
    //several types of messages are supported
221
    switch (type)
222
    {
223
 
224
      case TRACE_START: //application start
225
        EventLog->Report( event, CNT_MSG_STARTED, m);
226
        break;
227
      case TRACE_STOP: //application stop
228
        EventLog->Report( event, CNT_MSG_STOPPED, m);
229
        break;
230
      case TRACE_TEXT: //normal text message, cna be with binairy data
231
      default:
232
 
233
        if (bin && binlen)
234
          EventLog->Report( event, CNT_MSG_FULLSTRING, m, bin, binlen);
235
        else
236
          EventLog->Report( (WORD)event, (DWORD)CNT_MSG_FULLSTRING, (LPCTSTR)m);
237
        break;
238
    }
239
  }
240
 
241
  //set the argument pointer to NULL
242
  va_end( arg_ptr);
243
}
244
 
245
 
246
int DbgView::Log2EventViewer(int Type, char *msg)
247
{
248
  if ( EventLog) 
249
    return EventLog->Report( Type, CNT_MSG_FULLSTRING, msg);
250
 
251
  return (-1);
252
}