DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Context.cpp
Go to the documentation of this file.
1/*
2 * Context.cxx
3 * ers
4 *
5 * Created by Serguei Kolos on 26.11.05.
6 * Copyright 2004 CERN. All rights reserved.
7 *
8 */
9#include <string.h>
10#include <cxxabi.h>
11#include <sys/types.h>
12#include <pwd.h>
13#include <unistd.h>
14
15#include <iostream>
16#include <sstream>
17
18#ifndef __rtems__
19#include <execinfo.h>
20#else
21char** backtrace_symbols (void ** , int size) {
22 return 0;
23}
24#endif
25
26#include <ers/Context.hpp>
27#include <ers/Configuration.hpp>
28
29namespace
30{
31 std::string
32 demangle( char * mangled )
33 {
34 int status;
35 char * function_begin = ::strchr( mangled, '(' );
36 if ( function_begin ) {
37 char * function_end = ::strchr( ++function_begin, '+' );
38 if ( function_end && function_end != function_begin)
39 {
40 std::string fname(function_begin, function_end - function_begin);
41 char * name = abi::__cxa_demangle( fname.c_str(), 0, 0, &status );
42
43 if (!name) {
44 return std::string( mangled );
45 }
46
47 std::ostringstream out;
48 out << std::string(mangled, function_begin - mangled) << name << function_end;
49 free( name );
50 return out.str();
51 }
52 }
53 return std::string( mangled );
54 }
55
56 void
57 print_function( std::ostream & out, const char * function, int verbosity )
58 {
59 if ( verbosity )
60 {
61 out << function;
62 return;
63 }
64
65 const char * end = strchr( function, '(' );
66 if ( end )
67 {
68 const char * beg = end;
69 while ( beg > function ) {
70 if ( *(beg-1) == ' ' ) {
71 break;
72 }
73 --beg;
74 }
75 out.write( beg, end - beg );
76 out << "(...)";
77 } else {
78 out << function;
79 }
80 }
81}
82
83std::vector<std::string>
85{
86 std::vector<std::string> stack;
87 stack.reserve( stack_size() );
88 char ** symbols = backtrace_symbols( (void**)stack_symbols(), stack_size() );
89
90 if (symbols) {
91 for (int i = 1; i < stack_size(); i++) {
92 stack.push_back( demangle(symbols[i]) );
93 }
94 free(symbols);
95 }
96
97 return stack;
98}
99
104std::string
105ers::Context::position( int verbosity ) const
106{
107 std::ostringstream out;
108 print_function( out, function_name(), verbosity );
109 out << " at ";
110
111 const char * file = file_name();
112 if ( file[0] == '.'
113 && file[1] == '.'
114 && file[2] == '/' ) // file name starts with "../"
115 {
116 out << package_name() << (file + 2);
117 } else {
118 out << file;
119 }
120 out << ":" << line_number();
121 return out.str();
122}
123
124
virtual void *const * stack_symbols() const =0
virtual int line_number() const =0
virtual int stack_size() const =0
std::string position(int verbosity=ers::Configuration::instance().verbosity_level()) const
Definition Context.cpp:105
virtual const char * package_name() const =0
virtual const char * file_name() const =0
std::vector< std::string > stack() const
Definition Context.cpp:84
virtual const char * function_name() const =0
FELIX Initialization std::string initerror FELIX queue timed out