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 std::int32_t argc, char *argv[],
13 std::unordered_map<std::string, std::string> &allowedMap,
14 std::unordered_map<std::string, std::string> &outArgs, const std::set<std::string> &booleanArgs)
15{
16 std::set<std::string> allowedKeys;
17 std::set<std::string> allowedShortKeys;
18 for (const auto& [k, v] : allowedMap) {
19 allowedShortKeys.insert(k);
20 allowedKeys.insert(v);
21 }
22 for ( std::int32_t i = 1; i < argc; ++i) {
23 std::string current = argv[i];
24
25 // Check if this is a key
26 if (current.rfind("--", 0) == 0 || current.rfind('-', 0) == 0) {
27 std::string key;
28 if (current.rfind("--", 0) == 0){
29 key = current.substr(2);
30 }else {
31 std::string sKey = current.substr(1);
32 RET_IF_ERR_MSG(allowedShortKeys.find(sKey) == allowedShortKeys.end(), static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument: -" + sKey);
33 key = allowedMap.at(sKey);
34 }
35 RET_IF_ERR_MSG(allowedKeys.find(key) == allowedKeys.end(), static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument: --" + key);
36
37 if (booleanArgs.find(key) != booleanArgs.end()) {
38 outArgs[key] = "true";
39 }else {
40 RET_IF_ERR_MSG(i + 1 >= argc, static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Missing value for argument: --" + key);
41 const std::string value = argv[++i];
42 outArgs[key] = value;
43 }
44 }else {
45 return CCSDS::Error{ static_cast<CCSDS::ErrorCode>(ARG_PARSE_ERROR), "Unknown argument:" + std::string(argv[i]) };
46 }
47 }
48 return true;
49}
50
51void customConsole(const std::string& appName, const std::string& message, const std::string& logLevel ) {
52 // Get current timestamp with high precision
53 const auto now = std::chrono::high_resolution_clock::now();
54 const auto duration = now.time_since_epoch();
55 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration);
56
57 // Get the current system time for date and time portion
58 const std::time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
59 const std::tm* tm = std::localtime(&currentTime);
60
61 // Format timestamp (YYYY-MM-DD HH:MM:SS)
62 std::ostringstream timestampStream;
63 timestampStream << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
64
65 // Output log with timestamp, application name, log level, and microseconds
66 std::cout << "[" << timestampStream.str() << "."
67 << std::setw(6) << std::setfill('0') << microseconds.count() % 1000000 // last 6 digits (microseconds)
68 << "] [" << appName << "] "
69 << "[" << logLevel << "] : "
70 << message << std::endl;
71}
#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:44
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:82
void customConsole(const std::string &appName, const std::string &message, const std::string &logLevel)
Prints log to console adding various information about.
CCSDS::ResultBool parseArguments(const std::int32_t argc, char *argv[], std::unordered_map< std::string, std::string > &allowedMap, std::unordered_map< std::string, std::string > &outArgs, const std::set< std::string > &booleanArgs)
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:12
ErrorCode
Defines various error codes used in CCSDS packet handling.
Definition CCSDSResult.h:20