Module: check_mk
Branch: master
Commit: dcee878fb080931a0bbeaeeaa378bb9465dc2c92
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dcee878fb08093…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Thu Feb 1 14:09:13 2018 +0100
Windows agent: wrap FILE pointer with RAII
---
agents/windows/build_version | 2 +-
agents/windows/check_mk_agent.cc | 36 +++++++++++++++++++++---------------
2 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index a4e26ee..2aeb20d 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3084
+3086
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index 802f138..564a71c 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -154,9 +154,6 @@ public:
UnpackError(const std::string &what) : std::runtime_error(what) {}
};
-bool do_file = false;
-static FILE *fileout;
-
class MillisecondsFormatter : public Formatter {
void format(ostream &os, const LogRecord &record) override {
auto tp = record.getTimePoint();
@@ -510,21 +507,37 @@ void usage() {
exit(1);
}
+void outputFileBase(const Environment &env, FILE *file) {
+ FileOutputProxy dummy(file);
+ output_data(dummy, env, false, *s_config->section_flush);
+}
+
void do_debug(const Environment &env) {
Logger *logger = Logger::getLogger("winagent");
const auto saveLevel = logger->getLevel();
logger->setLevel(LogLevel::notice);
- FileOutputProxy dummy(do_file ? fileout : stdout);
+ outputFileBase(env, stdout);
- output_data(dummy, env, false, *s_config->section_flush);
logger->setLevel(saveLevel);
}
void do_test(const Environment &env) {
- FileOutputProxy dummy(do_file ? fileout : stdout);
Notice(Logger::getLogger("winagent")) << "Started in test
mode.";
- output_data(dummy, env, false, *s_config->section_flush);
+ outputFileBase(env, stdout);
+}
+
+void do_file(const Environment &env, const char *filename) {
+ auto closeFile = [](FILE *f) { fclose(f); };
+ std::unique_ptr<FILE, decltype(closeFile)> file(fopen(filename,
"w"),
+ closeFile);
+
+ if (!file) {
+ std::cerr << "Cannot open " << filename << " for
writing." << std::endl;
+ exit(1);
+ }
+
+ outputFileBase(env, file.get());
}
bool ctrl_handler(DWORD fdwCtrlType) {
@@ -994,14 +1007,7 @@ void RunImmediate(const char *mode, int argc, char **argv) {
fprintf(stderr, "Please specify the name of an output file.\n");
exit(1);
}
- fileout = fopen(argv[0], "w");
- if (!fileout) {
- fprintf(stderr, "Cannot open %s for writing.\n", argv[2]);
- exit(1);
- }
- do_file = true;
- do_test(env);
- fclose(fileout);
+ do_file(env, argv[0]);
} else if (!strcmp(mode, "adhoc") || !strcmp(mode, "service"))
do_adhoc(env);
else if (!strcmp(mode, "install"))