📄 #prettydebug.cc#
字号:
// -*- mode:C++ ; tab-width:4 ; c-basic-offset:4 ; indent-tabs-mode:nil -*-// ----------------------------------------------------------------------------// CERTI - HLA RunTime Infrastructure// Copyright (C) 2002, 2003 ONERA//// This file is part of CERTI-libCERTI//// CERTI-libCERTI is free software ; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public License// as published by the Free Software Foundation ; either version 2 of// the License, or (at your option) any later version.//// CERTI-libCERTI is distributed in the hope that it will be useful, but// WITHOUT ANY WARRANTY ; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this program ; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307// USA//// $Id: PrettyDebug.cc,v 3.3 2003/03/21 18:22:12 breholee Exp $// ----------------------------------------------------------------------------#include "PrettyDebug.hh"DebugOStream DebugOStream::nullOutputStream(cout);DebugOStream PrettyDebug::defaultOutputStream(cerr);//Fix this pointer to the default initialisation of pointers for your compilerDebugOStream* PrettyDebug::nullOutputStreamPtr = 0 ; DebugOStream& PrettyDebug::nullOutputStream = DebugOStream::nullOutputStream;// ----------------------------------------------------------------------------/** Print the message to the default output ostream. * This function does NOT add any trailing \n. */voidPrettyDebug::Print(DebugOStream& theOutputStream, const char* theHeaderMessage, const char * Message){ if (Message != NULL) theOutputStream << theHeaderMessage << Message; else theOutputStream << theHeaderMessage << pdSEmptyMessage;}// ----------------------------------------------------------------------------/** Parse the environment variable value to find debug keys and enable * debug levels. The internal LEnvVar and LMessage variables must * already have been set. */voidPrettyDebug::ParseEnvString(const char *Name){ unsigned int i; const char *pdInitMessage = " variable read, content = "; const char *pdTooLongInitMessage = "Env Var read(string too long)."; char *Value = getenv(Name); // The value of the variable Name char Key; // A character from the value compared to keys. char *Pos; // The position of Key in pdDebugKeys char *DebugKeys = (char *) pdDebugKeysString ; // needs a non const string for (i = pdUnused; i < pdLast + 1; i++) // Clear current level map Level_Map[i] = PrettyDebug::nullOutputStreamPtr; if (Value != 0) { // Print Debug Init Message int length = strlen(LEnvVar) + strlen(pdInitMessage) + strlen(Value); if (length < pdMaxMessageSize) cout << endl << LEnvVar << pdInitMessage << Value << endl; else cout << endl << LEnvVar << pdTooLongInitMessage << endl; // Compare each char of Value to content of the pgDebugKeys // string, to enable matching debug levels. for (i = 0; i < strlen(Value); i++) { Key = Value[i]; Pos = strchr(DebugKeys, Key); if (Pos != 0) Level_Map [Pos - DebugKeys] = &(PrettyDebug::defaultOutputStream); } } return;}// ---------------------------------------------------------------------------- /** Constructor. Initialize the debug process according to the value * of the 'Name' environment variable. The 'Header' message is put * in front of all printed debug messages. It can be a module name or * whatever you need. */PrettyDebug::PrettyDebug(const char * Name, const char * Header){ if (Name == 0) { PrettyDebug::Print(PrettyDebug::defaultOutputStream, "", "Error in pgCDebug constructor, no Name specified.\n"); exit(EXIT_FAILURE); } LEnvVar = strdup(Name); if (Header != 0) HeaderMessage = strdup(Header); else HeaderMessage = strdup(""); //Initialisation de LMessage 庎 0. Il est allou庨 la premi庤re fois //dans la m庨thode Out. LMessage = 0; if ((LEnvVar == 0) || (HeaderMessage == 0)) { PrettyDebug::Print(PrettyDebug::defaultOutputStream, "", "Error in pgCDebug constructor memory allocation.\n"); exit(EXIT_FAILURE); } ParseEnvString(Name);}// ----------------------------------------------------------------------------/** Destructor. */PrettyDebug::~PrettyDebug(){ free(LEnvVar); if (LMessage != 0) { free(LMessage); } if (HeaderMessage != 0) { free(HeaderMessage); }}// ----------------------------------------------------------------------------/** Return true if Level is enabled, else return false. */boolPrettyDebug::Mode(pdDebugLevel Level){ return (Level_Map[Level] != PrettyDebug::nullOutputStreamPtr);}// ----------------------------------------------------------------------------/** Enable the echoing of the given debug level with the provided * ostream or modify the ostream if the debug level was already * enabled. @attention: only the address of the given ostream is * stored, it is not copied so the ostream MUST exist until the * destruction of the PrettyDebug or until the level is disabled */voidPrettyDebug::enableDebugLevel(pdDebugLevel Level, DebugOStream& theOutputStream){ Level_Map[Level] = &(theOutputStream); return;}// ----------------------------------------------------------------------------/** Disable the echoing of the given debug level. */voidPrettyDebug::disableDebugLevel(pdDebugLevel Level){ Level_Map[Level] = PrettyDebug::nullOutputStreamPtr; return;} // ----------------------------------------------------------------------------#ifndef NO_PRETTYDEBUG/** If Level is enabled, Message is sent to the DebugServer, preceded * with the Header specified in the Constructor. If the * NO_PRETTYDEBUG constant is defined, the Out method has beed * declared inline, and its body set to {}(see PrettyDebug.hh). * @attention: Probl庤me, rien ne garantit qu'on ne d庨passera pas le * nombre max de char dans le vsprintf. Mieux vaut utiliser la * syntaxe C++ */voidPrettyDebug::Out(pdDebugLevel Level, const char * Format, ...){ const char *pdEmptyMessage = "Pretty Debug received an empty Message."; DebugOStream* theOutputStreamPtr = Level_Map[Level]; //Warnings messages //if (LEnvVar == 0) cout << endl << "ENV VAR NULL CALLING OUT METHOD "; //if (HeaderMessage == 0) cout << "HEADER NULL CALLING OUT METHOD" << endl; if (theOutputStreamPtr != PrettyDebug::nullOutputStreamPtr) { if (Format != 0) { // Cat Header and Message strings if (LMessage == 0) { // A final printed message is // Header+Message+\n(+\0) //Optimisation, on pouurait peut-庩tre mettre LMessage //en static. Mais il ne faudra pas que dans un D.Out //qu'on appelle un autre D.Out car autrement, seul un //message serait affich庨. LMessage = (char *) malloc((pdMaxMessageSize+2) * sizeof(char)); if (LMessage == 0) { PrettyDebug::Print(PrettyDebug::defaultOutputStream, HeaderMessage, "Error in pgCDebug Out method while " "allocating initial memory for messages\n"); exit(EXIT_FAILURE); } else PrettyDebug::Print(PrettyDebug::defaultOutputStream, HeaderMessage, "Allocated initial memory " "for Message buffer.\n"); } va_list argptr; // Variable Argument list, see stdarg.h va_start(argptr, Format); //Probl庤me, rien ne garantit qu'on ne d庨passera pas le //nombre max de char dans le vsprintf. vsprintf(LMessage, Format, argptr); va_end(argptr); } else strcpy(LMessage, pdEmptyMessage); strcat(LMessage, "\n"); // Add trailing \n PrettyDebug::Print(*theOutputStreamPtr, HeaderMessage, LMessage); } return;}#endif // NO_PRETTYDEBUG// $Id: PrettyDebug.cc,v 3.3 2003/03/21 18:22:12 breholee Exp $
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -