Skip to content

Commit b2470ee

Browse files
committed
Full logging of the CLI behavior
Fixed #4
1 parent d9edbec commit b2470ee

1 file changed

Lines changed: 57 additions & 19 deletions

File tree

pytelemetrycli/cli.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from pytelemetrycli.ui.superplot import Superplot, PlotType
1111
from threading import Lock
1212
from pytelemetrycli.initialization import init_logging
13+
import logging
14+
from logging import getLogger
15+
16+
logger = getLogger('cli')
1317

1418
def docopt_cmd(func):
1519
def fn(self, arg):
@@ -77,16 +81,25 @@ def do_serial(self, arg):
7781
"""
7882
try:
7983
self.runner.disconnect()
80-
except:
81-
pass # Already disconnected
84+
logger.warn("User requested connect without desconnecting first.")
85+
except (IOError,AttributeError) as e:
86+
logger.warn("Already disconnected. Continuing happily. E : {0}"
87+
.format(e))
88+
pass
89+
8290
try:
8391
b = int(arg['--bauds'])
8492
self.runner.connect(arg['<port>'],b)
85-
except:
86-
print("Failed to connect to :",arg['<port>']," at ",b," (bauds)")
87-
print("Connection error : ",sys.exc_info())
93+
except IOError as e:
94+
print("Failed to connect to {0} at {1} (bauds)."
95+
.format(arg['<port>'],b))
96+
97+
logger.warn("Failed to connect to {0} at {1} (bauds). E : "
98+
.format(arg['<port>'],b,e))
8899
else:
89-
print("Connected to :",arg['<port>']," at ",b," (bauds)")
100+
s = "Connected to {0} at {1} (bauds).".format(arg['<port>'],b)
101+
print(s)
102+
logger.info(s)
90103

91104
@docopt_cmd
92105
def do_print(self, arg):
@@ -99,14 +112,28 @@ def do_print(self, arg):
99112
-a X, --amount X Amount of samples to display [default: 1]
100113
101114
"""
102-
if not self.topics.exists(arg['<topic>']):
103-
print("Topic '",arg['<topic>'],"' unknown. Type 'ls' to list all available")
115+
topic = arg['<topic>']
116+
if not self.topics.exists(topic):
117+
s = "Topic '{0}' unknown. Type 'ls' to list all available topics.".format(topic)
118+
print(s)
119+
logger.warn(s)
104120
return
105121

106-
s = self.topics.samples(arg['<topic>'],int(arg['--amount']))
122+
try:
123+
amount = int(arg['--amount'])
124+
except:
125+
s = "Could not cast --amount = '{0}' to integer. Using 1.".format(amount)
126+
print(s)
127+
logger.warn(s)
128+
amount = 1
129+
130+
s = self.topics.samples(topic,amount)
131+
107132
if s is not None:
108133
for i in s:
109134
print(i)
135+
else:
136+
logger.error("Could not retrieve {0} sample(s) under topic '{1}'.".format(amount,topic))
110137

111138
@docopt_cmd
112139
def do_ls(self, arg):
@@ -139,11 +166,15 @@ def do_plot(self, arg):
139166
topic = arg['<topic>']
140167

141168
if not self.topics.exists(topic):
142-
print("Topic ",topic," unknown.")
169+
s = "Topic '{0}' unknown. Type 'ls' to list all available topics.".format(topic)
170+
print(s)
171+
logger.warn(s)
143172
return
144173

145174
if self.topics.intransfer(topic):
146-
print("Topic already plotted.")
175+
s = "Topic '{0}' already plotting.".format(topic)
176+
print(s)
177+
logger.warn(s)
147178
return
148179

149180
plotTypeFlag = self.topics.xytype(arg['<topic>'])
@@ -169,7 +200,9 @@ def do_plot(self, arg):
169200

170201
self.topics.transfer(topic,q)
171202

172-
print("Plotting:", topic,' in mode [',plotTypeFlag,']')
203+
s = "Plotting '{0}' in mode [{1}].".format(topic,plotTypeFlag)
204+
logger.info(s)
205+
print(s)
173206

174207
@docopt_cmd
175208
def do_pub(self, arg):
@@ -192,14 +225,17 @@ def do_pub(self, arg):
192225
valtype = self.types_lookup[i]
193226

194227
if not valtype:
195-
print("Impossible to identify type of payload. pub cancelled.")
196-
print(arg)
228+
logger.error(
229+
"Payload type [{0}] unkown."
230+
.format(arg))
197231
return
198232

199-
print("Published on |",arg['<topic>'],"|",arg['<value>'],"[",valtype,"]")
200-
201233
self.telemetry.publish(arg['<topic>'],arg['<value>'],valtype)
202234

235+
s = "Published on topic '{0}' : {1} [{2}]".format(arg['<topic>'], arg['<value>'],valtype)
236+
print(s)
237+
logger.info(s)
238+
203239
@docopt_cmd
204240
def do_count(self, arg):
205241
"""
@@ -208,7 +244,7 @@ def do_count(self, arg):
208244
Usage: count
209245
"""
210246
for topic in self.topics.ls():
211-
print(topic,":",self.topics.count(topic))
247+
print("{0} : {1}".format(topic, self.topics.count(topic)))
212248

213249
@docopt_cmd
214250
def do_disconnect(self, arg):
@@ -220,8 +256,9 @@ def do_disconnect(self, arg):
220256
try:
221257
self.runner.disconnect()
222258
print("Disconnected.")
259+
logger.info("Disconnected.")
223260
except:
224-
print("Already disconnected.")
261+
logger.warn("Already disconnected. Continuing happily.")
225262

226263
def do_quit(self, arg):
227264
"""
@@ -230,7 +267,8 @@ def do_quit(self, arg):
230267
Usage: quit
231268
"""
232269
self.runner.terminate()
233-
print('Good Bye!')
270+
print("Good Bye!")
271+
logger.info("Application quit.")
234272
exit()
235273

236274
# Main function to start from script or from entry point

0 commit comments

Comments
 (0)