Message.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "Message.h"
19 
20 int g_iDebugLevel = 3;
21 bool g_bDebugFile = false;
22 bool g_bDebug = true;
23 const char* g_pcDebugFileName = "debug.txt";
24 
26 
27 CRITICAL_SECTION* CMessage::m_csMessage = NULL;
28 
29 #define ENTERCS if(m_csMessage!=NULL) EnterCriticalSection(m_csMessage);
30 
31 #define LEAVECS if(m_csMessage!=NULL) LeaveCriticalSection(m_csMessage);
32 
33 // ========================================================================== ;
34 // ;
35 // ---- constructors / destructor ------------------------------------------- ;
36 // ;
37 // ========================================================================== ;
38 
39 CMessage::CMessage() : m_bDebug(g_bDebug), m_bDebugFile(g_bDebugFile), m_iDebugLevel(g_iDebugLevel)
40 {
41  m_acClassName[0] = 0;
42 }
43 
44 CMessage::CMessage(const char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile) : m_bDebug(bDebug), m_bDebugFile(bDebugFile), m_iDebugLevel(iDebuglevel)
45 {
46  strncpy(m_acClassName ,pcClassName, 50);
47 }
48 
49 CMessage::CMessage(const CMessage& clMessage) : m_bDebug(clMessage.m_bDebug), m_bDebugFile(clMessage.m_bDebugFile), m_iDebugLevel(clMessage.m_iDebugLevel)
50 {
51  strncpy(m_acClassName ,clMessage.m_acClassName, 50);
52 }
53 
55 {
56 }
57 
58 // ========================================================================== ;
59 // ;
60 // ---- operators ----------------------------------------------------------- ;
61 // ;
62 // ========================================================================== ;
63 
65 {
66  strncpy(m_acClassName ,clMessage.m_acClassName, 50);
67  m_bDebug = clMessage.m_bDebug;
68  m_bDebugFile = clMessage.m_bDebugFile;
69  m_iDebugLevel = clMessage.m_iDebugLevel;
70  return *this;
71 }
72 
73 // ========================================================================== ;
74 // ;
75 // ---- query functions ----------------------------------------------------- ;
76 // ;
77 // ========================================================================== ;
78 
80 {
81  return m_iDebugLevel;
82 }
83 
84 // ========================================================================== ;
85 // ;
86 // ---- modify functions ---------------------------------------------------- ;
87 // ;
88 // ========================================================================== ;
89 
90 int CMessage::initMessage(const char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile)
91 {
92  strncpy(m_acClassName, pcClassName, 50);
93  m_bDebug = bDebug;
94  m_bDebugFile = bDebugFile;
95  m_iDebugLevel = iDebuglevel;
96  return 0;
97 }
98 
100 {
101  #if defined(__QNX__)
102  timespec nowTimeVal;
103  clock_gettime(CLOCK_REALTIME,&nowTimeVal);
104  m_fInitTime = (nowTimeVal.tv_sec
105  +(double(nowTimeVal.tv_nsec)/1e+9));
106  #elif defined(_WIN32)
107  _timeb nowTimeVal;
108  _ftime(&nowTimeVal);
109  m_fInitTime = (nowTimeVal.time
110  +(double(nowTimeVal.millitm)/1e+3));
111  #else
112  timeval nowTimeVal;
113  gettimeofday(&nowTimeVal,0);
114  m_fInitTime = (nowTimeVal.tv_sec
115  +(double(nowTimeVal.tv_usec)/1e+6));
116  #endif
117 }
118 
119 void CMessage::setDebugLevel(int iLevel)
120 {
121  m_iDebugLevel = iLevel;
122 }
123 
124 void CMessage::setDebug(bool bFlag)
125 {
126  m_bDebug = bFlag;
127 }
128 
129 void CMessage::setDebugFile(bool bFlag)
130 {
131  m_bDebugFile = bFlag;
132 }
133 
134 void CMessage::setCriticalSection(CRITICAL_SECTION *csMessage)
135 {
136  m_csMessage = csMessage;
137 }
138 
139 // ========================================================================== ;
140 // ;
141 // ---- I/O functions ------------------------------------------------------- ;
142 // ;
143 // ========================================================================== ;
144 
145 // ========================================================================== ;
146 // ;
147 // ---- exec functions ------------------------------------------------------ ;
148 // ;
149 // ========================================================================== ;
150 
151 void CMessage::error(const char *pcErrorMessage,...) const
152 {
153 
154  ENTERCS;
155  va_list args;
156 
157  va_start(args, pcErrorMessage);
158 
159  #if defined(__QNX__)
160  timespec nowTimeVal;
161  clock_gettime(CLOCK_REALTIME,&nowTimeVal);
162  double fSeconds = (nowTimeVal.tv_sec
163  +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
164  #elif defined(_WIN32)
165  _timeb nowTimeVal;
166  _ftime(&nowTimeVal);
167  double fSeconds = (nowTimeVal.time
168  +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
169  #else
170  timeval nowTimeVal;
171  gettimeofday(&nowTimeVal,0);
172  double fSeconds = (nowTimeVal.tv_sec
173  +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
174  #endif
175 
176  static char acBuffer[255];
177  static char acOutBuffer[300];
178  vsprintf(acBuffer, pcErrorMessage, args);
179  sprintf(acOutBuffer, "\nERROR: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);
180  if (m_bDebugFile==true)
181  {
182 
183  FILE* hFile;
184  hFile=fopen(g_pcDebugFileName,"a+");
185  if(hFile != NULL)
186  {
187  fprintf(hFile, "%s", acOutBuffer);
188  fclose(hFile);
189  }
190  }
191 
192 #ifdef WIN32
193  OutputDebugString(acOutBuffer);
194 #else
195  fprintf(stderr, "%s",acOutBuffer);
196 #endif
197 
198  va_end(args);
199  LEAVECS;
200  exit(-1);
201 
202 };
203 
204 void CMessage::error(const int iErrorCode,
205  const char *pcErrorMessage,...)const
206 {
207 
208  ENTERCS;
209  va_list args;
210 
211  va_start(args, pcErrorMessage);
212 
213  #if defined(__QNX__)
214  timespec nowTimeVal;
215  clock_gettime(CLOCK_REALTIME,&nowTimeVal);
216  double fSeconds = (nowTimeVal.tv_sec
217  +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
218  #elif defined(_WIN32)
219  _timeb nowTimeVal;
220  _ftime(&nowTimeVal);
221  double fSeconds = (nowTimeVal.time
222  +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
223  #else
224  timeval nowTimeVal;
225  gettimeofday(&nowTimeVal,0);
226  double fSeconds = (nowTimeVal.tv_sec
227  +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
228  #endif
229 
230  static char acBuffer[255];
231  static char acOutBuffer[300];
232  vsprintf(acBuffer, pcErrorMessage, args);
233  sprintf(acOutBuffer, "\nERROR: #%i %5.3f %s::%s", iErrorCode, fSeconds, m_acClassName, acBuffer);
234  if (m_bDebugFile==true)
235  {
236 
237  FILE* hFile;
238  hFile=fopen(g_pcDebugFileName,"a+");
239  if(hFile != NULL)
240  {
241  fprintf(hFile, "%s", acOutBuffer);
242  fclose(hFile);
243  }
244 
245  }
246 
247 #ifdef WIN32
248  OutputDebugString(acOutBuffer);
249 #else
250  fprintf(stderr, "%s", acOutBuffer);
251 #endif
252  LEAVECS;
253  exit(-1);
254 
255 };
256 
257 void CMessage::warning(const char *pcWarningMessage,...) const
258 {
259  //UHR:use m_Debug as flag for screen output
260  //if(!m_bDebug)
261  // return;
262  ENTERCS;
263  va_list args;
264 
265  va_start(args, pcWarningMessage);
266 
267  #if defined(__QNX__)
268  timespec nowTimeVal;
269  clock_gettime(CLOCK_REALTIME,&nowTimeVal);
270  double fSeconds = (nowTimeVal.tv_sec
271  +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
272  #elif defined(_WIN32)
273  _timeb nowTimeVal;
274  _ftime(&nowTimeVal);
275  double fSeconds = (nowTimeVal.time
276  +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
277  #else
278  timeval nowTimeVal;
279  gettimeofday(&nowTimeVal,0);
280  double fSeconds = (nowTimeVal.tv_sec
281  +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
282  #endif
283 
284  static char acBuffer[255];
285  static char acOutBuffer[300];
286  vsprintf(acBuffer, pcWarningMessage, args);
287  sprintf(acOutBuffer, "\nWARNING: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);
288  sprintf(acOutBuffer, "\nWARNING: %s::%s", m_acClassName, acBuffer);
289  if (m_bDebugFile==true)
290  {
291  FILE* hFile;
292  hFile=fopen(g_pcDebugFileName,"a+");
293  if(hFile != NULL)
294  {
295  fprintf(hFile, "%s", acOutBuffer);
296  fclose(hFile);
297  }
298 
299  }
300 
301 #ifdef WIN32
302  OutputDebugString(acOutBuffer);
303 #else
304  if (m_bDebug)
305  fprintf(stderr, "%s", acOutBuffer);
306 #endif
307 
308  va_end(args);
309  LEAVECS;
310 
311 };
312 
313 void CMessage::logging(const char *pcLoggingMessage,...)
314 {
315 
316  ENTERCS;
317  static char acBuffer[255];
318  va_list args;
319  va_start(args, pcLoggingMessage);
320  vsprintf(acBuffer, pcLoggingMessage, args);
321  va_end(args);
322  FILE *m_hLogFile=fopen("log.txt","a+");
323  if(m_hLogFile != NULL)
324  {
325  fprintf(m_hLogFile,"%s",acBuffer);
326  fclose(m_hLogFile);
327  }
328  LEAVECS;
329 
330 };
331 
332 void CMessage::debug(const int iDebugLevel,
333  const char *pcDebugMessage,...) const
334 {
335 
336  //UHR:use m_Debug as flag for screen output
337  //orig: if(iDebugLevel > m_iDebugLevel || !m_bDebug)
338  if(iDebugLevel > m_iDebugLevel )
339  return;
340  ENTERCS;
341 
342  va_list args;
343 
344  va_start(args, pcDebugMessage);
345 
346  #if defined(__QNX__)
347  timespec nowTimeVal;
348  clock_gettime(CLOCK_REALTIME,&nowTimeVal);
349  double fSeconds = (nowTimeVal.tv_sec
350  +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
351  #elif defined(_WIN32)
352  _timeb nowTimeVal;
353  _ftime(&nowTimeVal);
354  double fSeconds = (nowTimeVal.time
355  +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
356  #else
357  timeval nowTimeVal;
358  gettimeofday(&nowTimeVal,0);
359  double fSeconds = (nowTimeVal.tv_sec
360  +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
361  #endif
362 
363  static char acBuffer[255];
364  static char acOutBuffer[300];
365  vsprintf(acBuffer, pcDebugMessage, args);
366  sprintf(acOutBuffer, "\nDEBUG: %i %5.3f %s::%s", iDebugLevel, fSeconds, m_acClassName, acBuffer);
367  if (m_bDebugFile==true)
368  {
369 
370  FILE* hFile;
371  hFile=fopen(g_pcDebugFileName,"a+");
372  if(hFile != NULL)
373  {
374  fprintf(hFile, "%s", acOutBuffer);
375  fclose(hFile);
376  }
377 
378  }
379 
380 #ifdef WIN32
381  OutputDebugString(acOutBuffer);
382 #else
383  if (m_bDebug)
384  {
385  fprintf(stderr, "%s", acOutBuffer);
386  }
387 #endif
388 
389  va_end(args);
390  LEAVECS;
391 
392 };
void setDebug(bool bFlag)
Definition: Message.cpp:124
#define LEAVECS
Definition: Message.cpp:31
static void setCriticalSection(CRITICAL_SECTION *cs)
Definition: Message.cpp:134
int initMessage(const char *pcClassName, int iDebuglevel=0, bool bDebug=true, bool bDebugFile=false)
Definition: Message.cpp:90
void setInitTime(void)
Definition: Message.cpp:99
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
bool m_bDebugFile
Definition: Message.h:61
int getDebugLevel() const
Definition: Message.cpp:79
int g_iDebugLevel
Definition: Message.cpp:20
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
bool g_bDebug
Definition: Message.cpp:22
void setDebugLevel(int iLevel)
Definition: Message.cpp:119
bool g_bDebugFile
Definition: Message.cpp:21
void setDebugFile(bool bFlag)
Definition: Message.cpp:129
static CRITICAL_SECTION * m_csMessage
Definition: Message.h:53
static double m_fInitTime
Definition: Message.h:63
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
Definition: Message.cpp:332
#define ENTERCS
Definition: Message.cpp:29
CMessage(void)
Definition: Message.cpp:39
virtual ~CMessage(void)
Definition: Message.cpp:54
int m_iDebugLevel
Definition: Message.h:62
void logging(const char *pcLoggingMessage,...)
Definition: Message.cpp:313
CMessage & operator=(const CMessage &clMessage)
Definition: Message.cpp:64
bool m_bDebug
Definition: Message.h:60
const char * g_pcDebugFileName
Definition: Message.cpp:23
char m_acClassName[50]
Definition: Message.h:59


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19