BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
Context.cxx
Go to the documentation of this file.
1/*
2 * Context.cxx
3 * ers
4 *
5 * Created by Matthias Wiesmann on 26.11.04.
6 * Copyright 2004 CERN. All rights reserved.
7 *
8 */
9#include <iostream>
10#include <sstream>
11#include "ers/Context.h"
12#include <cstdlib>
13
14#if defined(__GNU_LIBRARY__)
15#include <execinfo.h>
16#endif
17
19std::string ers::Context::s_host_type ;
20
21std::vector<std::string> ers::Context::default_qualifiers ;
22
23/** Returns the empty instance
24 * \return a pointer to the empty instance.
25 */
26
28 if (! empty_instance) {
29 std::string empty = "" ;
31 } // if
32 return empty_instance ;
33} // empty
34
35/** Tries to gues the host name
36* \return a string describing the host, in the form <var>architecture</var>-<var>operating-system</var>
37*/
38
39std::string & ers::Context::host_type() {
40 if (s_host_type.empty()) build_host_type() ;
41 return s_host_type ;
42} // plateform
43
44/** Gives the current debug level
45 * \return debug level, or -1 if it cannot be determined
46 */
47
49#if defined(DEBUG_LEVEL)
50 return DEBUG_LEVEL ;
51#else
52 return -1 ;
53#endif
54} // debug_level
55
56void ers::Context::add_qualifier(const std::string &qualif) {
57 default_qualifiers.push_back(qualif) ;
58} // add_qualifier
59
60/** Constructor - defines an Issue context.
61 * This constructor should generally not be called directly, instead use the macro \c ERS_HERE.
62 * \param filename name of the source code file
63 * \param line_number line_number in the source code
64 * \param function_name name of the function - either pretty printed or not
65 * \param compiler_name name of the compiler
66 * \param compiler_version version of the compiler
67 * \param compilation_time time of the compilation
68 * \param compilation_date date of the compilation
69 */
70
71ers::Context::Context(const std::string &filename, int line_number, const std::string &function_name,
72 const std::string &compiler_name, const std::string &compiler_version,
73 const std::string &compilation_time, const std::string &compilation_date,
74 const std::string &package_name) {
75 this->m_file_name = filename ;
76 this->m_line_number = line_number ;
77 this->m_function_name = function_name ;
78 this->m_compiler_name = compiler_name ;
79 this->m_compiler_version = compiler_version ;
80 this->m_compilation_date = compilation_date ;
81 this->m_compilation_time = compilation_time ;
82 this->m_package_name = package_name ;
83#if defined(__GNU_LIBRARY__)
84 void * array[128] ;
85 const int n_size = backtrace (array,128) ;
86 char ** symbols = backtrace_symbols(array, n_size);
87 for (int i = 1; i < n_size; i++) { // we start at 1, current position is noise
88 this->m_stack_frames.push_back(symbols[i]);
89 } // for
90 free(symbols);
91#endif
92} // Context
93
94/** The source code file name
95 * \return path of the source file
96 */
97
98const std::string & ers::Context::file() const throw() {
99 return m_file_name ;
100} // file
101
102/** The line number in the source code
103 * \return line number
104 */
105
106int ers::Context::line() const throw() {
107 return m_line_number ;
108} // line
109
110/** Name of the function containing the context
111 * \return name of the function
112 */
113
114const std::string & ers::Context::function() const throw() {
115 return m_function_name ;
116} // function
117
118/** Pretty printed code position
119 * format: file_name:line_number (function_name)
120 * \return reference to string containing format
121 * \note the file name is truncated from the last slash
122 */
123
124const std::string & ers::Context::position() const {
125 if (m_position.empty()) {
126 std::ostringstream position_s ;
127 if (! m_package_name.empty()) {
128 position_s << m_package_name << ":" ;
129 }
130 if (! m_file_name.empty()) {
131 const std::string::size_type p = m_file_name.rfind('/') ;
132 if (std::string::npos == p) {
133 position_s << m_file_name ;
134 } else {
135 position_s << (m_file_name.substr(p+1)) ;
136 } //
137 position_s << ":" << m_line_number << " ";
138 }
139 if (! m_function_name.empty()) {
140 position_s << "(" << m_function_name << ")" ;
141 } // if
142 m_position = position_s.str();
143 } // cached version not calculated
144 return m_position ;
145} // position
146
147/** Pretty printed compiler name
148 * \return reference to string containing format
149 */
150
151const std::string & ers::Context::compiler() const {
152 if (m_compiler.empty()) {
153 if (! m_compiler_name.empty()) {
154 std::ostringstream compiler_s ;
155 compiler_s << m_compiler_name << " " << m_compiler_version ;
156 m_compiler = compiler_s.str();
157 } else {
158 m_compiler = "unknown" ;
159 }
160 } // build cache
161 return m_compiler ;
162} // compiler
163
164/** Pretty printed compilation time description
165 * \return reference to string containing description
166 */
167
168const std::string & ers::Context::compilation() const {
169 if (m_compilation.empty()) {
170 std::ostringstream compilation_s ;
171 if (! m_compilation_time.empty()) {
172 compilation_s << m_compilation_time << " " ;
173 } // compilation time
174 if (! m_compilation_date.empty()) {
175 compilation_s << m_compilation_date ;
176 } // compilation date
177 m_compilation = compilation_s.str();
178 } // if
179 return m_compilation ;
180} // compilation
181
182/** \return package name
183 */
184
185const std::string & ers::Context::package_name() const throw() {
186 return m_package_name ;
187} // package_name
188
190 std::ostringstream plateform_s ;
191#if defined(__linux__)
192 plateform_s << "linux" ;
193#endif
194#if defined(__OpenBSD__)
195 plateform_s << "OpenBSD" ;
196#endif
197#if defined(__FreeBSD__)
198 plateform_s << "FreeBSD" ;
199#endif
200#if defined(__APPLE__) && defined(__MACH__)
201 plateform_s << "Darwin" ;
202#endif
203#if defined(__SOLARIS__)
204 plateform_s << "Solaris" ;
205#endif
206 plateform_s << "/" ;
207#if defined(__POWERPC__) || defined(__ppc__ ) || defined( __PPC__ ) || defined( powerpc ) || defined( ppc )
208 plateform_s << "PowerPC" ;
209#endif
210#if defined(__i386__) || defined(__INTEL__) || defined( intel ) || defined( _M_IX86 )
211 plateform_s << "i386" ;
212#endif
213#if defined(sparc) || defined(__sparc)
214 plateform_s << "Sparc" ;
215#endif
216 s_host_type= plateform_s.str();
217} // build_host_type
218
219int ers::Context::stack_frames() const throw() {
220 return m_stack_frames.size();
221} // stack_frames
222
223const std::string & ers::Context::stack_frame(int i) const {
224 return m_stack_frames[i] ;
225} // stack_frame
226
227/** Returns the set of qualifiers associated with context
228 * At the moment, this includes the default qualifiers plus the package
229 * \return array of strings represnting the qualifiers
230 */
231
232std::vector<std::string> ers::Context::qualifiers() const throw() {
233 std::vector<std::string> qualif = default_qualifiers ;
234 if (! m_package_name.empty()) {
235 qualif.push_back(m_package_name) ;
236 } // if
237 return qualif ;
238} // qualifiers
239
240
#define DEBUG_LEVEL
Source context for Issue.
Definition: Context.h:42
static std::string s_host_type
Definition: Context.h:45
static const Context * empty()
Definition: Context.cxx:27
const std::string & package_name() const
Definition: Context.cxx:185
const std::string & stack_frame(int i) const
Definition: Context.cxx:223
int line() const
Definition: Context.cxx:106
std::vector< std::string > qualifiers() const
Definition: Context.cxx:232
static int debug_level()
Definition: Context.cxx:48
const std::string & compiler() const
Definition: Context.cxx:151
static void build_host_type()
Definition: Context.cxx:189
static std::vector< std::string > default_qualifiers
Definition: Context.h:46
const std::string & compilation() const
Definition: Context.cxx:168
static void add_qualifier(const std::string &qualif)
Definition: Context.cxx:56
const std::string & position() const
Definition: Context.cxx:124
static std::string & host_type()
type of target host
Definition: Context.cxx:39
int stack_frames() const
Definition: Context.cxx:219
Context(const std::string &filename, int line_number, const std::string &function_name, const std::string &compiler_name, const std::string &compiler_version, const std::string &compilation_time, const std::string &compilation_date, const std::string &package)
Definition: Context.cxx:71
const std::string & file() const
Definition: Context.cxx:98
const std::string & function() const
Definition: Context.cxx:114
static Context * empty_instance
Definition: Context.h:44