diff mbox series

[v2,1/8] kernel-shark: Configuration information in ${HOME}/.cache/kernelshark

Message ID 20190418152146.27232-2-ykaradzhov@vmware.com (mailing list archive)
State Superseded
Headers show
Series Various modifications and fixes toward KS 1.0 | expand

Commit Message

Yordan Karadzhov April 18, 2019, 3:21 p.m. UTC
By default the "Last session" configuration file will be saved in
${HOME}/.cache/kernelshark. If ${HOME}/.cache/kernelshark doesn't exist,
it will be created automatically. The user can select another directory
to be used to store the cached data. This can be done by setting the
environment variable KS_USER_CACHE_DIR. In this case if the path (the
value of KS_USER_CACHE_DIR) doesn't exist the user will be asked before
create it.

Suggested-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark/src/KsMainWindow.cpp | 68 +++++++++++++++++++++++++++----
 kernel-shark/src/KsMainWindow.hpp |  4 ++
 2 files changed, 63 insertions(+), 9 deletions(-)

Comments

Slavomir Kaslev April 19, 2019, 7:19 a.m. UTC | #1
On Thu, Apr 18, 2019 at 6:22 PM Yordan Karadzhov <ykaradzhov@vmware.com> wrote:
>
> By default the "Last session" configuration file will be saved in
> ${HOME}/.cache/kernelshark. If ${HOME}/.cache/kernelshark doesn't exist,
> it will be created automatically. The user can select another directory
> to be used to store the cached data. This can be done by setting the
> environment variable KS_USER_CACHE_DIR. In this case if the path (the
> value of KS_USER_CACHE_DIR) doesn't exist the user will be asked before
> create it.
>
> Suggested-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
> Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
>
> Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>

Nit: two identical Signed-off-by tags

> ---
>  kernel-shark/src/KsMainWindow.cpp | 68 +++++++++++++++++++++++++++----
>  kernel-shark/src/KsMainWindow.hpp |  4 ++
>  2 files changed, 63 insertions(+), 9 deletions(-)
>
> diff --git a/kernel-shark/src/KsMainWindow.cpp b/kernel-shark/src/KsMainWindow.cpp
> index 7afb721..cf2db74 100644
> --- a/kernel-shark/src/KsMainWindow.cpp
> +++ b/kernel-shark/src/KsMainWindow.cpp

[...]

> -void KsMainWindow::_restoreSession()
> +QString KsMainWindow::_getCacheDir()
> +{
> +       QString dir;
> +
> +       auto lamMakePath = [&] (bool ask) {
> +               if (ask) {
> +                       QMessageBox::StandardButton reply;
> +                       QString err("KernelShark cache directory not found!\n");
> +                       QString question =
> +                               QString("Do you want to create %1").arg(dir);
> +
> +                       reply = QMessageBox::question(this, "KernelShark",
> +                                                     err + question,
> +                                                     QMessageBox::Yes |
> +                                                     QMessageBox::No);
> +
> +                       if (reply == QMessageBox::No) {
> +                               dir.clear();
> +                               return;
> +                       }
> +               }
> +
> +               QDir().mkpath(dir);
> +       };
> +
> +       dir = getenv("KS_USER_CACHE_DIR");
> +       if (!dir.isEmpty()) {
> +               if (!QDir(dir).exists())
> +                       lamMakePath(true);
> +       } else {
> +               dir = QString(getpwuid(getuid())->pw_dir) +
> +                     "/.cache/kernelshark";

Nit: s/QString(getpwuid(getuid())->pw_dir)/QDir::homePath()/

Everything else looks great with the patch
Reviewed-by: Slavomir Kaslev <kaslevs@vmware.com>

Cheers,

--Slavi
Steven Rostedt April 19, 2019, 6:18 p.m. UTC | #2
On Fri, 19 Apr 2019 10:19:38 +0300
Slavomir Kaslev <slavomir.kaslev@gmail.com> wrote:

> Nit: s/QString(getpwuid(getuid())->pw_dir)/QDir::homePath()/
> 
> Everything else looks great with the patch
> Reviewed-by: Slavomir Kaslev <kaslevs@vmware.com>

Yordan,

If someone gives you a review by, please add it to the next revision, if
you didn't change the patch. And if you do change it, ask for a new
review-by.

Thanks!

-- Steve
diff mbox series

Patch

diff --git a/kernel-shark/src/KsMainWindow.cpp b/kernel-shark/src/KsMainWindow.cpp
index 7afb721..cf2db74 100644
--- a/kernel-shark/src/KsMainWindow.cpp
+++ b/kernel-shark/src/KsMainWindow.cpp
@@ -133,13 +133,15 @@  KsMainWindow::KsMainWindow(QWidget *parent)
 KsMainWindow::~KsMainWindow()
 {
 	kshark_context *kshark_ctx(nullptr);
-	QString file = KS_CONF_DIR;
+	QString file = lastSessionFile();
 
-	file += "/lastsession.json";
+	if (!file.isEmpty()) {
+		QByteArray fileBA = file.toLocal8Bit();
 
-	_updateSession();
-	kshark_save_config_file(file.toLocal8Bit().data(),
-				_session.getConfDocPtr());
+		_updateSession();
+		kshark_save_config_file(fileBA.data(),
+					_session.getConfDocPtr());
+	}
 
 	_data.clear();
 
@@ -368,12 +370,60 @@  void KsMainWindow::_open()
 		loadDataFile(fileName);
 }
 
-void KsMainWindow::_restoreSession()
+QString KsMainWindow::_getCacheDir()
+{
+	QString dir;
+
+	auto lamMakePath = [&] (bool ask) {
+		if (ask) {
+			QMessageBox::StandardButton reply;
+			QString err("KernelShark cache directory not found!\n");
+			QString question =
+				QString("Do you want to create %1").arg(dir);
+
+			reply = QMessageBox::question(this, "KernelShark",
+						      err + question,
+						      QMessageBox::Yes |
+						      QMessageBox::No);
+
+			if (reply == QMessageBox::No) {
+				dir.clear();
+				return;
+			}
+		}
+
+		QDir().mkpath(dir);
+	};
+
+	dir = getenv("KS_USER_CACHE_DIR");
+	if (!dir.isEmpty()) {
+		if (!QDir(dir).exists())
+			lamMakePath(true);
+	} else {
+		dir = QString(getpwuid(getuid())->pw_dir) +
+		      "/.cache/kernelshark";
+		if (!QDir(dir).exists())
+			lamMakePath(false);
+	}
+
+	return dir;
+}
+
+/** Get the description file of the last session. */
+QString KsMainWindow::lastSessionFile()
 {
-	QString file = KS_CONF_DIR;
-	file += "/lastsession.json";
+	QString file;
 
-	loadSession(file);
+	file = _getCacheDir();
+	if (!file.isEmpty())
+		file += "/lastsession.json";
+
+	return file;
+}
+
+void KsMainWindow::_restoreSession()
+{
+	loadSession(lastSessionFile());
 	_graph.updateGeom();
 }
 
diff --git a/kernel-shark/src/KsMainWindow.hpp b/kernel-shark/src/KsMainWindow.hpp
index 78cd442..ec6506e 100644
--- a/kernel-shark/src/KsMainWindow.hpp
+++ b/kernel-shark/src/KsMainWindow.hpp
@@ -37,6 +37,8 @@  public:
 
 	void loadSession(const QString &fileName);
 
+	QString lastSessionFile();
+
 	/**
 	 * @brief
 	 *
@@ -230,6 +232,8 @@  private:
 
 	void _filterSyncCBoxUpdate(kshark_context *kshark_ctx);
 
+	QString _getCacheDir();
+
 private slots:
 	void _captureFinished(int, QProcess::ExitStatus);
 };