Subversion Repositories libraries.mycontrols

Rev

Rev 6 | Go to most recent revision | Details | Compare with Previous | 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
 
7 mjordaan 70
 
71
void DbgView::SetEventLogging( char *AppName)
72
{
73
 
74
  EventLog = new CNTEventLogSource( NULL, AppName);
75
  EventLog->Install( AppName, "D:\\Projects\\alcan\\datataker\\source\\vs2012\\datataker\\datataker.dll", EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE);
76
  CString Cstr = AppName;
77
  Cstr.Append( ".txt");
78
  char temp[ _MAX_PATH];
79
  //strcat( strcat( strcpy( m_sLogFile.GetBuffer(), _getcwd( temp, _MAX_PATH )), "\\"), Cstr.GetBuffer());
80
  m_sLogFile.Empty();
81
  m_sLogFile.Append( _getcwd( temp, _MAX_PATH ));
82
  m_sLogFile.Append( "\\");
83
  m_sLogFile += Cstr;
84
}
85
 
6 mjordaan 86
/////////////////////////////////////////////////////////////////////////////
87
// Descripton: The Debug() member function writes a message to the screen
88
// and/or the log file. The requested debug-level, the 'level' parameter is
89
// checked against the level retrieved from the ini-file during start of the
90
// task
91
// Internally the fl_xlate function is used to convert from a key-string to a
92
// language dependent message. The function accepts variable number of
93
// parameters. The format of these parameters must be consistent with the
94
// format in the language tranlsation file.
95
//
96
// Parameters: codetracetext - text to log, casn include format specifiers.
97
// level - required debug level neede to log this message.
98
// event - log indication for eventviewer: EVENTLOG_SUCCESS, EVENTLOG_INFORMATION_TYPE
99
// or EVENTLOG_WARNING_TYPE.
100
// type - type of message: TRACE_TEXT, TRACE_START or TRACE_STOP. Message will
101
// indicate  normal text (if given binairy data is included), or starting or
102
// stopping.
103
// bin - binaire data, e.g. rx or tx data of a driver.
104
// binlen - legth of binairy buffer.
105
//
106
// Returns:
107
//
108
// Comment:
109
//
110
/////////////////////////////////////////////////////////////////////////////
111
void DbgView::CodeTrace( char *codetracetext, int level, int event, int type,
112
                        char *bin, int binlen, ...)
113
{
114
 
115
  va_list arg_ptr; //pointer to argument list
116
  char    m[ MAX_MSG];
117
  char    dbg_date[15],
118
          dbg_time[15];
119
 
120
 
121
  //for debugging the level should be greater or equal to one
122
  if (m_iDebugLevel < level) return;
123
 
124
  //set the argument pointer to the last argument
125
  va_start( arg_ptr, binlen);
126
 
127
  if (codetracetext)
128
    vsprintf( m, codetracetext, arg_ptr); //print the string in message
129
  else
130
    m[ 0] = '\0';
131
 
132
  if (bin && binlen) 
133
    bin[ binlen] = '\0';
134
 
135
  //print "Date + Time + Mess."
136
  if (m_iLog2Screen)
137
  {
138
 
139
    printf( "%s %s %s\n", _strdate( dbg_date), _strtime( dbg_time), m);
140
    if (bin && binlen) printf( "                  %s\n", bin);
141
  }
142
 
143
  //windows debugger
144
  if (m_iLog2DebugViewer)
145
  {
146
 
147
    strcat( m, "\n");
148
    OutputDebugString( (LPCTSTR)m);
149
    if (bin && binlen) 
150
    {
151
 
152
      bin[ binlen++] = '\n';
153
      bin[ binlen] = '\0';
154
 
155
      OutputDebugString( (LPCTSTR)bin);
156
    }
157
  }
158
 
159
  //only report state for log facility
160
  if (m_iLog2File && (m_sLogFile.GetLength() >0))
161
  {
162
 
163
    FILE *file;
164
 
165
    if (m_iFirstCodeTrace)
166
    {
167
      file = fopen( m_sLogFile.GetBuffer(), "a+"); //m_sLogFile.GetBuffer()
168
 
169
      if (!file->_ptr)
170
      {
171
        int win_error = GetLastError();
172
        char MsgBuf[4096];
173
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
174
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
175
 
176
        //remove line feed
177
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
178
      }
179
 
180
    }
181
    else
182
    {
183
 
184
      int win_error = GetLastError();
185
      file = fopen( m_sLogFile.GetBuffer(), "w+");
186
 
187
      if (!file->_ptr)
188
      {
189
        int win_error = GetLastError();
190
        char MsgBuf[4096];
191
        DWORD bufLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, win_error, 
192
                                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &MsgBuf, sizeof(MsgBuf), NULL );
193
 
194
        //remove line feed
195
        MsgBuf[ --bufLen] = '\0';MsgBuf[ --bufLen] = '\0';
196
      }
197
 
198
      m_iFirstCodeTrace++;
199
    }
200
 
201
    //write "Date + Time + Mess."
202
    //if (file)
203
    {
204
 
205
      fprintf( file, "%s %s %s", _strdate( dbg_date), _strtime( dbg_time), m);
206
 
207
      //write binairy data
208
      if (bin && binlen)
209
      {
210
 
211
        fprintf( file, "                  ");
212
 
213
        if (0)
214
          for( int i = 1; i < binlen+1; i++)
215
          {
216
            char buf[20];
217
            sprintf( buf, "%2.2X ", (int)bin[ i]);
218
 
219
            fprintf( file, buf + strlen( buf) - 3, (unsigned int)bin[ i]);
220
            if (!(i % 20)) fprintf( file, "\r\n                  ");
221
          }
222
        else fprintf( file, bin);
223
 
224
        //end with new line
225
        //fprintf( file, "\r\n");
226
      }
227
 
228
      fclose( file);
229
    }
230
  }
231
 
232
  //log message with event viewer
233
  if (m_iLog2EventViewer && EventLog)
234
  {
235
 
236
    //several types of messages are supported
237
    switch (type)
238
    {
239
 
240
      case TRACE_START: //application start
241
        EventLog->Report( event, CNT_MSG_STARTED, m);
242
        break;
243
      case TRACE_STOP: //application stop
244
        EventLog->Report( event, CNT_MSG_STOPPED, m);
245
        break;
246
      case TRACE_TEXT: //normal text message, cna be with binairy data
247
      default:
248
 
249
        if (bin && binlen)
250
          EventLog->Report( event, CNT_MSG_FULLSTRING, m, bin, binlen);
251
        else
252
          EventLog->Report( (WORD)event, (DWORD)CNT_MSG_FULLSTRING, (LPCTSTR)m);
253
        break;
254
    }
255
  }
256
 
257
  //set the argument pointer to NULL
258
  va_end( arg_ptr);
259
}
260
 
261
 
262
int DbgView::Log2EventViewer(int Type, char *msg)
263
{
264
  if ( EventLog) 
265
    return EventLog->Report( Type, CNT_MSG_FULLSTRING, msg);
266
 
267
  return (-1);
268
}