00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <iostream>
00010 #include <sstream>
00011 #include "ers/Context.h"
00012 #include <cstdlib>
00013
00014 #if defined(__GNU_LIBRARY__)
00015 #include <execinfo.h>
00016 #endif
00017
00018 ers::Context *ers::Context::empty_instance = 0 ;
00019 std::string ers::Context::s_host_type ;
00020
00021 std::vector<std::string> ers::Context::default_qualifiers ;
00022
00027 const ers::Context *ers::Context::empty() {
00028 if (! empty_instance) {
00029 std::string empty = "" ;
00030 empty_instance = new ers::Context(empty,0,empty,empty,empty,empty,empty,empty) ;
00031 }
00032 return empty_instance ;
00033 }
00034
00039 std::string & ers::Context::host_type() {
00040 if (s_host_type.empty()) build_host_type() ;
00041 return s_host_type ;
00042 }
00043
00048 int ers::Context::debug_level() {
00049 #if defined(DEBUG_LEVEL)
00050 return DEBUG_LEVEL ;
00051 #else
00052 return -1 ;
00053 #endif
00054 }
00055
00056 void ers::Context::add_qualifier(const std::string &qualif) {
00057 default_qualifiers.push_back(qualif) ;
00058 }
00059
00071 ers::Context::Context(const std::string &filename, int line_number, const std::string &function_name,
00072 const std::string &compiler_name, const std::string &compiler_version,
00073 const std::string &compilation_time, const std::string &compilation_date,
00074 const std::string &package_name) {
00075 this->m_file_name = filename ;
00076 this->m_line_number = line_number ;
00077 this->m_function_name = function_name ;
00078 this->m_compiler_name = compiler_name ;
00079 this->m_compiler_version = compiler_version ;
00080 this->m_compilation_date = compilation_date ;
00081 this->m_compilation_time = compilation_time ;
00082 this->m_package_name = package_name ;
00083 #if defined(__GNU_LIBRARY__)
00084 void * array[128] ;
00085 const int n_size = backtrace (array,128) ;
00086 char ** symbols = backtrace_symbols(array, n_size);
00087 for (int i = 1; i < n_size; i++) {
00088 this->m_stack_frames.push_back(symbols[i]);
00089 }
00090 free(symbols);
00091 #endif
00092 }
00093
00098 const std::string & ers::Context::file() const throw() {
00099 return m_file_name ;
00100 }
00101
00106 int ers::Context::line() const throw() {
00107 return m_line_number ;
00108 }
00109
00114 const std::string & ers::Context::function() const throw() {
00115 return m_function_name ;
00116 }
00117
00124 const std::string & ers::Context::position() const {
00125 if (m_position.empty()) {
00126 std::ostringstream position_s ;
00127 if (! m_package_name.empty()) {
00128 position_s << m_package_name << ":" ;
00129 }
00130 if (! m_file_name.empty()) {
00131 const std::string::size_type p = m_file_name.rfind('/') ;
00132 if (std::string::npos == p) {
00133 position_s << m_file_name ;
00134 } else {
00135 position_s << (m_file_name.substr(p+1)) ;
00136 }
00137 position_s << ":" << m_line_number << " ";
00138 }
00139 if (! m_function_name.empty()) {
00140 position_s << "(" << m_function_name << ")" ;
00141 }
00142 m_position = position_s.str();
00143 }
00144 return m_position ;
00145 }
00146
00151 const std::string & ers::Context::compiler() const {
00152 if (m_compiler.empty()) {
00153 if (! m_compiler_name.empty()) {
00154 std::ostringstream compiler_s ;
00155 compiler_s << m_compiler_name << " " << m_compiler_version ;
00156 m_compiler = compiler_s.str();
00157 } else {
00158 m_compiler = "unknown" ;
00159 }
00160 }
00161 return m_compiler ;
00162 }
00163
00168 const std::string & ers::Context::compilation() const {
00169 if (m_compilation.empty()) {
00170 std::ostringstream compilation_s ;
00171 if (! m_compilation_time.empty()) {
00172 compilation_s << m_compilation_time << " " ;
00173 }
00174 if (! m_compilation_date.empty()) {
00175 compilation_s << m_compilation_date ;
00176 }
00177 m_compilation = compilation_s.str();
00178 }
00179 return m_compilation ;
00180 }
00181
00185 const std::string & ers::Context::package_name() const throw() {
00186 return m_package_name ;
00187 }
00188
00189 void ers::Context::build_host_type() {
00190 std::ostringstream plateform_s ;
00191 #if defined(__linux__)
00192 plateform_s << "linux" ;
00193 #endif
00194 #if defined(__OpenBSD__)
00195 plateform_s << "OpenBSD" ;
00196 #endif
00197 #if defined(__FreeBSD__)
00198 plateform_s << "FreeBSD" ;
00199 #endif
00200 #if defined(__APPLE__) && defined(__MACH__)
00201 plateform_s << "Darwin" ;
00202 #endif
00203 #if defined(__SOLARIS__)
00204 plateform_s << "Solaris" ;
00205 #endif
00206 plateform_s << "/" ;
00207 #if defined(__POWERPC__) || defined(__ppc__ ) || defined( __PPC__ ) || defined( powerpc ) || defined( ppc )
00208 plateform_s << "PowerPC" ;
00209 #endif
00210 #if defined(__i386__) || defined(__INTEL__) || defined( intel ) || defined( _M_IX86 )
00211 plateform_s << "i386" ;
00212 #endif
00213 #if defined(sparc) || defined(__sparc)
00214 plateform_s << "Sparc" ;
00215 #endif
00216 s_host_type= plateform_s.str();
00217 }
00218
00219 int ers::Context::stack_frames() const throw() {
00220 return m_stack_frames.size();
00221 }
00222
00223 const std::string & ers::Context::stack_frame(int i) const {
00224 return m_stack_frames[i] ;
00225 }
00226
00232 std::vector<std::string> ers::Context::qualifiers() const throw() {
00233 std::vector<std::string> qualif = default_qualifiers ;
00234 if (! m_package_name.empty()) {
00235 qualif.push_back(m_package_name) ;
00236 }
00237 return qualif ;
00238 }
00239
00240