Message.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "Message.h"
00019 
00020 int g_iDebugLevel = 3;
00021 bool g_bDebugFile = false;
00022 bool g_bDebug = true;
00023 const char* g_pcDebugFileName = "debug.txt";
00024 
00025 double CMessage::m_fInitTime;
00026 
00027 CRITICAL_SECTION* CMessage::m_csMessage = NULL;
00028 
00029 #define ENTERCS if(m_csMessage!=NULL) EnterCriticalSection(m_csMessage);
00030 
00031 #define LEAVECS if(m_csMessage!=NULL) LeaveCriticalSection(m_csMessage);
00032 
00033 // ========================================================================== ;
00034 //                                                                            ;
00035 // ---- constructors / destructor ------------------------------------------- ;
00036 //                                                                            ;
00037 // ========================================================================== ;
00038 
00039 CMessage::CMessage() : m_bDebug(g_bDebug), m_bDebugFile(g_bDebugFile), m_iDebugLevel(g_iDebugLevel)
00040 {
00041         m_acClassName[0] = 0;
00042 }
00043 
00044 CMessage::CMessage(const char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile) : m_bDebug(bDebug), m_bDebugFile(bDebugFile), m_iDebugLevel(iDebuglevel)
00045 {
00046         strncpy(m_acClassName ,pcClassName, 50);
00047 }
00048 
00049 CMessage::CMessage(const CMessage& clMessage) : m_bDebug(clMessage.m_bDebug), m_bDebugFile(clMessage.m_bDebugFile), m_iDebugLevel(clMessage.m_iDebugLevel)
00050 {
00051         strncpy(m_acClassName ,clMessage.m_acClassName, 50);
00052 }
00053 
00054 CMessage::~CMessage(void)
00055 {
00056 }
00057 
00058 // ========================================================================== ;
00059 //                                                                            ;
00060 // ---- operators ----------------------------------------------------------- ;
00061 //                                                                            ;
00062 // ========================================================================== ;
00063 
00064 CMessage& CMessage::operator=(const CMessage& clMessage)
00065 {
00066         strncpy(m_acClassName ,clMessage.m_acClassName, 50);
00067         m_bDebug = clMessage.m_bDebug;
00068         m_bDebugFile = clMessage.m_bDebugFile;
00069         m_iDebugLevel = clMessage.m_iDebugLevel;
00070         return *this;
00071 }
00072 
00073 // ========================================================================== ;
00074 //                                                                            ;
00075 // ---- query functions ----------------------------------------------------- ;
00076 //                                                                            ;
00077 // ========================================================================== ;
00078 
00079 int CMessage::getDebugLevel() const
00080 {
00081         return m_iDebugLevel;
00082 }
00083 
00084 // ========================================================================== ;
00085 //                                                                            ;
00086 // ---- modify functions ---------------------------------------------------- ;
00087 //                                                                            ;
00088 // ========================================================================== ;
00089 
00090 int CMessage::initMessage(const char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile)
00091 {
00092         strncpy(m_acClassName, pcClassName, 50);
00093         m_bDebug = bDebug;
00094         m_bDebugFile = bDebugFile;
00095         m_iDebugLevel = iDebuglevel;
00096         return 0;
00097 }
00098 
00099 void CMessage::setInitTime()
00100 {
00101         #if defined(__QNX__)
00102                 timespec nowTimeVal;
00103                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00104                 m_fInitTime = (nowTimeVal.tv_sec
00105                   +(double(nowTimeVal.tv_nsec)/1e+9));
00106         #elif defined(_WIN32)
00107                 _timeb nowTimeVal;
00108                 _ftime(&nowTimeVal);
00109                 m_fInitTime = (nowTimeVal.time
00110                   +(double(nowTimeVal.millitm)/1e+3));
00111         #else
00112                 timeval nowTimeVal;
00113                 gettimeofday(&nowTimeVal,0);
00114                 m_fInitTime = (nowTimeVal.tv_sec
00115                   +(double(nowTimeVal.tv_usec)/1e+6));
00116         #endif
00117 }
00118 
00119 void CMessage::setDebugLevel(int iLevel)
00120 {
00121         m_iDebugLevel = iLevel;
00122 }
00123 
00124 void CMessage::setDebug(bool bFlag)
00125 {
00126         m_bDebug = bFlag;
00127 }
00128 
00129 void CMessage::setDebugFile(bool bFlag)
00130 {
00131         m_bDebugFile = bFlag;
00132 }
00133 
00134 void CMessage::setCriticalSection(CRITICAL_SECTION *csMessage)
00135 {
00136         m_csMessage = csMessage;
00137 }
00138 
00139 // ========================================================================== ;
00140 //                                                                            ;
00141 // ---- I/O functions ------------------------------------------------------- ;
00142 //                                                                            ;
00143 // ========================================================================== ;
00144 
00145 // ========================================================================== ;
00146 //                                                                            ;
00147 // ---- exec functions ------------------------------------------------------ ;
00148 //                                                                            ;
00149 // ========================================================================== ;
00150 
00151 void CMessage::error(const char *pcErrorMessage,...) const
00152 {
00153 
00154         ENTERCS;
00155         va_list args;
00156   
00157         va_start(args, pcErrorMessage);
00158 
00159         #if defined(__QNX__)
00160                 timespec nowTimeVal;
00161                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00162                 double fSeconds = (nowTimeVal.tv_sec
00163                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00164         #elif defined(_WIN32)
00165                 _timeb nowTimeVal;
00166                 _ftime(&nowTimeVal);
00167                 double fSeconds = (nowTimeVal.time
00168                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00169         #else
00170                 timeval nowTimeVal;
00171                 gettimeofday(&nowTimeVal,0);
00172                 double fSeconds = (nowTimeVal.tv_sec
00173                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00174         #endif
00175 
00176         static char acBuffer[255];      
00177         static char acOutBuffer[300];
00178         vsprintf(acBuffer, pcErrorMessage, args);       
00179         sprintf(acOutBuffer, "\nERROR: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);       
00180         if (m_bDebugFile==true)
00181         {       
00182                 
00183                 FILE* hFile;
00184                 hFile=fopen(g_pcDebugFileName,"a+");
00185                 if(hFile != NULL)
00186                 {
00187                         fprintf(hFile, "%s", acOutBuffer);
00188                         fclose(hFile);
00189                 }
00190         }
00191                 
00192 #ifdef WIN32
00193         OutputDebugString(acOutBuffer);
00194 #else
00195         fprintf(stderr, "%s",acOutBuffer);
00196 #endif
00197 
00198         va_end(args);
00199         LEAVECS;
00200         exit(-1);
00201 
00202 };
00203 
00204 void CMessage::error(const int iErrorCode,                      
00205                     const char *pcErrorMessage,...)const
00206 {
00207 
00208         ENTERCS;
00209         va_list args;
00210   
00211         va_start(args, pcErrorMessage);
00212 
00213         #if defined(__QNX__)
00214                 timespec nowTimeVal;
00215                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00216                 double fSeconds = (nowTimeVal.tv_sec
00217                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00218         #elif defined(_WIN32)
00219                 _timeb nowTimeVal;
00220                 _ftime(&nowTimeVal);
00221                 double fSeconds = (nowTimeVal.time
00222                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00223         #else
00224                 timeval nowTimeVal;
00225                 gettimeofday(&nowTimeVal,0);
00226                 double fSeconds = (nowTimeVal.tv_sec
00227                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00228         #endif
00229 
00230         static char acBuffer[255];      
00231         static char acOutBuffer[300];
00232         vsprintf(acBuffer, pcErrorMessage, args);       
00233         sprintf(acOutBuffer, "\nERROR: #%i %5.3f %s::%s", iErrorCode, fSeconds, m_acClassName, acBuffer);       
00234         if (m_bDebugFile==true)
00235         {       
00236                 
00237                 FILE* hFile;
00238                 hFile=fopen(g_pcDebugFileName,"a+");
00239                 if(hFile != NULL)
00240                 {
00241                         fprintf(hFile, "%s", acOutBuffer);
00242                         fclose(hFile);
00243                 }
00244                 
00245         }
00246                 
00247 #ifdef WIN32
00248         OutputDebugString(acOutBuffer);
00249 #else
00250         fprintf(stderr, "%s", acOutBuffer);
00251 #endif
00252         LEAVECS;
00253         exit(-1);
00254 
00255 };
00256 
00257 void CMessage::warning(const char *pcWarningMessage,...) const
00258 {
00259         //UHR:use m_Debug as flag for screen output
00260         //if(!m_bDebug)
00261         //              return;
00262         ENTERCS;
00263         va_list args;
00264   
00265         va_start(args, pcWarningMessage);
00266 
00267         #if defined(__QNX__)
00268                 timespec nowTimeVal;
00269                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00270                 double fSeconds = (nowTimeVal.tv_sec
00271                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00272         #elif defined(_WIN32)
00273                 _timeb nowTimeVal;
00274                 _ftime(&nowTimeVal);
00275                 double fSeconds = (nowTimeVal.time
00276                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00277         #else
00278                 timeval nowTimeVal;
00279                 gettimeofday(&nowTimeVal,0);
00280                 double fSeconds = (nowTimeVal.tv_sec
00281                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00282         #endif
00283 
00284         static char acBuffer[255];      
00285         static char acOutBuffer[300];
00286         vsprintf(acBuffer, pcWarningMessage, args);     
00287         sprintf(acOutBuffer, "\nWARNING: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);     
00288         sprintf(acOutBuffer, "\nWARNING: %s::%s", m_acClassName, acBuffer);     
00289         if (m_bDebugFile==true)
00290         {               
00291                 FILE* hFile;
00292                 hFile=fopen(g_pcDebugFileName,"a+");
00293                 if(hFile != NULL)
00294                 {
00295                         fprintf(hFile, "%s", acOutBuffer);
00296                         fclose(hFile);
00297                 }
00298                 
00299         }
00300                 
00301 #ifdef WIN32
00302         OutputDebugString(acOutBuffer);
00303 #else
00304         if (m_bDebug)
00305                 fprintf(stderr, "%s", acOutBuffer);
00306 #endif
00307 
00308         va_end(args);
00309         LEAVECS;
00310 
00311 };
00312 
00313 void CMessage::logging(const char *pcLoggingMessage,...) 
00314 {
00315 
00316         ENTERCS;
00317         static char acBuffer[255];      
00318         va_list args;
00319         va_start(args, pcLoggingMessage);
00320         vsprintf(acBuffer, pcLoggingMessage, args);     
00321         va_end(args);
00322         FILE *m_hLogFile=fopen("log.txt","a+");
00323         if(m_hLogFile != NULL)
00324         {
00325                 fprintf(m_hLogFile,"%s",acBuffer);      
00326                 fclose(m_hLogFile);
00327         }
00328         LEAVECS;
00329 
00330 };
00331 
00332 void CMessage::debug(const int iDebugLevel,
00333                       const char *pcDebugMessage,...) const
00334 {
00335 
00336   //UHR:use m_Debug as flag for screen output
00337         //orig: if(iDebugLevel > m_iDebugLevel || !m_bDebug)
00338         if(iDebugLevel > m_iDebugLevel )
00339                 return;
00340         ENTERCS;
00341 
00342         va_list args;
00343   
00344         va_start(args, pcDebugMessage);
00345 
00346         #if defined(__QNX__)
00347                 timespec nowTimeVal;
00348                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00349                 double fSeconds = (nowTimeVal.tv_sec
00350                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00351         #elif defined(_WIN32)
00352                 _timeb nowTimeVal;
00353                 _ftime(&nowTimeVal);
00354                 double fSeconds = (nowTimeVal.time
00355                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00356         #else
00357                 timeval nowTimeVal;
00358                 gettimeofday(&nowTimeVal,0);
00359                 double fSeconds = (nowTimeVal.tv_sec
00360                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00361         #endif
00362         
00363         static char acBuffer[255];      
00364         static char acOutBuffer[300];
00365         vsprintf(acBuffer, pcDebugMessage, args);       
00366         sprintf(acOutBuffer, "\nDEBUG: %i %5.3f %s::%s", iDebugLevel, fSeconds, m_acClassName, acBuffer);       
00367         if (m_bDebugFile==true)
00368         {       
00369                 
00370                 FILE* hFile;
00371                 hFile=fopen(g_pcDebugFileName,"a+");
00372                 if(hFile != NULL)
00373                 {
00374                         fprintf(hFile, "%s", acOutBuffer);
00375                         fclose(hFile);
00376                 }
00377                 
00378         }
00379                 
00380 #ifdef WIN32
00381         OutputDebugString(acOutBuffer);
00382 #else
00383         if (m_bDebug)
00384         {
00385                 fprintf(stderr, "%s", acOutBuffer);
00386         }
00387 #endif
00388 
00389         va_end(args);
00390         LEAVECS;
00391 
00392 };


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:13