1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_tracing_TraceWriter_hpp
38 #define anna_core_tracing_TraceWriter_hpp
42 #include <anna/core/mt/NRMutex.hpp>
43 #include <anna/core/tracing/Logger.hpp>
44 #include <anna/config/defines.hpp>
45 #include <anna/core/util/Microsecond.hpp>
50 class TraceWriterObserver;
56 class TraceWriter : public Logger::Writer {
58 static const int DefaultMaxKBSize = 4192 * 1024;
62 \warning Si se usa este constructor las trazas se escribiran en un fichero del directorio temporal
63 con el mismo nombre que el pid del proceso.
69 \param fileName Nombre del fichero de trazas.
70 \param maxSize Tama�o maximo del fichero de trazas expresando en bytes.
72 TraceWriter(const char* fileName, const int maxSize);
75 Devuelve el tama�o maximo del fichero de trazas.
76 \return El tama�o maximo del fichero de trazas.
78 int getMaxSize() const throw() { return a_maxSize; }
81 Metodo para cambiar dinamicamente la configuracion del grabador de trazas. Las seccciones
82 requeridas para el archivo de configuracion seran:
86 MaxFileSize = Kbytes maximo de los ficheros de trazas. Por defecto sera 4096 Kbytes
87 Level = Nivel de trazas con el que comenzar la configuracion. Por defecto sera Debug para las
88 versiones con depuracion y Error para las versiones definitivas.
89 Clean = Un 1 indica que los ficheros de trazas usados hasta el momento deben borrarse y un 0
90 indica que deben mantenerse. Por defecto sera 1.
93 Todos los parametros de la seccion \em Trace son opcionales.
95 \param fileName Nombre del fichero de trazas.
96 \param configuration Configuracion que contiene las variables y secciones necesarias
97 para establecer la configuracion desea del grabador de trazas.
99 \warning La configuracion debera estar correctamente cargada. Ver Configuration::load.
101 void setup(const char* fileName, const Configuration& configuration) throw();
104 Metodo para cambiar dinamicamente la configuracion del grabador de trazas.
106 \param fileName Nombre del fichero de trazas.
107 \param maxSize Tama�o maximo del fichero de trazas expresando en bytes. Un valor 0 mantiene
108 el valor del tama�o estahlecido hasta el momento.
109 \param clean \em true Indica que los ficheros de trazas usados hasta el momento deben
110 borrase, \em false indica que los ficheros de trazas deben mantenerse.
112 \warning Solo deberia llamarse una vez para cambiar de la configuracion por defecto a la
113 configuracion particular de cada proceso.
115 void setup(const char* fileName, const int maxSize = 0, const bool clean = true) throw();
118 Saca por pantalla la informacion relevente de este grabador de trazas.
120 void printResume() throw();
123 Conecta esta instancia con un TraceWriterObserver, lo que permitira que esta instancia
124 conozca cuando se abre o cierra el fichero asociado a la escritura de trazas.
126 Este metodo puede invocarse tantas veces como sea necesario con distintas instancias de observador.
128 \param observer Instancia del observador que recibira las notificaciones de apertura y
129 cierre del fichero usado para sacar las trazas.
131 \warning La instancia del TraceWriterObserver debera estar disponible durante toda
134 void attach(TraceWriterObserver* observer) throw();
137 typedef std::vector <TraceWriterObserver*>::iterator observer_iterator;
139 static const int ErrorStream = 2;
143 GuardNoLog(NRMutex& mutex) : a_mutex(mutex) { mutex.lock(); }
144 ~GuardNoLog() { a_mutex.unlock(); }
149 std::string a_outputFile;
150 std::string a_outputOldFile;
153 Microsecond a_lastTime;
156 std::vector <TraceWriterObserver*> a_observers;
159 observer_iterator observer_begin() throw() { return a_observers.begin(); }
160 observer_iterator observer_end() throw() { return a_observers.end(); }
162 static TraceWriterObserver* observer(observer_iterator& ii) throw() { return *ii; }
164 void initialize(const char*) throw() { printResume(); }
165 void do_write(int level, const char* text, ...) throw();
166 const char* getDate() throw();
167 int prepareOutput(const char* date) throw();