Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

trazas.hpp

Go to the documentation of this file.
00001 #ifndef __TRAZAS_H__
00002 #define __TRAZAS_H__
00003 
00004 #include "ace/Thread.h" 
00005 #include "ace/Synch_T.h" 
00006 #include "ace/Synch.h" 
00007 
00008 #include <fstream>
00009 #include <strstream>
00010 #include <map>
00011 #include <string>
00012 
00013 namespace TRAZAS
00014 {
00015 
00016 class Fichero
00017 {
00018 public:
00019   Fichero(const std::string &nombre);
00020   ~Fichero();
00021 
00022   void escribe(const std::string &cadena, int indentacion);
00023 private:
00024   std::ofstream *_fichero;
00025   ACE_Thread_Mutex _mutex;
00026 };
00027 
00028 class Contenedor
00029 {
00030 protected:
00031   // Impedir la instanciacion de objetos de esta clase
00032   Contenedor() {}
00033 
00034 public:
00035 
00036   // Metodos de inicializacion
00037   static void inicializa(const std::string &directorio,
00038                          const std::string &nombreProceso);
00039   static unsigned int dameNivel() { return _nivel; }
00040   static void ponNivel(unsigned int nivel) { _nivel = nivel; }
00041 
00042   // Si el thread no existe crean uno y lo inicializan
00043   // Si ya existe no hace nada
00044   static void ponFichero(ACE_thread_t id);
00045   static void ponFichero(ACE_thread_t id, const std::string &nombreFichero);
00046 
00047   // Metodos de escritura de trazas
00048   static void escribeTraza(ACE_thread_t id, const std::string &cadena);
00049   static void aumentaIndentacion(ACE_thread_t id);
00050   static void disminuyeIndentacion(ACE_thread_t id);
00051 
00052 private:
00053   static const std::string FICHERO_THREADS_DEFECTO;
00054   static const std::string FICHERO_MAIN_DEFECTO;
00055 
00056   static Fichero* _dameFichero(const std::string &nombre);
00057 
00058   struct EstructuraThread
00059   {
00060     Fichero *fichero;
00061     int nivelIndentacion;
00062   };
00063   typedef std::map<ACE_thread_t, EstructuraThread*> MapaThreads;
00064 
00065   typedef std::map<std::string, Fichero*> MapaFicheros;
00066 
00067   static MapaFicheros _mapaFicheros;
00068   static MapaThreads _mapaThreads;
00069   static std::string _directorioTrazas;
00070   static std::string _nombreProceso;
00071   static bool _inicializado;
00072 
00073   static ACE_RW_Thread_Mutex _mutex;
00074   static unsigned int _nivel;
00075   static ACE_thread_t _main;
00076 };
00077 
00078 //=========================================================================
00079 
00080 class TrazaMetodo
00081 {
00082 public:
00083   TrazaMetodo(const std::string &nombreMetodo, unsigned int nivel);
00084   ~TrazaMetodo();
00085 private:
00086   unsigned int _nivel;
00087   std::string _nombreMetodo;
00088   ACE_thread_t _id;
00089 };
00090 
00091 }
00092 
00093 //=========================================================================
00094 // Macros
00095 //=========================================================================
00096 
00097 #define NIVEL_NULO 0
00098 #define NIVEL_SUCESO 1
00099 #define NIVEL_TRAZA 2
00100 #define NIVEL_DEPURA 3
00101 
00102 #ifndef NIVEL_DEFINIDO_TRAZAS
00103 #define NIVEL_DEFINIDO_TRAZAS 3
00104 #endif
00105 
00106 #define INICIO_TRAZAS(directorio,argv0) \
00107   TRAZAS::Contenedor::inicializa(directorio, argv0);
00108 
00109 #if (NIVEL_DEFINIDO_TRAZAS >= NIVEL_DEPURA) 
00110   #define O_DEPURA(cadena) \
00111     std::strstream *__Aux_StrStream__ = new std::strstream; \
00112     (*__Aux_StrStream__) << cadena << std::ends; \
00113     char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00114     TRAZAS::TrazaMetodo __Aux_Obj_Traza__(__Aux_Cadena__, NIVEL_DEPURA); \
00115     delete __Aux_Cadena__; \
00116     delete __Aux_StrStream__;
00117 
00118   #define DEPURA(cadena) \
00119     if (TRAZAS::Contenedor::dameNivel() >= NIVEL_DEPURA) \
00120     { \
00121       std::strstream *__Aux_StrStream__ = new std::strstream; \
00122       (*__Aux_StrStream__) << cadena << std::ends; \
00123       char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00124       TRAZAS::Contenedor::escribeTraza(ACE_Thread::self(), __Aux_Cadena__); \
00125       delete __Aux_Cadena__; \
00126       delete __Aux_StrStream__; \
00127     }
00128 #else
00129   #define O_DEPURA(cadena) 
00130   #define DEPURA(cadena)
00131 #endif
00132 
00133 #if (NIVEL_DEFINIDO_TRAZAS >= NIVEL_TRAZA) 
00134   #define O_TRAZA(cadena) \
00135     std::strstream *__Aux_StrStream__ = new std::strstream; \
00136     (*__Aux_StrStream__) << cadena << std::ends; \
00137     char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00138     TRAZAS::TrazaMetodo __Aux_Obj_Traza__(__Aux_Cadena__, NIVEL_TRAZA); \
00139     delete __Aux_Cadena__; \
00140     delete __Aux_StrStream__;
00141 
00142   #define TRAZA(cadena) \
00143     if (TRAZAS::Contenedor::dameNivel() >= NIVEL_TRAZA) \
00144     { \
00145       std::strstream *__Aux_StrStream__ = new std::strstream; \
00146       (*__Aux_StrStream__) << cadena << std::ends; \
00147       char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00148       TRAZAS::Contenedor::escribeTraza(ACE_Thread::self(), __Aux_Cadena__); \
00149       delete __Aux_Cadena__; \
00150       delete __Aux_StrStream__; \
00151     }
00152 #else
00153   #define O_TRAZA(cadena) 
00154   #define TRAZA(cadena)
00155 #endif
00156 
00157 #if (NIVEL_DEFINIDO_TRAZAS >= NIVEL_SUCESO) 
00158   #define O_SUCESO(cadena) \
00159     std::strstream *__Aux_StrStream__ = new std::strstream; \
00160     (*__Aux_StrStream__) << cadena << std::ends; \
00161     char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00162     TRAZAS::TrazaMetodo __Aux_Obj_Traza__(__Aux_Cadena__, NIVEL_SUCESO); \
00163     delete __Aux_Cadena__; \
00164     delete __Aux_StrStream__;
00165 
00166   #define SUCESO(cadena) \
00167     if (TRAZAS::Contenedor::dameNivel() >= NIVEL_SUCESO) \
00168     { \
00169       std::strstream *__Aux_StrStream__ = new std::strstream; \
00170       (*__Aux_StrStream__) << cadena << std::ends; \
00171       char * __Aux_Cadena__ = __Aux_StrStream__->str(); \
00172       TRAZAS::Contenedor::escribeTraza(ACE_Thread::self(), __Aux_Cadena__); \
00173       delete __Aux_Cadena__; \
00174       delete __Aux_StrStream__; \
00175     }
00176 #else
00177   #define O_SUCESO(cadena) 
00178   #define SUCESO(cadena)
00179 #endif
00180 
00181 #define PON_NIVEL_TRAZAS(nivel) \
00182   TRAZAS::Contenedor::ponNivel(nivel)
00183 
00184 #if (NIVEL_DEFINIDO_TRAZAS > 0)
00185   #define FICHERO_TRAZA(cadena) \
00186     TRAZAS::Contenedor::ponFichero(ACE_Thread::self(), cadena);
00187 #else
00188   #define FICHERO_TRAZA(cadena) 
00189 #endif
00190 
00191 #endif

Generated on Wed Mar 5 21:31:47 2003 for JIC by doxygen1.3-rc3