Skip to content

Commit 422d5ba

Browse files
committed
count warnings and errors when piping via stdin
1 parent 3d8b2fa commit 422d5ba

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/cpp/buildhl/project_detect.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ namespace buildhl {
4141
public:
4242
virtual ssize_t write(const void* buffer, size_t size)=0;
4343
};
44+
45+
class NullStream : public InputStream, public OutputStream {
46+
public:
47+
ssize_t write(const void* buffer, size_t size) override {
48+
return size;
49+
}
50+
ssize_t read(void* buffer, size_t) override {
51+
return 0;
52+
}
53+
};
54+
4455
class CFileOutputStream : public OutputStream {
4556
public:
4657
CFileOutputStream(){}
@@ -62,6 +73,7 @@ namespace buildhl {
6273
};
6374
typedef unique_ptr<InputStream> InputStream_uptr;
6475
typedef unique_ptr<OutputStream> OutputStream_uptr;
76+
typedef unique_ptr<NullStream> NullStream_uptr;
6577
typedef unique_ptr<CFileOutputStream> CFileOutputStream_uptr;
6678

6779
struct PipeInputStream : InputStream {

src/cpp/buildhl_main.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ std::string dirname(std::string path) {
2424

2525
class StreamProcessor {
2626
public:
27-
StreamProcessor(){}
27+
StreamProcessor(){
28+
process_line("[build start]");
29+
}
2830
StreamProcessor(const std::string log_file) {
2931
std::string dir = dirname(log_file);
3032
if (!tea::path_exists(dir)) {
3133
tea::mkdir_p(dir);
3234
}
33-
FILE* fp = fopen(log_file.c_str(), "w");
35+
FILE* fp = nullptr;
36+
if (!log_file.empty())
37+
fp = fopen(log_file.c_str(), "w");
3438
if (fp == nullptr) {
35-
log("could not open for writing: " + log_file);
39+
process_line("could not open for writing: " + log_file);
3640
} else {
3741
m_log_file = std::make_unique<CFileOutputStream>(fp);
3842
}
@@ -173,7 +177,7 @@ class StreamProcessor {
173177
if (m_update_thread.joinable())
174178
m_update_thread.join();
175179
}
176-
CFileOutputStream_uptr m_log_file;
180+
OutputStream_uptr m_log_file;
177181
FileFilter m_file_filter;
178182
subprocess::StopWatch m_stop_watch;
179183
ProgressGraph m_progress;
@@ -195,6 +199,12 @@ std::vector<std::string> argv_to_vector(int argc, char** argv) {
195199
return result;
196200
}
197201

202+
struct CinStream : buildhl::InputStream {
203+
ssize_t read(void* buffer, size_t size) override {
204+
return fread(buffer, 1, size, stdin);
205+
}
206+
};
207+
198208
int main(int argc, char** argv) {
199209
for (int i = 1; i < argc; ++i) {
200210
if (strcmp(argv[i], "--version") == 0) {
@@ -205,10 +215,10 @@ int main(int argc, char** argv) {
205215
}
206216

207217
if (argc == 2 && strcmp(argv[1], "-") == 0) {
208-
for (std::string line; std::getline(std::cin, line);) {
209-
line = color_line(line);
210-
std::cout << line << '\n';
211-
}
218+
StreamProcessor stream_processor;
219+
stream_processor.add_search_path(tea::getcwd());
220+
CinStream cin;
221+
stream_processor.process(cin);
212222
return 0;
213223
}
214224

0 commit comments

Comments
 (0)