Remove dynamic exceptions
[anna.git] / source / core / tracing / TraceLogger.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <syslog.h>
12
13 #include <anna/core/tracing/Logger.hpp>
14 #include <anna/core/tracing/TraceLogger.hpp>
15 #include <anna/core/DataBlock.hpp>
16
17 using namespace anna;
18
19 void TraceLogger::initialize(const char* ident)
20 {
21   openlog(ident, LOG_PID | LOG_CONS | LOG_ODELAY | LOG_NOWAIT, LOG_LOCAL0);
22 }
23
24 void TraceLogger::do_write(int level, const char* message, ...)
25 {
26   va_list ap;
27   int size;
28   int nbytes;
29
30   try {
31     DataBlock& dataBlock(getDataBlock());
32     const char* data(dataBlock.getData());
33     size = dataBlock.getMaxSize();
34
35     while(true) {
36       va_start(ap, message);
37       nbytes = vsnprintf(const_cast <char*>(data), size, message, ap);
38       va_end(ap);
39
40       if(nbytes >= size) {
41         dataBlock.allocate(nbytes + 1);
42         data = dataBlock.getData();
43         size = dataBlock.getMaxSize();
44         continue;
45       }
46
47       break;
48     }
49
50     // void syslog(int priority, const char *format, ...);
51     syslog(level, "%s", data);
52   } catch(Exception&) {
53   }
54 }
55