diff --git a/CMakeLists.txt b/CMakeLists.txt index c3ace438..239b8bd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,13 @@ else () endif () +if (WIN32) + list (APPEND UTIL_FILES sysinfo_win32.cpp) +else () + list (APPEND UTIL_FILES sysinfo_posix.cpp) +endif () + + ############################################################################### # Common files list ( @@ -467,6 +474,7 @@ list ( stringid.hpp strongdef.cpp strongdef.hpp + sysinfo.hpp tap.cpp tap.hpp term.cpp diff --git a/meson.build b/meson.build index c6d5564a..3e4843d2 100644 --- a/meson.build +++ b/meson.build @@ -198,6 +198,15 @@ else endif +if host_machine.system() == 'linux' + sources += [ 'sysinfo_posix.cpp' ] +elif host_machine.system() == 'windows' + sources += [ 'sysinfo_win32.cpp' ] +else + error('Unsupported system for sysinfo') +endif + + ################################################################################# ### Common files sources += [ @@ -452,6 +461,7 @@ sources += [ 'stringid.hpp', 'strongdef.cpp', 'strongdef.hpp', + 'sysinfo.hpp', 'tap.cpp', 'tap.hpp', 'term.cpp', diff --git a/sysinfo.hpp b/sysinfo.hpp new file mode 100644 index 00000000..2051fd5c --- /dev/null +++ b/sysinfo.hpp @@ -0,0 +1,25 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019 Danny Robson + */ + +#pragma once + +#include +#include + +namespace cruft::util { + /// A tag structure that can be passed to an ostream which will print a + /// collection of system environment values in a JSONish format that + /// might be suitable for triaging bug reports. + /// + /// If you want something specific it would be better to avoid populating + /// this structure directly and instead write a query that exposes the + /// value directly. + struct sysinfo { }; + + std::ostream& operator<< (std::ostream &, sysinfo const&); +} diff --git a/sysinfo_posix.cpp b/sysinfo_posix.cpp new file mode 100644 index 00000000..a784c05d --- /dev/null +++ b/sysinfo_posix.cpp @@ -0,0 +1,44 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019 Danny Robson + */ + +#include "sysinfo.hpp" + +#include "posix/except.hpp" + +#include + +#include +#include +#include +#include + +using cruft::util::sysinfo; + + +/////////////////////////////////////////////////////////////////////////////// +std::ostream& +cruft::util::operator<< (std::ostream &os, sysinfo const &) +{ + os << "{ "; + + { + struct utsname name; + cruft::posix::error::try_code (uname (&name)); + + os << "os: { name: " << name.sysname << ", version: " << name.version + << " }, hostname: " << name.nodename; + } + + { + auto const uid = geteuid (); + auto const user = getpwuid (uid); + os << ", username: " << user->pw_name; + } + + return os << " }"; +} diff --git a/sysinfo_win32.cpp b/sysinfo_win32.cpp new file mode 100644 index 00000000..8f76a415 --- /dev/null +++ b/sysinfo_win32.cpp @@ -0,0 +1,61 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019 Danny Robson + */ + +#include "sysinfo.hpp" + +#include "win32/windows.hpp" +#include "win32/except.hpp" + +#include + + +/////////////////////////////////////////////////////////////////////////////// +std::ostream& +cruft::util::operator<< (std::ostream &os, sysinfo const &) +{ + os << "{ "; + + { + OSVERSIONINFO version; + if (!GetVersionExA (&version)) + win32::error::throw_code (); + + os << "os: { name: win32, " + << ", version: " << +version.dwMajorVersion << '.' + << +version.dwMinorVersion << '.' + << +version.dwPlatformId + << "}"; + } + + DWORD size = 0; + std::string name; + + { + if (!GetComputerNameExA (ComputerNamePhysicalNetBIOS, nullptr, &size)) + win32::error::throw_code (); + name.resize (size); + + if (!GetComputerNameExA (ComputerNamePhysicalNetBIOS, &name[0], &size)) + win32::error::throw_code (); + os << ", hostname: " << name; + } + + { + size = 0; + if (!GetUserNameA (nullptr, &size)) + win32::error::throw_code (); + name.resize (size); + + if (!GetUserNameA (&name[0], &size)) + win32::error::throw_code (); + + os << ", username: " << name; + } + + return os << " }"; +}