sysinfo: add a trivial system stats dump function

This commit is contained in:
Danny Robson 2019-06-20 12:37:48 +10:00
parent 4d2be853b8
commit c79b0bae22
5 changed files with 148 additions and 0 deletions

View File

@ -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

View File

@ -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',

25
sysinfo.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#pragma once
#include <iosfwd>
#include <string>
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&);
}

44
sysinfo_posix.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#include "sysinfo.hpp"
#include "posix/except.hpp"
#include <ostream>
#include <pwd.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
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 << " }";
}

61
sysinfo_win32.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#include "sysinfo.hpp"
#include "win32/windows.hpp"
#include "win32/except.hpp"
#include <ostream>
///////////////////////////////////////////////////////////////////////////////
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 << " }";
}