38 #include <android/log.h>
43 std::string Logger::_default_tag =
"Core";
44 std::string Logger::_android_tag_prefix =
"IBR-DTN/";
45 Logger::LogWriter Logger::_logwriter;
48 : _level(level), _tag(tag), _debug_verbosity(debug_verbosity)
50 ::gettimeofday(&_logtime, NULL);
54 : _level(obj._level), _tag(obj._tag), _debug_verbosity(obj._debug_verbosity), _logtime(obj._logtime), _data(obj._data)
114 Logger::_logwriter.setVerbosity(verbosity);
117 void Logger::LogWriter::setVerbosity(
const int verbosity)
119 _verbosity = verbosity;
124 return Logger::_logwriter.getLogMask();
129 return Logger::_logwriter.getVerbosity();
132 unsigned char Logger::LogWriter::getLogMask()
const
134 return _global_logmask;
137 int Logger::LogWriter::getVerbosity()
const
142 void Logger::addStream(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
144 Logger::_logwriter.addStream(stream, logmask, options);
149 Logger::_logwriter.setLogfile(logfile, logmask, options);
152 void Logger::LogWriter::addStream(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
154 _global_logmask |= logmask;
155 _logger.push_back( Logger::LoggerOutput(stream, logmask, options) );
160 Logger::_logwriter.enableSyslog(name, option, facility, logmask);
165 Logger::_logwriter.log(*
this);
168 void Logger::LogWriter::enableSyslog(
const char *name,
int option,
int facility,
const unsigned char logmask)
170 #if ( defined HAVE_SYSLOG_H || defined ANDROID )
173 ::openlog(name, option, facility);
176 _syslog_mask = logmask;
177 _global_logmask |= logmask;
181 void Logger::LogWriter::flush(
const Logger &logger)
183 if (_verbosity >= logger._debug_verbosity)
185 for (std::list<LoggerOutput>::iterator iter = _logger.begin(); iter != _logger.end(); ++iter)
187 LoggerOutput &output = (*iter);
194 if (_logfile_output != NULL)
196 _logfile_output->log(logger);
200 #if ( defined HAVE_SYSLOG_H || defined ANDROID )
204 if (logger._level & _syslog_mask)
207 std::string log_tag = Logger::_default_tag;
208 if (logger._tag.length() > 0) {
209 log_tag = logger._tag;
213 log_tag = Logger::_android_tag_prefix + log_tag;
215 switch (logger._level)
218 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.str().c_str());
222 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.str().c_str());
226 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.str().c_str());
230 __android_log_print(ANDROID_LOG_ERROR, log_tag.c_str(),
"%s", logger.str().c_str());
234 __android_log_print(ANDROID_LOG_WARN, log_tag.c_str(),
"%s", logger.str().c_str());
238 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.str().c_str());
242 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.str().c_str());
246 __android_log_print(ANDROID_LOG_DEBUG, log_tag.c_str(),
"%s", logger.str().c_str());
250 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.str().c_str());
254 switch (logger._level)
257 ::syslog( LOG_EMERG,
"%s", logger.str().c_str() );
261 ::syslog( LOG_ALERT,
"%s", logger.str().c_str() );
265 ::syslog( LOG_CRIT,
"%s", logger.str().c_str() );
269 ::syslog( LOG_ERR,
"%s", logger.str().c_str() );
273 ::syslog( LOG_WARNING,
"%s", logger.str().c_str() );
277 ::syslog( LOG_NOTICE,
"%s", logger.str().c_str() );
281 ::syslog( LOG_INFO,
"%s", logger.str().c_str() );
285 ::syslog( LOG_DEBUG,
"%s", logger.str().c_str() );
289 ::syslog( LOG_NOTICE,
"%s", logger.str().c_str() );
299 void Logger::LoggerOutput::log(
const Logger &log)
301 if (_level & log._level)
303 std::list<std::string> prefixes;
306 if (_options & LOG_DATETIME)
309 std::string timestamp(asctime( localtime(&log._logtime.tv_sec) ));
310 timestamp.erase(std::remove(timestamp.begin(), timestamp.end(),
'\n'), timestamp.end());
311 prefixes.push_back(timestamp);
315 if (_options & LOG_TIMESTAMP)
317 std::stringstream ss;
319 ss << log._logtime.tv_sec <<
"." << std::setw(6) << log._logtime.tv_usec;
320 prefixes.push_back(ss.str());
323 if (_options & LOG_LEVEL)
329 prefixes.push_back(
"EMERGENCY");
333 prefixes.push_back(
"ALERT");
337 prefixes.push_back(
"CRTITICAL");
341 prefixes.push_back(
"ERROR");
345 prefixes.push_back(
"WARNING");
349 prefixes.push_back(
"NOTICE");
353 prefixes.push_back(
"INFO");
358 std::stringstream ss;
359 ss <<
"DEBUG." << log._debug_verbosity;
360 prefixes.push_back(ss.str());
369 if (_options & LOG_HOSTNAME)
371 std::vector<char> hostname_array(64);
372 if ( gethostname(&hostname_array[0], 64) == 0 )
374 std::string hostname(&hostname_array[0]);
375 prefixes.push_back(hostname);
379 if (_options & LOG_TAG)
381 if (log._tag.length() > 0) {
382 prefixes.push_back(log._tag);
384 prefixes.push_back(Logger::_default_tag);
389 for (std::list<std::string>::const_iterator iter = prefixes.begin(); iter != prefixes.end(); ++iter)
391 if (iter == prefixes.begin())
397 _stream <<
" " << (*iter);
401 if (!prefixes.empty())
406 _stream << log.str() << std::endl;
410 Logger::LoggerOutput::LoggerOutput(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
411 : _stream(stream), _level(logmask), _options(options)
415 Logger::LoggerOutput::~LoggerOutput()
421 Logger::_logwriter.enableAsync();
426 Logger::_logwriter.enableBuffer(size);
434 Logger::_logwriter.writeBuffer(stream, logmask, options);
437 void Logger::LogWriter::enableAsync()
449 Logger::_default_tag = tag;
464 Logger::LogWriter::LogWriter()
465 : _global_logmask(0), _verbosity(0), _syslog(0), _syslog_mask(0), _queue(50), _use_queue(false), _buffer_size(0), _buffer(NULL),
466 _logfile_output(NULL), _logfile_logmask(0), _logfile_options(0)
471 Logger::LogWriter::~LogWriter()
474 if (_use_queue) stop();
481 if (_buffer != NULL)
delete _buffer;
484 void Logger::LogWriter::enableBuffer(
size_t size)
488 _buffer =
new std::list<Logger>();
491 void Logger::LogWriter::setLogfile(
const ibrcommon::File &logfile,
const unsigned char logmask,
const unsigned char options)
494 if (_logfile_output != NULL)
497 delete _logfile_output;
498 _logfile_output = NULL;
501 _logfile_stream.close();
506 if (logmask == 0)
return;
509 _logfile_logmask = logmask;
510 _logfile_options = options;
511 _global_logmask |= logmask;
514 _logfile_stream.open(_logfile.getPath().c_str(), std::ios::out | std::ios::app);
517 if (_logfile_stream.good())
519 _logfile_output =
new LoggerOutput(_logfile_stream, _logfile_logmask, _logfile_options);
523 _logfile_stream.close();
527 void Logger::LogWriter::reload()
530 if (_logfile_output != NULL)
533 delete _logfile_output;
534 _logfile_output = NULL;
537 _logfile_stream.close();
540 _logfile_stream.open(_logfile.getPath().c_str(), std::ios::out | std::ios::app);
543 if (_logfile_stream.good())
545 _logfile_output =
new LoggerOutput(_logfile_stream, _logfile_logmask, _logfile_options);
549 _logfile_stream.close();
554 void Logger::LogWriter::writeBuffer(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
557 if (_buffer == NULL)
return;
559 LoggerOutput output(stream, logmask, options);
561 for (std::list<Logger>::const_iterator iter = _buffer->begin(); iter != _buffer->end(); ++iter)
563 const Logger &l = (*iter);
568 void Logger::LogWriter::log(Logger &logger)
580 void Logger::LogWriter::run() throw ()
585 Logger log = _queue.getnpop(
true);
592 _buffer->push_back(log);
593 while (_buffer->size() > _buffer_size)
595 _buffer->pop_front();
599 }
catch (
const std::exception&) {
608 Logger &log = q.
front();
609 try { flush(log); }
catch (
const std::exception&) {};
613 }
catch (
const std::exception&) {
619 void Logger::LogWriter::__cancellation() throw ()