Message.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * 
00004  * Copyright (c) 2012 
00005  * 
00006  * SCHUNK GmbH & Co. KG
00007  *  
00008  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00009  * 
00010  * Project name: Drivers for "Amtec M5 Protocol" Electronics V4
00011  *                                                                        
00012  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00013  * 
00014  * Email:robotics@schunk.com
00015  * 
00016  * ToDo: 
00017  * 
00018  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00019  * 
00020  * Redistribution and use in source and binary forms, with or without 
00021  * modification, are permitted provided that the following conditions are met: 
00022  * 
00023  *  * Redistributions of source code must retain the above copyright 
00024  *    notice, this list of conditions and the following disclaimer. 
00025  *  * Redistributions in binary form must reproduce the above copyright 
00026  *    notice, this list of conditions and the following disclaimer in the 
00027  *    documentation and/or other materials provided with the distribution. 
00028  *  * Neither the name of SCHUNK GmbH & Co. KG nor the names of its 
00029  *    contributors may be used to endorse or promote products derived from 
00030  *    this software without specific prior written permission. 
00031  * 
00032  * This program is free software: you can redistribute it and/or modify 
00033  * it under the terms of the GNU Lesser General Public License LGPL as 
00034  * published by the Free Software Foundation, either version 3 of the 
00035  * License, or (at your option) any later version. 
00036  * 
00037  * This program is distributed in the hope that it will be useful, 
00038  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00039  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00040  * GNU Lesser General Public License LGPL for more details. 
00041  * 
00042  * You should have received a copy of the GNU Lesser General Public 
00043  * License LGPL along with this program. 
00044  * If not, see <http://www.gnu.org/licenses/>.
00045  * 
00046  ******************************************************************************/
00047 
00048 
00049 #include "Message.h"
00050 
00051 int g_iDebugLevel = 3;
00052 bool g_bDebugFile = false;
00053 bool g_bDebug = true;
00054 char* g_pcDebugFileName = "debug.txt";
00055 
00056 double CMessage::m_fInitTime;
00057 
00058 CRITICAL_SECTION* CMessage::m_csMessage = NULL;
00059 
00060 #define ENTERCS if(m_csMessage!=NULL) EnterCriticalSection(m_csMessage);
00061 
00062 #define LEAVECS if(m_csMessage!=NULL) LeaveCriticalSection(m_csMessage);
00063 
00064 // ========================================================================== ;
00065 //                                                                            ;
00066 // ---- constructors / destructor ------------------------------------------- ;
00067 //                                                                            ;
00068 // ========================================================================== ;
00069 
00070 CMessage::CMessage() : m_bDebug(g_bDebug), m_bDebugFile(g_bDebugFile), m_iDebugLevel(g_iDebugLevel)
00071 {
00072         m_acClassName[0] = 0;
00073 }
00074 
00075 CMessage::CMessage(char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile) : m_bDebug(bDebug), m_bDebugFile(bDebugFile), m_iDebugLevel(iDebuglevel)
00076 {
00077         strncpy(m_acClassName ,pcClassName, 50);
00078 }
00079 
00080 CMessage::CMessage(const CMessage& clMessage) : m_bDebug(clMessage.m_bDebug), m_bDebugFile(clMessage.m_bDebugFile), m_iDebugLevel(clMessage.m_iDebugLevel)
00081 {
00082         strncpy(m_acClassName ,clMessage.m_acClassName, 50);
00083 }
00084 
00085 CMessage::~CMessage(void)
00086 {
00087 }
00088 
00089 // ========================================================================== ;
00090 //                                                                            ;
00091 // ---- operators ----------------------------------------------------------- ;
00092 //                                                                            ;
00093 // ========================================================================== ;
00094 
00095 CMessage& CMessage::operator=(const CMessage& clMessage)
00096 {
00097         strncpy(m_acClassName ,clMessage.m_acClassName, 50);
00098         m_bDebug = clMessage.m_bDebug;
00099         m_bDebugFile = clMessage.m_bDebugFile;
00100         m_iDebugLevel = clMessage.m_iDebugLevel;
00101         return *this;
00102 }
00103 
00104 // ========================================================================== ;
00105 //                                                                            ;
00106 // ---- query functions ----------------------------------------------------- ;
00107 //                                                                            ;
00108 // ========================================================================== ;
00109 
00110 int CMessage::getDebugLevel() const
00111 {
00112         return m_iDebugLevel;
00113 }
00114 
00115 // ========================================================================== ;
00116 //                                                                            ;
00117 // ---- modify functions ---------------------------------------------------- ;
00118 //                                                                            ;
00119 // ========================================================================== ;
00120 
00121 int CMessage::initMessage(char* pcClassName, int iDebuglevel, bool bDebug, bool bDebugFile)
00122 {
00123         strncpy(m_acClassName, pcClassName, 50);
00124         m_bDebug = bDebug;
00125         m_bDebugFile = bDebugFile;
00126         m_iDebugLevel = iDebuglevel;
00127         return 0;
00128 }
00129 
00130 void CMessage::setInitTime()
00131 {
00132         #if defined(__QNX__)
00133                 timespec nowTimeVal;
00134                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00135                 m_fInitTime = (nowTimeVal.tv_sec
00136                   +(double(nowTimeVal.tv_nsec)/1e+9));
00137         #elif defined(_WIN32)
00138                 _timeb nowTimeVal;
00139                 _ftime(&nowTimeVal);
00140                 m_fInitTime = (nowTimeVal.time
00141                   +(double(nowTimeVal.millitm)/1e+3));
00142         #else
00143                 timeval nowTimeVal;
00144                 gettimeofday(&nowTimeVal,0);
00145                 m_fInitTime = (nowTimeVal.tv_sec
00146                   +(double(nowTimeVal.tv_usec)/1e+6));
00147         #endif
00148 }
00149 
00150 void CMessage::setDebugLevel(int iLevel)
00151 {
00152         m_iDebugLevel = iLevel;
00153 }
00154 
00155 void CMessage::setDebug(bool bFlag)
00156 {
00157         m_bDebug = bFlag;
00158 }
00159 
00160 void CMessage::setDebugFile(bool bFlag)
00161 {
00162         m_bDebugFile = bFlag;
00163 }
00164 
00165 void CMessage::setCriticalSection(CRITICAL_SECTION *csMessage)
00166 {
00167         m_csMessage = csMessage;
00168 }
00169 
00170 // ========================================================================== ;
00171 //                                                                            ;
00172 // ---- I/O functions ------------------------------------------------------- ;
00173 //                                                                            ;
00174 // ========================================================================== ;
00175 
00176 // ========================================================================== ;
00177 //                                                                            ;
00178 // ---- exec functions ------------------------------------------------------ ;
00179 //                                                                            ;
00180 // ========================================================================== ;
00181 
00182 void CMessage::error(const char *pcErrorMessage,...) const
00183 {
00184 
00185         ENTERCS;
00186         va_list args;
00187   
00188         va_start(args, pcErrorMessage);
00189 
00190         #if defined(__QNX__)
00191                 timespec nowTimeVal;
00192                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00193                 double fSeconds = (nowTimeVal.tv_sec
00194                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00195         #elif defined(_WIN32)
00196                 _timeb nowTimeVal;
00197                 _ftime(&nowTimeVal);
00198                 double fSeconds = (nowTimeVal.time
00199                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00200         #else
00201                 timeval nowTimeVal;
00202                 gettimeofday(&nowTimeVal,0);
00203                 double fSeconds = (nowTimeVal.tv_sec
00204                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00205         #endif
00206 
00207         static char acBuffer[255];      
00208         static char acOutBuffer[300];
00209         vsprintf(acBuffer, pcErrorMessage, args);       
00210         sprintf(acOutBuffer, "\nERROR: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);       
00211         if (m_bDebugFile==true)
00212         {       
00213                 
00214                 FILE* hFile;
00215                 hFile=fopen(g_pcDebugFileName,"a+");
00216                 if(hFile != NULL)
00217                 {
00218                         fprintf(hFile, "%s", acOutBuffer);
00219                         fclose(hFile);
00220                 }
00221         }
00222                 
00223 #ifdef WIN32
00224         OutputDebugString(acOutBuffer);
00225 #else
00226         fprintf(stderr, acOutBuffer);
00227 #endif
00228 
00229         va_end(args);
00230         LEAVECS;
00231         exit(-1);
00232 
00233 };
00234 
00235 void CMessage::error(const int iErrorCode,                      
00236                     const char *pcErrorMessage,...)const
00237 {
00238 
00239         ENTERCS;
00240         va_list args;
00241   
00242         va_start(args, pcErrorMessage);
00243 
00244         #if defined(__QNX__)
00245                 timespec nowTimeVal;
00246                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00247                 double fSeconds = (nowTimeVal.tv_sec
00248                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00249         #elif defined(_WIN32)
00250                 _timeb nowTimeVal;
00251                 _ftime(&nowTimeVal);
00252                 double fSeconds = (nowTimeVal.time
00253                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00254         #else
00255                 timeval nowTimeVal;
00256                 gettimeofday(&nowTimeVal,0);
00257                 double fSeconds = (nowTimeVal.tv_sec
00258                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00259         #endif
00260 
00261         static char acBuffer[255];      
00262         static char acOutBuffer[300];
00263         vsprintf(acBuffer, pcErrorMessage, args);       
00264         sprintf(acOutBuffer, "\nERROR: #%i %5.3f %s::%s", iErrorCode, fSeconds, m_acClassName, acBuffer);       
00265         if (m_bDebugFile==true)
00266         {       
00267                 
00268                 FILE* hFile;
00269                 hFile=fopen(g_pcDebugFileName,"a+");
00270                 if(hFile != NULL)
00271                 {
00272                         fprintf(hFile, "%s", acOutBuffer);
00273                         fclose(hFile);
00274                 }
00275                 
00276         }
00277                 
00278 #ifdef WIN32
00279         OutputDebugString(acOutBuffer);
00280 #else
00281         fprintf(stderr, acOutBuffer);
00282 #endif
00283         LEAVECS;
00284         exit(-1);
00285 
00286 };
00287 
00288 void CMessage::warning(const char *pcWarningMessage,...) const
00289 {
00290         //UHR:use m_Debug as flag for screen output
00291         //if(!m_bDebug)
00292         //              return;
00293         ENTERCS;
00294         va_list args;
00295   
00296         va_start(args, pcWarningMessage);
00297 
00298         #if defined(__QNX__)
00299                 timespec nowTimeVal;
00300                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00301                 double fSeconds = (nowTimeVal.tv_sec
00302                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00303         #elif defined(_WIN32)
00304                 _timeb nowTimeVal;
00305                 _ftime(&nowTimeVal);
00306                 double fSeconds = (nowTimeVal.time
00307                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00308         #else
00309                 timeval nowTimeVal;
00310                 gettimeofday(&nowTimeVal,0);
00311                 double fSeconds = (nowTimeVal.tv_sec
00312                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00313         #endif
00314 
00315         static char acBuffer[255];      
00316         static char acOutBuffer[300];
00317         vsprintf(acBuffer, pcWarningMessage, args);     
00318         sprintf(acOutBuffer, "\nWARNING: %5.3f %s::%s", fSeconds, m_acClassName, acBuffer);     
00319         sprintf(acOutBuffer, "\nWARNING: %s::%s", m_acClassName, acBuffer);     
00320         if (m_bDebugFile==true)
00321         {               
00322                 FILE* hFile;
00323                 hFile=fopen(g_pcDebugFileName,"a+");
00324                 if(hFile != NULL)
00325                 {
00326                         fprintf(hFile, "%s", acOutBuffer);
00327                         fclose(hFile);
00328                 }
00329                 
00330         }
00331                 
00332 #ifdef WIN32
00333         OutputDebugString(acOutBuffer);
00334 #else
00335         if (m_bDebug)
00336                 fprintf(stderr, acOutBuffer);
00337 #endif
00338 
00339         va_end(args);
00340         LEAVECS;
00341 
00342 };
00343 
00344 void CMessage::logging(const char *pcLoggingMessage,...) 
00345 {
00346 
00347         ENTERCS;
00348         static char acBuffer[255];      
00349         va_list args;
00350         va_start(args, pcLoggingMessage);
00351         vsprintf(acBuffer, pcLoggingMessage, args);     
00352         va_end(args);
00353         FILE *m_hLogFile=fopen("log.txt","a+");
00354         if(m_hLogFile != NULL)
00355         {
00356                 fprintf(m_hLogFile,"%s",acBuffer);      
00357                 fclose(m_hLogFile);
00358         }
00359         LEAVECS;
00360 
00361 };
00362 
00363 void CMessage::debug(const int iDebugLevel,
00364                       const char *pcDebugMessage,...) const
00365 {
00366 
00367   //UHR:use m_Debug as flag for screen output
00368         //orig: if(iDebugLevel > m_iDebugLevel || !m_bDebug)
00369         if(iDebugLevel > m_iDebugLevel )
00370                 return;
00371         ENTERCS;
00372 
00373         va_list args;
00374   
00375         va_start(args, pcDebugMessage);
00376 
00377         #if defined(__QNX__)
00378                 timespec nowTimeVal;
00379                 clock_gettime(CLOCK_REALTIME,&nowTimeVal);
00380                 double fSeconds = (nowTimeVal.tv_sec
00381                   +(double(nowTimeVal.tv_nsec)/1e+9)) - m_fInitTime;
00382         #elif defined(_WIN32)
00383                 _timeb nowTimeVal;
00384                 _ftime(&nowTimeVal);
00385                 double fSeconds = (nowTimeVal.time
00386                   +(double(nowTimeVal.millitm)/1e+3)) - m_fInitTime;
00387         #else
00388                 timeval nowTimeVal;
00389                 gettimeofday(&nowTimeVal,0);
00390                 double fSeconds = (nowTimeVal.tv_sec
00391                   +(double(nowTimeVal.tv_usec)/1e+6)) - m_fInitTime;
00392         #endif
00393         
00394         static char acBuffer[255];      
00395         static char acOutBuffer[300];
00396         vsprintf(acBuffer, pcDebugMessage, args);       
00397         sprintf(acOutBuffer, "\nDEBUG: %i %5.3f %s::%s", iDebugLevel, fSeconds, m_acClassName, acBuffer);       
00398         if (m_bDebugFile==true)
00399         {       
00400                 
00401                 FILE* hFile;
00402                 hFile=fopen(g_pcDebugFileName,"a+");
00403                 if(hFile != NULL)
00404                 {
00405                         fprintf(hFile, "%s", acOutBuffer);
00406                         fclose(hFile);
00407                 }
00408                 
00409         }
00410                 
00411 #ifdef WIN32
00412         OutputDebugString(acOutBuffer);
00413 #else
00414         if (m_bDebug)
00415         {
00416                 fprintf(stderr, acOutBuffer);
00417         }
00418 #endif
00419 
00420         va_end(args);
00421         LEAVECS;
00422 
00423 };


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Oct 6 2014 07:30:34