DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
LocalContext.cpp
Go to the documentation of this file.
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
22int backtrace(void**, int) {
23 return 0;
24}
25#endif
26
27pid_t gettid() {
28#if !defined(__APPLE__) && !defined(__rtems__)
29 return syscall( SYS_gettid );
30#else
31 return 0;
32#endif
33}
34
35
36namespace
37{
38 const char * get_cwd( )
39 {
40 static std::string buf;
41 if ( buf.empty() )
42 {
43 char tmp[1024];
44 if ( ::getcwd( tmp, sizeof( tmp ) ) )
45 buf = tmp;
46 }
47 return buf.c_str();
48 }
49
50 const char * get_user_name()
51 {
52 static std::string buf;
53 if ( buf.empty() )
54 {
55 struct passwd * psw = ::getpwuid( geteuid() );
56 if ( psw )
57 buf = psw->pw_name;
58 }
59 return buf.c_str();
60 }
61
62 const char * get_host_name()
63 {
64 static std::string buf;
65 if ( buf.empty() )
66 {
67 char tmp[1024];
68 if ( !::gethostname( tmp, sizeof( tmp ) ) )
69 buf = tmp;
70 }
71 return buf.c_str();
72 }
73}
74
75
77 const char * package_name,
78 const char * filename,
79 int line_number,
80 const char * function_name,
81 bool debug)
82 : m_package_name( package_name ),
83 m_file_name( filename ),
84 m_function_name( function_name ),
85 m_line_number( line_number ),
86 m_thread_id( gettid() ),
87 m_stack_size( debug ? backtrace( m_stack, std::size(m_stack) ) : 0)
88{ ; }
89
90const char *
92{
93 static int pid = ::getpid();
94 static const char * env = ::getenv( "DUNEDAQ_APPLICATION_NAME" ) ? ::getenv( "DUNEDAQ_APPLICATION_NAME" ) : "Undefined";
95
96 if (pid != ::getpid()) {
97 pid = ::getpid();
98 env = ::getenv( "DUNEDAQ_APPLICATION_NAME" ) ? ::getenv( "DUNEDAQ_APPLICATION_NAME" ) : "Undefined";
99 }
100 return env;
101}
102
104 get_cwd(),
105 geteuid(),
106 get_user_name());
pid_t gettid()
const char * application_name() const
LocalContext(const char *package_name, const char *filename, int line_number, const char *function_name, bool debug=false)
static const LocalProcessContext c_process