Skip to content

Commit e01c8c6

Browse files
authored
ProjMgr: in silence mode only output to string buffers
1 parent 2e4575c commit e01c8c6

File tree

8 files changed

+175
-43
lines changed

8 files changed

+175
-43
lines changed

libs/rtefsutils/src/RteFsUtils.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,6 @@ fs::path RteFsUtils::AbsolutePath(const std::string& path) {
549549
return fs::absolute(path);
550550
}
551551
catch (fs::filesystem_error const& e) {
552-
std::cout << "runtime_error: " << e.what() << std::endl;
553-
}
554-
catch (...) {
555-
std::cout << "non-standard exception occurred" << std::endl;
556552
}
557553
}
558554
return fs::path();
@@ -886,7 +882,7 @@ bool RteFsUtils::FindFileWithPattern(const std::string& searchDir,
886882
}
887883
}
888884
catch (const std::exception& e) {
889-
std::cout << "error: " << e.what() << std::endl;
885+
// do nothing, anyway it is false
890886
}
891887
return false; // No matching file found
892888
}

libs/rteutils/include/CollectionUtils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ typename M::mapped_type get_or_default(const M& m, const typename M::key_type& k
3131
return v;
3232
}
3333

34+
/**
35+
* @brief Returns reference to value stored in a map for a given key or default value reference if no entry is found
36+
* @param M template parameter representing a map
37+
* @param m a key-value map
38+
* @param k key to search for
39+
* @param v default value
40+
* @return stored value if found, default value otherwise
41+
*/
42+
template<typename M>
43+
typename M::mapped_type const& get_or_default_const_ref(const M& m, const typename M::key_type& k, const typename M::mapped_type& v) {
44+
auto itr = m.find(k);
45+
if (itr != m.end()) {
46+
return itr->second;
47+
}
48+
return v;
49+
}
50+
3451
/**
3552
* @brief Returns value stored in a map for a given key or nullptr
3653
* @tparam M template parameter representing a map with the value of pointer type

tools/projmgr/include/ProjMgrLogger.h

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <map>
1111
#include <string>
12+
#include <sstream>
1213
#include <vector>
1314

1415
/**
@@ -58,37 +59,86 @@ class ProjMgrLogger {
5859
void Info(const std::string& msg, const std::string& context = std::string(),
5960
const std::string& file = std::string(), const int line = 0, const int column = 0);
6061

61-
/**
62+
/**
6263
* @brief print debug
6364
* @param message
6465
*/
6566
static void Debug(const std::string& msg);
6667

68+
/**
69+
* @brief returns reference to active output stream: cout (default) or string buffer (in silent mode)
70+
* @return reference to active output stream
71+
*/
72+
static std::ostream& out();
6773

74+
/**
75+
* @brief check if in quiet mode
76+
* @return true if quiet or silent
77+
*/
78+
static bool IsQuiet() { return m_quiet || m_silent; }
79+
80+
/**
81+
* @brief flag to suppress infos and warnings
82+
*/
6883
static bool m_quiet;
6984

85+
/**
86+
* @brief flag to suppress all output and redirect cout to string buffer
87+
*/
88+
static bool m_silent;
89+
90+
7091
/**
7192
* @brief get errors
7293
* @return reference to errors map
7394
*/
74-
std::map<std::string, std::vector<std::string>>& GetErrors() { return m_errors; }
95+
const std::map<std::string, std::vector<std::string>>& GetErrors() const { return m_errors; }
7596

7697
/**
7798
* @brief get warnings
7899
* @return reference to warnings map
79100
*/
80-
std::map<std::string, std::vector<std::string>>& GetWarns() { return m_warns; }
101+
const std::map<std::string, std::vector<std::string>>& GetWarns() const { return m_warns; }
81102

82103
/**
83104
* @brief get info messages
84105
* @return reference to info messages map
85106
*/
86-
std::map<std::string, std::vector<std::string>>& GetInfos() { return m_infos; }
107+
const std::map<std::string, std::vector<std::string>>& GetInfos() const { return m_infos; }
108+
109+
110+
/**
111+
* @brief get errors
112+
* @return reference to errors map
113+
*/
114+
const std::vector<std::string>& GetErrorsForContext(const std::string& context = std::string()) const;
115+
116+
/**
117+
* @brief get warnings
118+
* @return reference to warnings map
119+
*/
120+
const std::vector<std::string>& GetWarnsForContext(const std::string& context = std::string()) const;
121+
122+
/**
123+
* @brief get info messages
124+
* @return reference to info messages map
125+
*/
126+
const std::vector<std::string>& GetInfosForContext(const std::string& context = std::string()) const;
127+
128+
129+
/**
130+
* @brief get sting stream
131+
* @return reference to internal string stream
132+
*/
133+
const std::ostringstream& GetStringStream() const { return m_ss; }
87134

88135
protected:
89136
std::map<std::string, std::vector<std::string>> m_errors;
90137
std::map<std::string, std::vector<std::string>> m_warns;
91138
std::map<std::string, std::vector<std::string>> m_infos;
139+
140+
std::ostringstream m_ss; // stream for unsorted messages sent directly to output
141+
92142
};
93143

94144
#endif // PROJMGRLOGGER_H

tools/projmgr/src/ProjMgr.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ bool ProjMgr::PrintUsage(
9090
string signature = string("csolution: Project Manager ") + VERSION_STRING + string(" ") + COPYRIGHT_NOTICE;
9191
if (cmd.empty() && subCmd.empty()) {
9292
// print main help
93-
cout << signature << endl;
94-
cout << USAGE << endl;
93+
ProjMgrLogger::out() << signature << endl;
94+
ProjMgrLogger::out() << USAGE << endl;
9595
return true;
9696
}
9797

@@ -102,7 +102,7 @@ bool ProjMgr::PrintUsage(
102102
}
103103

104104
// print command help
105-
cout << signature << endl;
105+
ProjMgrLogger::out() << signature << endl;
106106
auto [optionalArg, cmdOptions] = cmdOptionsDict.at(filter);
107107

108108
string program = ORIGINAL_FILENAME + string(" ") + cmd +
@@ -123,12 +123,12 @@ bool ProjMgr::PrintUsage(
123123
options.custom_help(RteUtils::EMPTY_STRING);
124124
}
125125

126-
cout << options.help() << endl;
126+
ProjMgrLogger::out() << options.help() << endl;
127127
return true;
128128
}
129129

130130
void ProjMgr::ShowVersion(void) {
131-
cout << ORIGINAL_FILENAME << " " << VERSION_STRING << " " << COPYRIGHT_NOTICE << endl;
131+
ProjMgrLogger::out() << ORIGINAL_FILENAME << " " << VERSION_STRING << " " << COPYRIGHT_NOTICE << endl;
132132
}
133133

134134
int ProjMgr::ParseCommandLine(int argc, char** argv) {
@@ -733,7 +733,7 @@ bool ProjMgr::RunListPacks(void) {
733733
vector<string> packs;
734734
bool ret = m_worker.ListPacks(packs, m_missingPacks, m_filter);
735735
for (const auto& pack : packs) {
736-
cout << pack << endl;
736+
ProjMgrLogger::out() << pack << endl;
737737
}
738738
return ret;
739739
}
@@ -757,7 +757,7 @@ bool ProjMgr::RunListBoards(void) {
757757
return false;
758758
}
759759
for (const auto& device : boards) {
760-
cout << device << endl;
760+
ProjMgrLogger::out() << device << endl;
761761
}
762762
return true;
763763
}
@@ -781,7 +781,7 @@ bool ProjMgr::RunListDevices(void) {
781781
return false;
782782
}
783783
for (const auto& device : devices) {
784-
cout << device << endl;
784+
ProjMgrLogger::out() << device << endl;
785785
}
786786
return true;
787787
}
@@ -806,7 +806,7 @@ bool ProjMgr::RunListComponents(void) {
806806
}
807807

808808
for (const auto& component : components) {
809-
cout << component << endl;
809+
ProjMgrLogger::out() << component << endl;
810810
}
811811
return true;
812812
}
@@ -829,7 +829,7 @@ bool ProjMgr::RunListConfigs() {
829829
}
830830

831831
for (const auto& configFile : configFiles) {
832-
cout << configFile << endl;
832+
ProjMgrLogger::out() << configFile << endl;
833833
}
834834
return true;
835835
}
@@ -852,7 +852,7 @@ bool ProjMgr::RunListDependencies(void) {
852852
}
853853

854854
for (const auto& dependency : dependencies) {
855-
cout << dependency << endl;
855+
ProjMgrLogger::out() << dependency << endl;
856856
}
857857
return true;
858858
}
@@ -868,7 +868,7 @@ bool ProjMgr::RunListContexts(void) {
868868
return false;
869869
}
870870
for (const auto& context : contexts) {
871-
cout << context << endl;
871+
ProjMgrLogger::out() << context << endl;
872872
}
873873
return true;
874874
}
@@ -891,7 +891,7 @@ bool ProjMgr::RunListGenerators(void) {
891891
}
892892

893893
for (const auto& generator : generators) {
894-
cout << generator << endl;
894+
ProjMgrLogger::out() << generator << endl;
895895
}
896896
return true;
897897
}
@@ -930,7 +930,7 @@ bool ProjMgr::RunListLayers(void) {
930930

931931
if (!m_updateIdx) {
932932
for (const auto& layer : layers) {
933-
cout << layer << endl;
933+
ProjMgrLogger::out() << layer << endl;
934934
}
935935
}
936936

@@ -986,7 +986,7 @@ bool ProjMgr::RunCodeGenerator(void) {
986986
return false;
987987
}
988988
}
989-
989+
990990
if (m_worker.HasToolchainErrors()) {
991991
return false;
992992
}
@@ -1027,7 +1027,7 @@ bool ProjMgr::RunListToolchains(void) {
10271027
toolchainsSet.insert(toolchainEntry);
10281028
}
10291029
for (const auto& toolchainEntry : toolchainsSet) {
1030-
cout << toolchainEntry;
1030+
ProjMgrLogger::out() << toolchainEntry;
10311031
}
10321032
// If the worker has toolchain errors, set the success flag to false
10331033
if (m_worker.HasToolchainErrors()) {
@@ -1040,11 +1040,11 @@ bool ProjMgr::RunListEnvironment(void) {
10401040
string notFound = "<Not Found>";
10411041
EnvironmentList env;
10421042
m_worker.ListEnvironment(env);
1043-
cout << "CMSIS_PACK_ROOT=" << (env.cmsis_pack_root.empty() ? notFound : env.cmsis_pack_root) << endl;
1044-
cout << "CMSIS_COMPILER_ROOT=" << (env.cmsis_compiler_root.empty() ? notFound : env.cmsis_compiler_root) << endl;
1043+
ProjMgrLogger::out() << "CMSIS_PACK_ROOT=" << (env.cmsis_pack_root.empty() ? notFound : env.cmsis_pack_root) << endl;
1044+
ProjMgrLogger::out() << "CMSIS_COMPILER_ROOT=" << (env.cmsis_compiler_root.empty() ? notFound : env.cmsis_compiler_root) << endl;
10451045
CrossPlatformUtils::REG_STATUS status = CrossPlatformUtils::GetLongPathRegStatus();
10461046
if (status != CrossPlatformUtils::REG_STATUS::NOT_SUPPORTED) {
1047-
cout << "Long pathname support=" <<
1047+
ProjMgrLogger::out() << "Long pathname support=" <<
10481048
(status == CrossPlatformUtils::REG_STATUS::ENABLED ? "enabled" : "disabled") << endl;
10491049
}
10501050
return true;

tools/projmgr/src/ProjMgrCbuildIdx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ ProjMgrCbuildIdx::ProjMgrCbuildIdx(YAML::Node node,
142142
}
143143

144144
// errors, warnings and info messages
145-
const vector<pair<StrVecMap&, const char*>> messages = {
145+
const vector<pair<const StrVecMap&, const char*>> messages = {
146146
{ ProjMgrLogger::Get().GetErrors(), YAML_ERRORS },
147147
{ ProjMgrLogger::Get().GetWarns(), YAML_WARNINGS },
148148
{ ProjMgrLogger::Get().GetInfos(), YAML_INFO },

0 commit comments

Comments
 (0)