diff options
Diffstat (limited to 'sinksh/state.cpp')
-rw-r--r-- | sinksh/state.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/sinksh/state.cpp b/sinksh/state.cpp new file mode 100644 index 0000000..e03bf87 --- /dev/null +++ b/sinksh/state.cpp | |||
@@ -0,0 +1,144 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 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 "state.h" | ||
21 | |||
22 | #include <QCoreApplication> | ||
23 | #include <QDebug> | ||
24 | #include <QEventLoop> | ||
25 | #include <QTextStream> | ||
26 | |||
27 | #include "common/log.h" | ||
28 | |||
29 | static bool s_hasEventLoop = false; | ||
30 | |||
31 | class State::Private | ||
32 | { | ||
33 | public: | ||
34 | Private() | ||
35 | : outStream(stdout) | ||
36 | { | ||
37 | } | ||
38 | |||
39 | QEventLoop *eventLoop() | ||
40 | { | ||
41 | if (!event) { | ||
42 | event = new QEventLoop; | ||
43 | } | ||
44 | |||
45 | return event; | ||
46 | } | ||
47 | |||
48 | int debugLevel = 0; | ||
49 | QEventLoop *event = 0; | ||
50 | bool timing = false; | ||
51 | QTextStream outStream; | ||
52 | }; | ||
53 | |||
54 | State::State() | ||
55 | : d(new Private) | ||
56 | { | ||
57 | } | ||
58 | |||
59 | void State::print(const QString &message, unsigned int indentationLevel) const | ||
60 | { | ||
61 | for (unsigned int i = 0; i < indentationLevel; ++i) { | ||
62 | d->outStream << "\t"; | ||
63 | } | ||
64 | |||
65 | d->outStream << message; | ||
66 | } | ||
67 | |||
68 | void State::printLine(const QString &message, unsigned int indentationLevel) const | ||
69 | { | ||
70 | print(message, indentationLevel); | ||
71 | d->outStream << "\n"; | ||
72 | d->outStream.flush(); | ||
73 | } | ||
74 | |||
75 | void State::printError(const QString &errorMessage, const QString &errorCode) const | ||
76 | { | ||
77 | printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); | ||
78 | } | ||
79 | |||
80 | void State::setDebugLevel(unsigned int level) | ||
81 | { | ||
82 | if (level < 7) { | ||
83 | d->debugLevel = level; | ||
84 | } | ||
85 | } | ||
86 | |||
87 | unsigned int State::debugLevel() const | ||
88 | { | ||
89 | return d->debugLevel; | ||
90 | } | ||
91 | |||
92 | int State::commandStarted() const | ||
93 | { | ||
94 | if (!s_hasEventLoop) { | ||
95 | return QCoreApplication::exec(); | ||
96 | } else if (!d->eventLoop()->isRunning()) { | ||
97 | return d->eventLoop()->exec(); | ||
98 | } | ||
99 | |||
100 | return 0; | ||
101 | } | ||
102 | |||
103 | void State::commandFinished(int returnCode) const | ||
104 | { | ||
105 | if (!s_hasEventLoop) { | ||
106 | QCoreApplication::exit(returnCode); | ||
107 | } else { | ||
108 | d->eventLoop()->exit(returnCode); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | void State::setHasEventLoop(bool evented) | ||
113 | { | ||
114 | s_hasEventLoop = evented; | ||
115 | } | ||
116 | |||
117 | bool State::hasEventLoop() | ||
118 | { | ||
119 | return s_hasEventLoop; | ||
120 | } | ||
121 | |||
122 | void State::setCommandTiming(bool time) | ||
123 | { | ||
124 | d->timing = time; | ||
125 | } | ||
126 | |||
127 | bool State::commandTiming() const | ||
128 | { | ||
129 | return d->timing; | ||
130 | } | ||
131 | |||
132 | void State::setLoggingLevel(const QString &level) const | ||
133 | { | ||
134 | Sink::Log::setDebugOutputLevel(Sink::Log::debugLevelFromName(level.toLatin1())); | ||
135 | } | ||
136 | |||
137 | QString 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 Sink::Log | ||
141 | QByteArray rv = Sink::Log::debugLevelName(Sink::Log::debugOutputLevel()); | ||
142 | return rv.toLower(); | ||
143 | } | ||
144 | |||