|
| 1 | +// <copyright file="dump_sensors.cpp" company="Visualisierungsinstitut der Universität Stuttgart"> |
| 2 | +// Copyright © 2023 Visualisierungsinstitut der Universität Stuttgart. Alle Rechte vorbehalten. |
| 3 | +// </copyright> |
| 4 | +// <author>Christoph Müller</author> |
| 5 | + |
| 6 | +#include "power_overwhelming/collector.h" |
| 7 | +#include "power_overwhelming/dump_sensors.h" |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <iostream> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#if defined(_WIN32) |
| 14 | +#include <Windows.h> |
| 15 | +#include <tchar.h> |
| 16 | +#endif /* defined(_WIN32) */ |
| 17 | + |
| 18 | +#if !defined(_tmain) |
| 19 | +#define _tmain main |
| 20 | +#define TCHAR char |
| 21 | +#define _T(x) (x) |
| 22 | +#endif /* !defined(_tmain) */ |
| 23 | + |
| 24 | + |
| 25 | +/// <summary> |
| 26 | +/// Entry point of the dump_sensors application, which dumps the definitions of |
| 27 | +/// all sensors into the user-provided file. |
| 28 | +/// </summary> |
| 29 | +/// <param name="argc"></param> |
| 30 | +/// <param name="argv"></param> |
| 31 | +/// <returns></returns> |
| 32 | +int _tmain(const int argc, const TCHAR **argv) { |
| 33 | + using namespace visus::power_overwhelming; |
| 34 | + |
| 35 | + std::wcout << L"dump_sensors" << std::endl; |
| 36 | + std::wcout << L"© 2023 Visualisierungsinstitut der Universität Stuttgart." |
| 37 | + << std::endl << L"All rights reserved." |
| 38 | + << std::endl << std::endl; |
| 39 | + |
| 40 | + const std::vector<std::basic_string<TCHAR>> cmd_line(argv, argv + argc); |
| 41 | + auto show_help = (argc < 2); |
| 42 | + |
| 43 | + const auto collector = std::find(cmd_line.begin(), cmd_line.end(), |
| 44 | + _T("--collector")); |
| 45 | + if (collector != cmd_line.end()) { |
| 46 | + // If the --collector option is specified, the file must be after it. |
| 47 | + show_help = ((collector + 1) == cmd_line.end()); |
| 48 | + } |
| 49 | + |
| 50 | + if (show_help) { |
| 51 | + // Input is wrong, so show the help. |
| 52 | + std::wcout << L"Dumps the definition of all sensors that are currently " |
| 53 | + << L"available on this machine " << std::endl |
| 54 | + << L"into a JSON file." << std::endl << std::endl; |
| 55 | + std::wcout << "Usage: dump_sensors [--collector] <output path>" |
| 56 | + << std::endl; |
| 57 | + return -2; |
| 58 | + } |
| 59 | + |
| 60 | + // Can go on with the real thing, which is just calling into the library to |
| 61 | + // save the sensor definitions. |
| 62 | + try { |
| 63 | + if (collector != cmd_line.end()) { |
| 64 | + const auto path0 = *(collector + 1); |
| 65 | + const auto path = convert_string<wchar_t>(path0); |
| 66 | + collector::make_configuration_template(path.c_str()); |
| 67 | + std::wcout << L"Collector configuration template dumped to \"" |
| 68 | + << path << L"\"." << std::endl; |
| 69 | + |
| 70 | + } else { |
| 71 | + const auto path = cmd_line[1]; |
| 72 | + const auto cnt = dump_sensors(path); |
| 73 | + std::wcout << cnt << ((cnt == 1) ? L" sensor" : L" sensors") |
| 74 | + << L" dumped to \"" << path.c_str() << L"\"." << std::endl; |
| 75 | + } |
| 76 | + |
| 77 | + return 0; |
| 78 | + } catch (std::exception& ex) { |
| 79 | + std::cout << ex.what() << std::endl; |
| 80 | + return -1; |
| 81 | + } |
| 82 | +} |
0 commit comments