summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-11 11:44:04 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-11 11:44:04 +0100
commitd2d87b5d0d57aaeb46d2058a28749c2463a86a77 (patch)
tree828ad131eea140407843f6ad7f207823de62e1b7
parentb1a66bc9b66213d1d4852553e7180dcc76fb6bf7 (diff)
parentb59a7fe545aa2732e98ecc373c4ab5ca741cd920 (diff)
downloadsink-d2d87b5d0d57aaeb46d2058a28749c2463a86a77.tar.gz
sink-d2d87b5d0d57aaeb46d2058a28749c2463a86a77.zip
Merge branch 'feature/akonadish_log_control' into develop
-rw-r--r--akonadish/CMakeLists.txt3
-rw-r--r--akonadish/akonadish_utils.cpp26
-rw-r--r--akonadish/state.cpp14
-rw-r--r--akonadish/state.h3
-rw-r--r--akonadish/syntax_modules/core_syntax.cpp26
-rw-r--r--akonadish/utils.cpp42
-rw-r--r--akonadish/utils.h30
-rw-r--r--common/log.cpp18
-rw-r--r--common/log.h4
9 files changed, 139 insertions, 27 deletions
diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt
index 6761a32..eaedf9a 100644
--- a/akonadish/CMakeLists.txt
+++ b/akonadish/CMakeLists.txt
@@ -18,7 +18,8 @@ set(akonadi2_cli_SRCS
18 akonadish_utils.cpp 18 akonadish_utils.cpp
19 repl/repl.cpp 19 repl/repl.cpp
20 repl/replStates.cpp 20 repl/replStates.cpp
21 state.cpp) 21 state.cpp
22 utils.cpp)
22 23
23include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) 24include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
24 25
diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp
index 070d788..140c741 100644
--- a/akonadish/akonadish_utils.cpp
+++ b/akonadish/akonadish_utils.cpp
@@ -22,6 +22,8 @@
22 22
23#include "common/clientapi.h" 23#include "common/clientapi.h"
24 24
25#include "utils.h"
26
25namespace AkonadishUtils 27namespace AkonadishUtils
26{ 28{
27 29
@@ -98,40 +100,24 @@ QStringList resourceIds(State &state)
98 return resources; 100 return resources;
99} 101}
100 102
101QStringList filtered(const QStringList &list, const QString &fragment)
102{
103 if (fragment.isEmpty()) {
104 return list;
105 }
106
107 QStringList filtered;
108 for (auto item: list) {
109 if (item.startsWith(fragment)) {
110 filtered << item;
111 }
112 }
113
114 return filtered;
115}
116
117QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state) 103QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state)
118{ 104{
119 return filtered(resourceIds(state), fragment); 105 return Utils::filteredCompletions(resourceIds(state), fragment);
120} 106}
121 107
122QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state) 108QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state)
123{ 109{
124 static QStringList types = QStringList() << "resource" << "folder" << "mail" << "event"; 110 static QStringList types = QStringList() << "resource" << "folder" << "mail" << "event";
125 if (commands.count() == 1) { 111 if (commands.count() == 1) {
126 return filtered(s_types, fragment); 112 return Utils::filteredCompletions(s_types, fragment);
127 } 113 }
128 114
129 return filtered(resourceIds(state), fragment); 115 return Utils::filteredCompletions(resourceIds(state), fragment);
130} 116}
131 117
132QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state) 118QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state)
133{ 119{
134 return filtered(s_types, fragment); 120 return Utils::filteredCompletions(s_types, fragment);
135} 121}
136 122
137QMap<QString, QString> keyValueMapFromArgs(const QStringList &args) 123QMap<QString, QString> keyValueMapFromArgs(const QStringList &args)
diff --git a/akonadish/state.cpp b/akonadish/state.cpp
index 85cbe10..9fb5bcc 100644
--- a/akonadish/state.cpp
+++ b/akonadish/state.cpp
@@ -24,6 +24,8 @@
24#include <QEventLoop> 24#include <QEventLoop>
25#include <QTextStream> 25#include <QTextStream>
26 26
27#include "common/log.h"
28
27static bool s_hasEventLoop = false; 29static bool s_hasEventLoop = false;
28 30
29class State::Private 31class State::Private
@@ -127,4 +129,16 @@ bool State::commandTiming() const
127 return d->timing; 129 return d->timing;
128} 130}
129 131
132void State::setLoggingLevel(const QString &level) const
133{
134 Akonadi2::Log::setDebugOutputLevel(Akonadi2::Log::debugLevelFromName(level.toLatin1()));
135}
136
137QString State::loggingLevel() const
138{
139 // do not turn this into a single line return: that core dumps due to allocation of
140 // the byte array in Akonadi2::Log
141 QByteArray rv = Akonadi2::Log::debugLevelName(Akonadi2::Log::debugOutputLevel());
142 return rv.toLower();
143}
130 144
diff --git a/akonadish/state.h b/akonadish/state.h
index b1ca358..3c4c2c7 100644
--- a/akonadish/state.h
+++ b/akonadish/state.h
@@ -39,6 +39,9 @@ public:
39 int commandStarted() const; 39 int commandStarted() const;
40 void commandFinished(int returnCode = 0) const; 40 void commandFinished(int returnCode = 0) const;
41 41
42 void setLoggingLevel(const QString &level) const;
43 QString loggingLevel() const;
44
42 static void setHasEventLoop(bool evented); 45 static void setHasEventLoop(bool evented);
43 static bool hasEventLoop(); 46 static bool hasEventLoop();
44 47
diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp
index b4812df..ccf96c1 100644
--- a/akonadish/syntax_modules/core_syntax.cpp
+++ b/akonadish/syntax_modules/core_syntax.cpp
@@ -24,6 +24,7 @@
24 24
25#include "state.h" 25#include "state.h"
26#include "syntaxtree.h" 26#include "syntaxtree.h"
27#include "utils.h"
27 28
28namespace CoreSyntax 29namespace CoreSyntax
29{ 30{
@@ -145,6 +146,24 @@ bool printSyntaxTree(const QStringList &, State &state)
145 return true; 146 return true;
146} 147}
147 148
149bool setLoggingLevel(const QStringList &commands, State &state)
150{
151 if (commands.count() != 1) {
152 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
153 return false;
154 }
155
156 state.setLoggingLevel(commands.at(0));
157 return true;
158}
159
160bool printLoggingLevel(const QStringList &commands, State &state)
161{
162 const QString level = state.loggingLevel();
163 state.printLine(level);
164 return true;
165}
166
148Syntax::List syntax() 167Syntax::List syntax()
149{ 168{
150 Syntax::List syntax; 169 Syntax::List syntax;
@@ -158,15 +177,22 @@ Syntax::List syntax()
158 177
159 Syntax set("set", QObject::tr("Sets settings for the session")); 178 Syntax set("set", QObject::tr("Sets settings for the session"));
160 set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); 179 set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel);
180
161 Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete")); 181 Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"));
162 setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); 182 setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; });
163 setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); 183 setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; });
164 set.children << setTiming; 184 set.children << setTiming;
185
186 Syntax logging("logging", QObject::tr("Set the logging level to one of Trace, Log, Warning or Error"), &CoreSyntax::setLoggingLevel);
187 logging.completer = [](const QStringList &, const QString &fragment, State &state) -> QStringList { return Utils::filteredCompletions(QStringList() << "trace" << "log" << "warning" << "error", fragment, Qt::CaseInsensitive); };
188 set.children << logging;
189
165 syntax << set; 190 syntax << set;
166 191
167 Syntax get("get", QObject::tr("Gets settings for the session")); 192 Syntax get("get", QObject::tr("Gets settings for the session"));
168 get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); 193 get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel);
169 get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); 194 get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming);
195 get.children << Syntax("logging", QObject::tr("The current logging level"), &CoreSyntax::printLoggingLevel);
170 syntax << get; 196 syntax << get;
171 197
172 return syntax; 198 return syntax;
diff --git a/akonadish/utils.cpp b/akonadish/utils.cpp
new file mode 100644
index 0000000..d2a28ed
--- /dev/null
+++ b/akonadish/utils.cpp
@@ -0,0 +1,42 @@
1/*
2 * Copyright (C) 2016 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "utils.h"
21
22namespace Utils
23{
24
25QStringList filteredCompletions(const QStringList &possibleCompletions, const QString &commandFragment, Qt::CaseSensitivity cs)
26{
27 if (commandFragment.isEmpty()) {
28 return possibleCompletions;
29 }
30
31 QStringList filtered;
32 for (auto item: possibleCompletions) {
33 if (item.startsWith(commandFragment, cs)) {
34 filtered << item;
35 }
36 }
37
38 return filtered;
39}
40
41} // namespace Utils
42
diff --git a/akonadish/utils.h b/akonadish/utils.h
new file mode 100644
index 0000000..82be8d5
--- /dev/null
+++ b/akonadish/utils.h
@@ -0,0 +1,30 @@
1/*
2 * Copyright (C) 2016 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QStringList>
23
24namespace Utils
25{
26
27QStringList filteredCompletions(const QStringList &possibleCompletions, const QString &commandFragment, Qt::CaseSensitivity cs = Qt::CaseSensitive);
28
29} // namespace Utils
30
diff --git a/common/log.cpp b/common/log.cpp
index c33c700..489e1bd 100644
--- a/common/log.cpp
+++ b/common/log.cpp
@@ -96,7 +96,7 @@ static QString colorCommand(QList<int> colorCodes)
96 return string; 96 return string;
97} 97}
98 98
99QByteArray debugLevelName(DebugLevel debugLevel) 99QByteArray Akonadi2::Log::debugLevelName(DebugLevel debugLevel)
100{ 100{
101 switch (debugLevel) { 101 switch (debugLevel) {
102 case DebugLevel::Trace: 102 case DebugLevel::Trace:
@@ -114,15 +114,16 @@ QByteArray debugLevelName(DebugLevel debugLevel)
114 return QByteArray(); 114 return QByteArray();
115} 115}
116 116
117DebugLevel debugLevelFromName(const QByteArray &name) 117DebugLevel Akonadi2::Log::debugLevelFromName(const QByteArray &name)
118{ 118{
119 if (name.toLower() == "trace") 119 const QByteArray lowercaseName = name.toLower();
120 if (lowercaseName == "trace")
120 return DebugLevel::Trace; 121 return DebugLevel::Trace;
121 if (name.toLower() == "log") 122 if (lowercaseName == "log")
122 return DebugLevel::Log; 123 return DebugLevel::Log;
123 if (name.toLower() == "warning") 124 if (lowercaseName == "warning")
124 return DebugLevel::Warning; 125 return DebugLevel::Warning;
125 if (name.toLower() == "error") 126 if (lowercaseName == "error")
126 return DebugLevel::Error; 127 return DebugLevel::Error;
127 return DebugLevel::Log; 128 return DebugLevel::Log;
128} 129}
@@ -132,6 +133,11 @@ void Akonadi2::Log::setDebugOutputLevel(DebugLevel debugLevel)
132 qputenv("AKONADI2DEBUGLEVEL", debugLevelName(debugLevel)); 133 qputenv("AKONADI2DEBUGLEVEL", debugLevelName(debugLevel));
133} 134}
134 135
136Akonadi2::Log::DebugLevel Akonadi2::Log::debugOutputLevel()
137{
138 return debugLevelFromName(qgetenv("AKONADI2DEBUGLEVEL"));
139}
140
135QDebug Akonadi2::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) 141QDebug Akonadi2::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea)
136{ 142{
137 DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("AKONADI2DEBUGLEVEL")); 143 DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("AKONADI2DEBUGLEVEL"));
diff --git a/common/log.h b/common/log.h
index 9db9e8e..e531348 100644
--- a/common/log.h
+++ b/common/log.h
@@ -12,7 +12,11 @@ enum DebugLevel {
12 Error 12 Error
13}; 13};
14 14
15QByteArray debugLevelName(DebugLevel debugLevel);
16DebugLevel debugLevelFromName(const QByteArray &name);
17
15void setDebugOutputLevel(DebugLevel); 18void setDebugOutputLevel(DebugLevel);
19DebugLevel debugOutputLevel();
16 20
17QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); 21QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0);
18 22