File indexing completed on 2024-12-22 05:06:06

0001 /*
0002  *   Copyright (C) 2021 Christian Mollekopf <christian@mkpf.ch>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include <QDebug>
0021 #include <QObject> // tr()
0022 #include <iostream>
0023 
0024 #include "common/log.h"
0025 #include "common/notification.h"
0026 #include "common/notifier.h"
0027 
0028 #include "sinksh_utils.h"
0029 #include "state.h"
0030 #include "syntaxtree.h"
0031 
0032 namespace SinkMonitor
0033 {
0034 
0035 Syntax::List syntax();
0036 
0037 bool monitor(const QStringList &args_, State &state)
0038 {
0039 
0040     auto options = SyntaxTree::parseOptions(args_);
0041 
0042     Sink::Query query;
0043     query.setId("monitor");
0044     if (options.options.contains("resource")) {
0045         for (const auto &f : options.options.value("resource")) {
0046             query.filter(f.toLatin1());
0047         }
0048     }
0049 
0050     auto notifier = new Sink::Notifier{query};
0051 
0052     notifier->registerHandler([&] (const Sink::Notification &notification) {
0053         QString line;
0054         QDebug(&line)<< "Resource:" << notification.resource << notification;
0055         state.printLine(line);
0056     });
0057 
0058 
0059     return true;
0060 }
0061 
0062 Syntax::List syntax()
0063 {
0064     Syntax resource("monitor", QObject::tr("Monitor resource status."), &SinkMonitor::monitor, Syntax::EventDriven);
0065 
0066     resource.addParameter("resource", {"resource", "Resource to monitor" });
0067 
0068     resource.completer = &SinkshUtils::resourceOrTypeCompleter;
0069     return {resource};
0070 }
0071 
0072 REGISTER_SYNTAX(SinkMonitor)
0073 
0074 }