Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
40namespace goby
41{
42namespace 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
53inline 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__
62inline uint64_t gettid()
63{
64 uint64_t tid;
65 pthread_threadid_np(NULL, &tid);
66 return tid;
67}
68#elif defined SYS_gettid
69inline 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
77inline 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>());
85 boost::trim(hostname);
86 return hostname;
87 }
88 else
89 {
90 throw(goby::Exception("Could not open /etc/hostname"));
91 }
92};
93
94// hostname plus process ID
95inline 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
104inline 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
simple exception class for goby applications
Definition exception.h:35
const std::string & Layer_Name(T enum_t_value)
Definition layer.pb.h:69
std::string full_process_and_thread_id(std::thread::id i=std::this_thread::get_id())
Definition common.h:104
std::string hostname()
Definition common.h:77
std::string to_string(goby::middleware::protobuf::Layer layer)
Definition common.h:44
std::string full_process_id()
Definition common.h:95
std::string thread_id(std::thread::id i=std::this_thread::get_id())
Definition common.h:53
The global namespace for the Goby project.