CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
exec_utils.cpp
Go to the documentation of this file.
1
6#include "exec_utils.h"
7#include <locale>
8#include <iomanip>
9#include <chrono>
10#include <set>
11
12CCSDS::ResultBool parseArguments(const int argc, char *argv[],
13 std::unordered_map<std::string, std::string> &allowedMap,
14 std::unordered_map<std::string, std::string> &outArgs)
15{
16 const std::set<std::string> booleanArgs{"verbose", "help"};
17 std::set<std::string> allowedKeys;
18 std::set<std::string> allowedShortKeys;
19 for (const auto& [k, v] : allowedMap) {
20 allowedShortKeys.insert(k);
21 allowedKeys.insert(v);
22 }
23 for (int i = 1; i < argc; ++i) {
24 std::string current = argv[i];
25
26 // Check if this is a key
27 if (current.rfind("--", 0) == 0 || current.rfind('-', 0) == 0) {
28 std::string key;
29 if (current.rfind("--", 0) == 0){
30 key = current.substr(2);
31 }else {
32 std::string sKey = current.substr(1);
33 RET_IF_ERR_MSG(allowedShortKeys.find(sKey) == allowedShortKeys.end(), static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument: -" + sKey);
34 key = allowedMap.at(sKey);
35 }
36 RET_IF_ERR_MSG(allowedKeys.find(key) == allowedKeys.end(), static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument: --" + key);
37
38 if (booleanArgs.find(key) != booleanArgs.end()) {
39 outArgs[key] = "true";
40 }else {
41 RET_IF_ERR_MSG(i + 1 >= argc, static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Missing value for argument: --" + key);
42 const std::string value = argv[++i];
43 outArgs[key] = value;
44 }
45 }else {
46 return CCSDS::Error{ static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument:" + std::string(argv[i]) };
47 }
48 }
49 return true;
50}
51
52void customConsole(const std::string& appName, const std::string& message, const std::string& logLevel ) {
53 // Get current timestamp with high precision
54 const auto now = std::chrono::high_resolution_clock::now();
55 const auto duration = now.time_since_epoch();
56 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration);
57
58 // Get the current system time for date and time portion
59 const std::time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
60 const std::tm* tm = std::localtime(&currentTime);
61
62 // Format timestamp (YYYY-MM-DD HH:MM:SS)
63 std::ostringstream timestampStream;
64 timestampStream << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
65
66 // Output log with timestamp, application name, log level, and microseconds
67 std::cout << "[" << timestampStream.str() << "."
68 << std::setw(6) << std::setfill('0') << microseconds.count() % 1000000 // last 6 digits (microseconds)
69 << "] [" << appName << "] "
70 << "[" << logLevel << "] : "
71 << message << std::endl;
72}
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
Represents an error with both an error code and a message.
Definition CCSDSResult.h:43
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:81
void customConsole(const std::string &appName, const std::string &message, const std::string &logLevel)
CCSDS::ResultBool parseArguments(const int argc, char *argv[], std::unordered_map< std::string, std::string > &allowedMap, std::unordered_map< std::string, std::string > &outArgs)
This is the source file that holds the execution logic of ccsds_encoder binary file.
@ ARG_PARSE_ERROR
Error Parsing argument.
Definition exec_utils.h:10
ErrorCode
Defines various error codes used in CCSDS packet handling.
Definition CCSDSResult.h:19