Browse Source

Merge branch 'cli-verbosity' into 'develop'

Provide switch to enable verbosity

Closes #63

See merge request tobias.wach/ccats!57
Sander, Paul 4 years ago
parent
commit
4d13e2812f

+ 3 - 2
cli/include/batchioman.h

@@ -38,6 +38,8 @@ private:
 	std::recursive_mutex msgmutex;
 
 	bool getnextline;
+	bool verbose;
+	string filepath;
 	std::mutex linemutex;
 	std::condition_variable linecv;
 
@@ -74,8 +76,7 @@ public:
 	/**
 	 * Constructor and destructor
 	 */
-	BatchIoMan(char *ipcstring);
-	BatchIoMan(char *ipcstring, bool usessl, string batchpath);
+	BatchIoMan(char *ipcstring, bool usessl, bool beverbose, string batchpath);
 	~BatchIoMan();
 
 	bool init();

+ 4 - 1
cli/include/machineioman.h

@@ -10,8 +10,11 @@
  * for interactive non-user sessions
  */
 class MachineIoMan : public IoMan {
+private:
+	bool verbose;
+
 public:
-	using IoMan::IoMan;
+	MachineIoMan(char *ipcstring, bool usessl, bool beverbose);
 
 protected:
 	/**

+ 3 - 2
cli/include/userioman.h

@@ -24,6 +24,8 @@ private:
 	Json::StreamWriterBuilder wbuilder;
 	string jsonerror;
 
+	bool verbose;
+
 	/**
 	 * Format and pretty print json for terminal output
 	 */
@@ -62,8 +64,7 @@ public:
 	/**
 	 * Constructor and destructor
 	 */
-	UserIoMan(char *ipcstring);
-	UserIoMan(char *ipcstring, bool usessl);
+	UserIoMan(char *ipcstring, bool usessl, bool verbose);
 	~UserIoMan();
 
 protected:

+ 13 - 11
cli/src/batchioman.cpp

@@ -4,14 +4,7 @@
 #include <string>
 #include <vector>
 
-BatchIoMan::BatchIoMan(char *ipcstring) : IoMan(ipcstring){};
-
-BatchIoMan::BatchIoMan(char *ipcstring, bool usessl, std::string batchpath) : IoMan(ipcstring, usessl) {
-	batchin.open(batchpath);
-	normalout.open(batchpath + ".out");
-	debugout.open(batchpath + ".debug");
-	errorout.open(batchpath + ".err");
-
+BatchIoMan::BatchIoMan(char *ipcstring, bool usessl, bool beverbose, std::string batchpath) : IoMan(ipcstring, usessl) {
 	/* setup json stuff */
 	Json::CharReaderBuilder rbuilder;
 	wbuilder.settings_["indentation"] = "";
@@ -38,10 +31,17 @@ BatchIoMan::BatchIoMan(char *ipcstring, bool usessl, std::string batchpath) : Io
 	printmap["closekey"] = &BatchIoMan::printClosekey;
 
 	getnextline = false;
+	verbose = beverbose;
+	filepath = batchpath;
 }
 
 bool BatchIoMan::init() {
-	if (!batchin.is_open() || !normalout.is_open() || !debugout.is_open() || !errorout.is_open())
+	batchin.open(filepath);
+	normalout.open(filepath + ".out");
+	if (verbose)
+		debugout.open(filepath + ".debug");
+	errorout.open(filepath + ".err");
+	if (!batchin.is_open() || !normalout.is_open() || (verbose && !debugout.is_open()) || !errorout.is_open())
 		return false;
 	return IoMan::init();
 }
@@ -49,7 +49,8 @@ bool BatchIoMan::init() {
 BatchIoMan::~BatchIoMan() {
 	batchin.close();
 	normalout.close();
-	debugout.close();
+	if (verbose)
+		debugout.close();
 	errorout.close();
 	delete reader;
 }
@@ -77,7 +78,8 @@ void BatchIoMan::printMessage(std::string msg, OutMsgType type) {
 		break;
 	}
 	case debug: {
-		debugout << msg << std::endl;
+		if (verbose)
+			debugout << msg << std::endl;
 		break;
 	}
 	}

+ 9 - 12
cli/src/ioman.cpp

@@ -25,18 +25,7 @@ extern IoMan *gIOMAN;
 
 void ioman_externalDebugPrint(string msg) { gIOMAN->printMessage(msg, gIOMAN->OutMsgType::debug); }
 
-IoMan::IoMan(char *ipcstring, bool enablessl) : IoMan(ipcstring) {
-	usessl = enablessl;
-	if (usessl) {
-		sslctx = new boost::asio::ssl::context(boost::asio::ssl::context::sslv23);
-		sslctx->set_verify_mode(boost::asio::ssl::verify_peer);
-		sslctx->set_options(boost::asio::ssl::context::no_sslv2);
-		sslctx->load_verify_file("rootca.crt");
-		sslsock = new boost::asio::ssl::stream<tcp::socket &>(*tcpsock, *sslctx);
-	}
-}
-
-IoMan::IoMan(char *ipcstring) : cmdman(fileman, &ioman_externalDebugPrint), recvbuf(16384) {
+IoMan::IoMan(char *ipcstring, bool enablessl) : cmdman(fileman, &ioman_externalDebugPrint), recvbuf(16384) {
 	ipstring = std::string(ipcstring);
 	port = 1234;
 	tcpsock = new tcp::socket(ios);
@@ -54,6 +43,14 @@ IoMan::IoMan(char *ipcstring) : cmdman(fileman, &ioman_externalDebugPrint), recv
 	runresponse = false;
 	versionstatus = off;
 	loginstatus = off;
+	usessl = enablessl;
+	if (usessl) {
+		sslctx = new boost::asio::ssl::context(boost::asio::ssl::context::sslv23);
+		sslctx->set_verify_mode(boost::asio::ssl::verify_peer);
+		sslctx->set_options(boost::asio::ssl::context::no_sslv2);
+		sslctx->load_verify_file("rootca.crt");
+		sslsock = new boost::asio::ssl::stream<tcp::socket &>(*tcpsock, *sslctx);
+	}
 }
 
 IoMan::~IoMan() {

+ 5 - 8
cli/src/machineioman.cpp

@@ -3,6 +3,8 @@
 #include <iostream>
 #include <vector>
 
+MachineIoMan::MachineIoMan(char *ipcstring, bool usessl, bool beverbose) : IoMan(ipcstring, usessl) { verbose = beverbose; }
+
 void MachineIoMan::printMessage(std::string msg, OutMsgType type) {
 	switch (type) {
 	case normal: {
@@ -14,18 +16,13 @@ void MachineIoMan::printMessage(std::string msg, OutMsgType type) {
 		break;
 	}
 	case debug: {
-		//~ std::cerr << msg << std::endl;
+		if (verbose)
+			std::cerr << msg << std::endl;
 		break;
 	}
 	}
 }
 
-void MachineIoMan::printWelcomeMessage() {
-	//~ std::cout << "please enter user and password" << std::endl;
-}
+void MachineIoMan::printWelcomeMessage() {}
 
 std::string MachineIoMan::getCmdPrompt() { return ""; }
-
-// currently not in use:
-//~ std::string MachineIoMan::getUserPrompt() { return ""; }
-//~ std::string MachineIoMan::getPassPrompt() { return ""; }

+ 10 - 6
cli/src/main.cpp

@@ -20,10 +20,11 @@ void show_help(char *exec) {
 int main(int argc, char **argv) {
 	bpo::options_description desc("Options");
 	desc.add_options()("help", "show this help")("machine", "switch to machine mode for I/O (not designed for user interaction)")(
-	    "batch", bpo::value<std::string>(), "run operations from arg as batch file")("usessl", "enable ssl for connection to server");
+	    "batch", bpo::value<std::string>(), "run operations from arg as batch file")("usessl", "enable ssl for connection to server")("verbose",
+	                                                                                                                                  "enable debug output");
 
 	bpo::variables_map vm;
-	bool machine = false, usessl = false, batch = false;
+	bool machine = false, usessl = false, batch = false, verbose = false;
 	const char *file = NULL;
 	IoMan *ioman;
 
@@ -54,16 +55,19 @@ int main(int argc, char **argv) {
 		if (vm.count("usessl")) {
 			usessl = true;
 		}
+		if (vm.count("verbose")) {
+			verbose = true;
+		}
 	} catch (const bpo::error &ex) {
 		std::fprintf(stderr, "%s\n", ex.what());
 	}
-	std::printf("ip %s machine mode is %d file is %s enablessl is %d\n", argv[1], machine, file ? file : "", usessl);
+	std::printf("ip %s machine mode is %d file is %s enablessl is %d verbose is %d\n", argv[1], machine, file ? file : "", usessl, verbose);
 	if (batch) {
-		ioman = new BatchIoMan(argv[1], usessl, file);
+		ioman = new BatchIoMan(argv[1], usessl, verbose, file);
 	} else if (machine) {
-		ioman = new MachineIoMan(argv[1], usessl);
+		ioman = new MachineIoMan(argv[1], usessl, verbose);
 	} else {
-		ioman = new UserIoMan(argv[1], usessl);
+		ioman = new UserIoMan(argv[1], usessl, verbose);
 	}
 	gIOMAN = ioman;
 	if (ioman->init()) {

+ 4 - 12
cli/src/userioman.cpp

@@ -4,13 +4,12 @@
 #include <readline/readline.h>
 #include <vector>
 
-UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring){};
-
-UserIoMan::UserIoMan(char *ipcstring, bool usessl) : IoMan(ipcstring, usessl) {
+UserIoMan::UserIoMan(char *ipcstring, bool usessl, bool beverbose) : IoMan(ipcstring, usessl) {
 	/* setup json stuff */
 	Json::CharReaderBuilder rbuilder;
 	wbuilder.settings_["indentation"] = "";
 	reader = rbuilder.newCharReader();
+	verbose = beverbose;
 
 	/* initialize print command map */
 	printmap["error"] = &UserIoMan::printError;
@@ -49,12 +48,9 @@ void UserIoMan::printMessage(std::string msg, OutMsgType type) {
 		}
 		break;
 	}
-	//~ case error: {
-	//~ std::cout << msg << std::endl;
-	//~ break;
-	//~ }
 	case debug: {
-		std::cerr << msg << std::endl;
+		if (verbose)
+			std::cerr << msg << std::endl;
 		break;
 	}
 	}
@@ -70,10 +66,6 @@ void UserIoMan::printWelcomeMessage() {
 
 std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
 
-// currently not in use:
-//~ std::string UserIoMan::getUserPrompt() { return "User: "; }
-//~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
-
 void UserIoMan::printJson(Json::Value root) {
 	map<string, void (UserIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
 	if (it == printmap.end()) {