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