Goby3  3.1.5a
2024.05.23
common.h
Go to the documentation of this file.
1 // Copyright 2009-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 // James D. Turner <james.turner@nrl.navy.mil>
8 //
9 //
10 // This file is part of the Goby Underwater Autonomy Project Libraries
11 // ("The Goby Libraries").
12 //
13 // The Goby Libraries are free software: you can redistribute them and/or modify
14 // them under the terms of the GNU Lesser General Public License as published by
15 // the Free Software Foundation, either version 2.1 of the License, or
16 // (at your option) any later version.
17 //
18 // The Goby Libraries are distributed in the hope that they will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef GOBY_MIDDLEWARE_COMMON_H
27 #define GOBY_MIDDLEWARE_COMMON_H
28 
29 #include <fstream>
30 #include <sstream>
31 #include <sys/syscall.h>
32 #include <thread>
33 #include <unistd.h>
34 
35 #include <boost/algorithm/string.hpp>
36 
37 #include "goby/exception.h"
39 
40 namespace goby
41 {
42 namespace middleware
43 {
45 {
46  const int underscore_pos = 5; // underscore position in: "LAYER_"
47  std::string name = goby::middleware::protobuf::Layer_Name(layer).substr(underscore_pos + 1);
48  boost::to_lower(name);
49  return name;
50 }
51 
52 // unique portable thread id string from hashing std::thread::id
53 inline std::string thread_id(std::thread::id i = std::this_thread::get_id())
54 {
55  std::stringstream ss;
56  ss << std::hex << std::hash<std::thread::id>{}(i);
57  return ss.str();
58 }
59 
60 // Linux/Apple thread ID, useful because you can access it outside the system
61 #if defined __APPLE__
62 inline uint64_t gettid()
63 {
64  uint64_t tid;
65  pthread_threadid_np(NULL, &tid);
66  return tid;
67 }
68 #elif defined SYS_gettid
69 inline pid_t gettid()
70 {
71  return syscall(SYS_gettid);
72 }
73 #else
74 #error "SYS_gettid unavailable on this system, and this is not an Apple system."
75 #endif
76 
77 inline std::string hostname()
78 {
79  // read from boot id as this is static while the machine is up and running
80  std::ifstream hs("/etc/hostname");
81  if (hs.is_open())
82  {
83  std::string hostname((std::istreambuf_iterator<char>(hs)),
84  std::istreambuf_iterator<char>());
86  return hostname;
87  }
88  else
89  {
90  throw(goby::Exception("Could not open /etc/hostname"));
91  }
92 };
93 
94 // hostname plus process ID
95 inline std::string full_process_id()
96 {
97  static const std::string host_id = hostname();
98  static const std::string pid = std::to_string(getpid());
99  static const std::string full_pid = host_id + std::string("-p") + pid;
100  return full_pid;
101 }
102 
103 // full_process_id + thread_id
104 inline std::string full_process_and_thread_id(std::thread::id i = std::this_thread::get_id())
105 {
106  return full_process_id() + "-t" + thread_id(i);
107 }
108 
109 } // namespace middleware
110 } // namespace goby
111 
112 #endif
goby::middleware::hostname
std::string hostname()
Definition: common.h:77
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::protobuf::Layer
Layer
Definition: layer.pb.h:61
goby::middleware::thread_id
std::string thread_id(std::thread::id i=std::this_thread::get_id())
Definition: common.h:53
goby::middleware::full_process_and_thread_id
std::string full_process_and_thread_id(std::thread::id i=std::this_thread::get_id())
Definition: common.h:104
goby::middleware::full_process_id
std::string full_process_id()
Definition: common.h:95
httplib::detail::to_lower
std::string to_lower(const char *beg, const char *end)
Definition: httplib.h:5045
to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:24301
goby::middleware::protobuf::Layer_Name
const ::std::string & Layer_Name(Layer value)
Definition: layer.pb.h:73
layer.pb.h
goby::Exception
simple exception class for goby applications
Definition: exception.h:34
httplib::detail::trim
std::pair< size_t, size_t > trim(const char *b, const char *e, size_t left, size_t right)
Definition: httplib.h:2472
exception.h
goby::middleware::to_string
std::string to_string(goby::middleware::protobuf::Layer layer)
Definition: common.h:44