1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/core/RuntimeException.hpp>
13 #include <anna/core/DataBlock.hpp>
14 #include <anna/core/tracing/TraceLogger.hpp>
15 #include <anna/core/functions.hpp>
19 Logger::Writer* Logger::st_writer = NULL;
20 bool Logger::st_enabled(true);
23 pid_t Logger::st_pid(-1);
27 Logger::Level Logger::st_level(Logger::Debug);
29 Logger::Level Logger::st_level(Logger::Warning);
32 NRMutex Logger::st_mutex;
34 void Logger::initialize(const char* ident)
36 if(st_writer == NULL) {
37 st_writer = new TraceLogger;
38 st_writer->initialize(ident);
42 void Logger::initialize(const char* ident, Writer* writer)
44 if(st_writer == NULL && writer != NULL) {
45 writer->initialize(ident);
50 void Logger::showPID(const bool show)
63 void Logger::write(const Level level, const char* text, const char* fromFile, const int fromLine)
67 if(isActive(level) && st_writer != NULL) {
69 st_writer->do_write(level, "%s (%d) | %s", fromFile, fromLine, text);
71 st_writer->do_write(level, "%s (%d) | pid: %d | %s", fromFile, fromLine, st_pid, text);
76 if(isActive(level) && st_writer != NULL)
77 st_writer->do_write(level, "%s (%d) | thr: 0x%x | %s", fromFile, fromLine, (unsigned int) pthread_self(), text);
82 void Logger::write(const Level level, const char* text, const char* value, const char* fromFile, const int fromLine)
86 if(isActive(level) && st_writer != NULL) {
88 st_writer->do_write(level, "%s (%d) | %s | %s", fromFile, fromLine, text, value);
90 st_writer->do_write(level, "%s (%d) | pid: %d | %s | %s", fromFile, fromLine, st_pid, text, value);
95 if(isActive(level) && st_writer != NULL)
96 st_writer->do_write(level, "%s (%d) | thr: 0x%x | %s | %s", fromFile, fromLine, (unsigned int) pthread_self(), text, value);
101 void Logger::write(const Level level, const char* text, const int value, const char* fromFile, const int fromLine)
105 if(isActive(level) && st_writer != NULL) {
107 st_writer->do_write(level, "%s (%d) | %s | %d (%x)", fromFile, fromLine, text, value, value);
109 st_writer->do_write(level, "%s (%d) | pid: %d | %s | %d (%x)", fromFile, fromLine, st_pid, text, value, value);
114 if(isActive(level) && st_writer != NULL)
115 st_writer->do_write(level, "%s (%d) | thr: 0x%x | %s | %d (%x)", fromFile, fromLine, (unsigned int) pthread_self(), text, value, value);
120 void Logger::write(const Level level, const char* text, const DataBlock& value, const char* fromFile, const int fromLine)
122 if(isActive(level) && st_writer != NULL) {
126 st_writer->do_write(level, "%s (%d) | %s | Data block: %s", fromFile, fromLine, text, functions::asString(value).c_str());
128 st_writer->do_write(level, "%s (%d) | %s | Data block: <fault!>", fromFile, fromLine, text);
134 st_writer->do_write(level, "%s (%d) | thr: 0x%x | %s | Data block: %s", fromFile, fromLine, (unsigned int) pthread_self(), text, functions::asString(value).c_str());
136 st_writer->do_write(level, "%s (%d) | thr: 0x%x | %s | Data block: <fault!>", fromFile, fromLine, (unsigned int) pthread_self(), text);
143 void Logger::disable()
144 throw(RuntimeException) {
150 void Logger::enable()
151 throw(RuntimeException) {
157 Logger::Level Logger::asLevel(const char* stringLevel)
158 throw(RuntimeException) {
160 const char* stringLevel;
163 { "emergency", Emergency }, { "alert", Alert }, { "critical", Critical }, { "error", Error },
164 { "warning", Warning }, { "notice", Notice }, { "information", Information }, { "debug", Debug },
165 { "local0", Local0 }, { "local1", Local1 }, { "local2", Local2 }, { "local3", Local3 },
166 { "local4", Local4 }, { "local5", Local5 }, { "local6", Local6 }, { "local7", Local7 },
167 { NULL, (Level) - 1 }
171 while(values [i].stringLevel != NULL) {
172 if(strcasecmp(stringLevel, values [i].stringLevel) == 0)
178 if(values [i].stringLevel == NULL) {
179 std::string msg = "Level not registered: '";
181 msg += "'. Possible values: ";
183 for(i = 0; values [i].stringLevel != NULL; i ++) {
184 msg += values [i].stringLevel;
188 throw RuntimeException(msg, ANNA_FILE_LOCATION);
191 return values [i].level;
194 const char* Logger::asString(const Level level)
196 static const char* levels [] = {
197 "Emergency", "Alert", "Critical", "Error", "Warning", "Notice", "Information", "Debug"
200 const char* stringLevel;
203 { "Local0", Local0 }, { "Local1", Local1 }, { "Local2", Local2 }, { "Local3", Local3 },
204 { "Local4", Local4 }, { "Local5", Local5 }, { "Local6", Local6 }, { "Local7", Local7 },
205 { NULL, (Level) - 1 }
207 const char* result = NULL;
209 if(level >= Emergency && level <= Debug)
210 result = levels [level];
212 for(int i = 0; values [i].stringLevel != NULL; i ++) {
213 if(level == values [i].level) {
214 result = values [i].stringLevel;
226 Logger::Writer::Writer(const int bufferSize) {
228 a_dataBlock = new DataBlock(true);
229 a_dataBlock->allocate(bufferSize);
230 } catch(Exception&) {
234 Logger::Writer::Writer() {
236 a_dataBlock = new DataBlock(true);
237 a_dataBlock->allocate(1024);
238 } catch(Exception&) {
242 Logger::Writer::~Writer() {
243 Logger::st_writer = NULL;