Skip to content

Commit 8f76c95

Browse files
authored
Merge pull request #767 from os-fpga/cleanup_and_logfile
planning utils: cleanup and logfile
2 parents b6ba088 + 6920f66 commit 8f76c95

File tree

5 files changed

+94
-39
lines changed

5 files changed

+94
-39
lines changed

planning/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static const char* _pln_VERSION_STR = "pln0267";
1+
static const char* _pln_VERSION_STR = "pln0270";
22

33
#include "RS/rsEnv.h"
44
#include "util/pln_log.h"

planning/src/util/cmd_line.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,44 @@ cmd_line::cmd_line(int argc, const char** argv) {
99

1010
bool needVal = false;
1111
string key, s;
12+
auto& ls = lout();
1213

1314
for (int i = 1; i < argc; ++i) {
1415
assert(argv[i]);
1516
s = argv[i];
1617
if (s.size() < 2) {
17-
cout << "Warning: Not a valid flag \"" << s << "\" discarding" << endl;
18+
ls << "Warning: Not a valid flag \"" << s << "\" discarding" << endl;
1819
continue;
1920
}
2021
if ('-' == s[0]) {
2122
if ('-' == s[1]) { // param key
2223
if (needVal)
23-
cout << "Warning: Key " << key << " did not get a value" << endl;
24+
ls << "Warning: Key " << key << " did not get a value" << endl;
2425
needVal = true;
2526
key = s;
2627
} else { // flag
2728
flags_.insert(s);
2829
if (needVal)
29-
cout << "Warning: Key " << key << " did not get a value" << endl;
30+
ls << "Warning: Key " << key << " did not get a value" << endl;
3031
needVal = false;
3132
}
3233
} else { // param value
3334
if (needVal) {
3435
params_[key] = s;
3536
needVal = false;
3637
} else {
37-
cout << "Warning: No key for value " << s << endl;
38+
ls << "Warning: No key for value " << s << endl;
3839
}
3940
}
4041
}
4142
}
4243

43-
void cmd_line::set_flag(const string& fl) { flags_.insert(fl); }
44-
45-
void cmd_line::set_param_value(string& key, string& val) { params_[key] = val; }
46-
4744
void cmd_line::print_options() const {
48-
cout << "Flags :\n";
49-
for (const auto& f : flags_) cout << "\t" << f << endl;
50-
cout << "Params :" << endl;
45+
auto& ls = lout();
46+
47+
ls << "Flags :\n";
48+
for (const auto& f : flags_) ls << "\t" << f << endl;
49+
ls << "Params :" << endl;
5150

5251
// sort by name
5352
vector<pair<string, string>> V;
@@ -56,7 +55,10 @@ void cmd_line::print_options() const {
5655

5756
std::sort(V.begin(), V.end());
5857

59-
for (const auto& p : V) cout << '\t' << p.first << '\t' << p.second << endl;
58+
for (const auto& p : V) {
59+
ls << '\t' << p.first << '\t' << p.second << endl;
60+
}
61+
}
62+
6063
}
6164

62-
} // namespace pln

planning/src/util/cmd_line.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ struct cmd_line {
1919

2020
cmd_line(int argc, const char **argv);
2121

22-
const unordered_set<string> &get_flag_set() const { return flags_; }
23-
unordered_map<string, string> get_param_map() const { return params_; }
22+
const unordered_set<string>& get_flag_set() const noexcept { return flags_; }
23+
const unordered_map<string, string>& get_param_map() const noexcept { return params_; }
2424

25-
bool is_flag_set(const string &fl) const noexcept { return flags_.count(fl); }
25+
bool is_flag_set(const string& fl) const noexcept { return flags_.count(fl); }
2626

27-
string get_param(const string &key) const noexcept {
27+
string get_param(const string& key) const noexcept {
2828
auto fitr = params_.find(key);
29-
if (fitr == params_.end()) return "";
29+
if (fitr == params_.end()) return {};
3030
return fitr->second;
3131
}
3232

33-
void set_flag(const string &fl);
34-
void set_param_value(string &key, string &val);
33+
void set_flag(const string& fl) { flags_.insert(fl); }
34+
35+
void set_param_value(const string& key, const string& val) { params_[key] = val; }
36+
3537
void print_options() const;
3638
};
3739

38-
} // namespace pln
40+
}
3941

4042
#endif

planning/src/util/pln_log.cpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "util/pln_log.h"
22

3-
#include <stdarg.h>
43
#include <alloca.h>
54
#include <errno.h>
65
#include <fcntl.h>
6+
#include <stdarg.h>
77
#include <sys/stat.h>
88
#include <sys/types.h>
99
#include <unistd.h>
@@ -12,21 +12,13 @@ namespace pln {
1212

1313
using namespace std;
1414

15+
static void flush_all() noexcept;
16+
1517
// log-trace value (debug print verbosity)
1618
// can be set in main() by calling set_ltrace()
1719
static uint16_t s_logLevel = 0;
18-
uint16_t ltrace() noexcept { return s_logLevel; }
1920

20-
static void flush_all() noexcept {
21-
// Flushing does not prevent interleaved stdout and stderr
22-
// becauses FOEDAG messes with child process stdout.
23-
// pin_c will create a separate 'pin_c.log' sometime.
24-
fflush(stdout);
25-
fflush(stderr);
26-
cout.flush();
27-
cerr.flush();
28-
// ::fsync(1);
29-
}
21+
uint16_t ltrace() noexcept { return s_logLevel; }
3022

3123
void set_ltrace(int t) noexcept {
3224
flush_all();
@@ -42,6 +34,52 @@ void set_ltrace(int t) noexcept {
4234
s_logLevel = t;
4335
}
4436

37+
//
38+
// LOut is an ostream that splits the output,
39+
// i.e. it prints to a file (logfile) and to stdout.
40+
/////////////
41+
42+
static constexpr size_t LOut_buf_cap = 65518;
43+
44+
LOut::LOut() noexcept : std::ostream(this), buf_sz_(0) {
45+
buf_ = (char*)::calloc(LOut_buf_cap + 2, 1);
46+
assert(buf_);
47+
}
48+
49+
LOut::~LOut() {
50+
if (buf_sz_) cout << buf_ << endl;
51+
fflush(stdout);
52+
fflush(stderr);
53+
cout.flush();
54+
cerr.flush();
55+
}
56+
57+
int LOut::overflow(int c) {
58+
if (c == '\n') {
59+
cout << buf_;
60+
flush_out(true);
61+
if (buf_sz_)
62+
buf_sz_--;
63+
return 0;
64+
}
65+
assert(buf_sz_ < LOut_buf_cap);
66+
buf_[buf_sz_++] = c;
67+
return 0;
68+
}
69+
70+
/////////////
71+
72+
static void flush_all() noexcept {
73+
// Flushing does not prevent interleaved stdout and stderr
74+
// becauses FOEDAG messes with child process stdout.
75+
// pin_c will create a separate 'pin_c.log' sometime.
76+
fflush(stdout);
77+
fflush(stderr);
78+
cout.flush();
79+
cerr.flush();
80+
// ::fsync(1);
81+
}
82+
4583
#define LPUT if (cs && cs[0]) cout << cs;
4684
#define LEND cout << endl; fflush(stdout);
4785

planning/src/util/pln_log.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
// lprintf2 : log-printf with CC to stderr
77
//
88
// lputs : log-puts
9-
// lout : replaces cout, will print to both stdout and logfile
10-
//
11-
// currently, log- functions just print on stdout, real logfile can be added later
9+
// LOut : replaces cout, prints to both stdout and logfile
1210
//
1311
// lputs<Number> functions are equivalent to lputs(),
1412
// there are convenient "anchor points" for setting temporary breakpoints.
@@ -323,9 +321,24 @@ inline void logVec(const std::vector<T>& vec, CStr pref) noexcept {
323321
template <typename T>
324322
inline T* unconst(const T* p) noexcept { return const_cast<T*>(p); }
325323

326-
} // namespace pln
324+
//
325+
// LOut is an ostream that splits the output,
326+
// i.e. it prints to a file (logfile) and to stdout.
327+
//
328+
struct LOut : public std::ostream, public std::streambuf {
329+
char* buf_ = nullptr;
330+
uint buf_sz_ = 0;
327331

328-
namespace pinc = pln;
332+
LOut() noexcept;
333+
virtual ~LOut();
334+
335+
virtual int overflow(int c) override;
336+
337+
LOut(const LOut&) = delete;
338+
LOut& operator = (const LOut&) = delete;
339+
};
340+
341+
} // namespace pln
329342

330343
const char* pln_get_version();
331344

0 commit comments

Comments
 (0)