Line data Source code
1 : /*
2 : * LocalContext.cxx
3 : * ers
4 : *
5 : * Created by Serguei Kolos on 26.11.05.
6 : * Copyright 2004 CERN. All rights reserved.
7 : *
8 : */
9 : #include <sys/types.h>
10 : #include <pwd.h>
11 : #include <unistd.h>
12 : #include <stdlib.h>
13 :
14 : #include <iterator>
15 :
16 : #include <ers/LocalContext.hpp>
17 :
18 : #if !defined(__APPLE__) && !defined(__rtems__)
19 : #include <sys/syscall.h>
20 : #include <execinfo.h>
21 : #else
22 : int backtrace(void**, int) {
23 : return 0;
24 : }
25 : #endif
26 :
27 3297 : pid_t gettid() {
28 : #if !defined(__APPLE__) && !defined(__rtems__)
29 3297 : return syscall( SYS_gettid );
30 : #else
31 : return 0;
32 : #endif
33 : }
34 :
35 :
36 : namespace
37 : {
38 73 : const char * get_cwd( )
39 : {
40 73 : static std::string buf;
41 73 : if ( buf.empty() )
42 : {
43 73 : char tmp[1024];
44 73 : if ( ::getcwd( tmp, sizeof( tmp ) ) )
45 73 : buf = tmp;
46 : }
47 73 : return buf.c_str();
48 : }
49 :
50 73 : const char * get_user_name()
51 : {
52 73 : static std::string buf;
53 73 : if ( buf.empty() )
54 : {
55 73 : struct passwd * psw = ::getpwuid( geteuid() );
56 73 : if ( psw )
57 73 : buf = psw->pw_name;
58 : }
59 73 : return buf.c_str();
60 : }
61 :
62 73 : const char * get_host_name()
63 : {
64 73 : static std::string buf;
65 73 : if ( buf.empty() )
66 : {
67 73 : char tmp[1024];
68 73 : if ( !::gethostname( tmp, sizeof( tmp ) ) )
69 73 : buf = tmp;
70 : }
71 73 : return buf.c_str();
72 : }
73 : }
74 :
75 :
76 3297 : ers::LocalContext::LocalContext(
77 : const char * package_name,
78 : const char * filename,
79 : int line_number,
80 : const char * function_name,
81 3297 : bool debug)
82 3297 : : m_package_name( package_name ),
83 3297 : m_file_name( filename ),
84 3297 : m_function_name( function_name ),
85 3297 : m_line_number( line_number ),
86 6594 : m_thread_id( gettid() ),
87 3297 : m_stack_size( debug ? backtrace( m_stack, std::size(m_stack) ) : 0)
88 3297 : { ; }
89 :
90 : const char *
91 0 : ers::LocalContext::application_name() const
92 : {
93 0 : static int pid = ::getpid();
94 0 : static const char * env = ::getenv( "DUNEDAQ_APPLICATION_NAME" ) ? ::getenv( "DUNEDAQ_APPLICATION_NAME" ) : "Undefined";
95 :
96 0 : if (pid != ::getpid()) {
97 0 : pid = ::getpid();
98 0 : env = ::getenv( "DUNEDAQ_APPLICATION_NAME" ) ? ::getenv( "DUNEDAQ_APPLICATION_NAME" ) : "Undefined";
99 : }
100 0 : return env;
101 : }
102 :
103 : const ers::LocalProcessContext ers::LocalContext::c_process( get_host_name(),
104 : get_cwd(),
105 : geteuid(),
106 : get_user_name());
|