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