Skip to content

Commit

Permalink
add verbose logging option
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 2, 2020
1 parent f4bdcff commit 6df29db
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
28 changes: 20 additions & 8 deletions chatbot/python/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
from tinode_grpc import pb
from tinode_grpc import pbx

# For compatibility with python2
if sys.version_info[0] >= 3:
unicode = str

APP_NAME = "Tino-chatbot"
APP_VERSION = "1.2.0"
LIB_VERSION = pkg_resources.get_distribution("tinode_grpc").version
Expand All @@ -51,11 +55,20 @@ def add_future(tid, bundle):
onCompletion[tid] = bundle

# Shorten long strings for logging.
class JsonHelper(json.JSONEncoder):
def default(self, obj):
if type(obj) == str and len(obj) > MAX_LOG_LEN:
return '<' + len(obj) + ', bytes: ' + obj[:12] + '...' + obj[-12:] + '>'
return super(JsonHelper, self).default(obj)
def clip_long_string(obj):
if isinstance(obj, unicode) or isinstance(obj, str):
if len(obj) > MAX_LOG_LEN:
return '<' + str(len(obj)) + ' bytes: ' + obj[:12] + '...' + obj[-12:] + '>'
return obj
elif isinstance(obj, (list, tuple)):
return [clip_long_string(item) for item in obj]
elif isinstance(obj, dict):
return dict((key, clip_long_string(val)) for key, val in obj.items())
else:
return obj

def to_json(msg):
return json.dumps(clip_long_string(MessageToDict(msg)))

# Resolve or reject the future
def exec_future(tid, code, text, params):
Expand Down Expand Up @@ -144,7 +157,7 @@ def client_generate():
msg = queue_out.get()
if msg == None:
return
log("out:", json.dumps(MessageToDict(msg), cls=JsonHelper))
log("out:", to_json(msg)
yield msg

def client_post(msg):
Expand Down Expand Up @@ -237,8 +250,7 @@ def client_message_loop(stream):
try:
# Read server responses
for msg in stream:
log(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],
"in:", json.dumps(MessageToDict(msg), cls=JsonHelper))
log(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], "in:", to_json(msg))

if msg.HasField("ctrl"):
# Run code on command completion
Expand Down
24 changes: 20 additions & 4 deletions tn-cli/tn-cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

"""Python implementation of Tinode command line client using gRPC."""

Expand Down Expand Up @@ -27,8 +28,6 @@
import threading
import time

from google.protobuf import json_format

# Import generated grpc modules
from tinode_grpc import pb
from tinode_grpc import pbx
Expand All @@ -37,6 +36,7 @@
from tn_globals import printerr
from tn_globals import printout
from tn_globals import stdoutln
from tn_globals import to_json

APP_NAME = "tn-cli"
APP_VERSION = "1.4.0"
Expand Down Expand Up @@ -821,15 +821,21 @@ def gen_message(scheme, secret, args):
tn_globals.InputThread.daemon = True
tn_globals.InputThread.start()

yield hiMsg(id)
msg = hiMsg(id)
if tn_globals.Verbose:
stdoutln("\r=> " + to_json(msg))
yield msg

if scheme != None:
id += 1
login = lambda:None
setattr(login, 'scheme', scheme)
setattr(login, 'secret', secret)
setattr(login, 'cred', None)
yield loginMsg(id, login, args)
msg = loginMsg(id, login, args)
if tn_globals.Verbose:
stdoutln("\r=> " + to_json(msg))
yield msg

print_prompt = True

Expand Down Expand Up @@ -859,6 +865,8 @@ def gen_message(scheme, secret, args):
tn_globals.WaitingFor = cmd

if not hasattr(cmd, 'no_yield'):
if tn_globals.Verbose:
stdoutln("\r=> " + to_json(pbMsg))
yield pbMsg

elif not tn_globals.OutputQueue.empty():
Expand Down Expand Up @@ -920,6 +928,9 @@ def run(args, schema, secret):

# Read server responses
for msg in stream:
if tn_globals.Verbose:
stdoutln("\r<= " + to_json(msg))

if msg.HasField("ctrl"):
handle_ctrl(msg.ctrl)

Expand Down Expand Up @@ -1044,12 +1055,17 @@ def print_server_params(params):
parser.add_argument('--api-key', default='AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K', help='API key for file uploads')
parser.add_argument('--load-macros', default='./macros.py', help='path to macro module to load')
parser.add_argument('--version', action='store_true', help='print version')
parser.add_argument('--verbose', action='store_true', help='verbose output: print full JSON representation of messages')

args = parser.parse_args()

if args.version:
printout(version)
exit()

if args.verbose:
tn_globals.Verbose = True

printout(purpose)
printout("Secure server" if args.ssl else "Server", "at '"+args.host+"'",
"SNI="+args.ssl_host if args.ssl_host else "")
Expand Down
33 changes: 33 additions & 0 deletions tn-cli/tn_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
# To make print() compatible between p2 and p3
from __future__ import print_function

import json
import sys
from collections import deque
from google.protobuf.json_format import MessageToDict
try:
import Queue as queue
except ImportError:
import queue

if sys.version_info[0] >= 3:
# for compatibility with python2
unicode = str

# Dictionary wich contains lambdas to be executed when server {ctrl} response is received.
OnCompletion = {}

Expand All @@ -35,6 +41,9 @@
# Variables: results of command execution
Variables = {}

# Flag to enable extended logging. Useful for debugging.
Verbose = False

# Print prompts in interactive mode only.
def printout(*args):
if IsInteractive:
Expand Down Expand Up @@ -65,3 +74,27 @@ def stdout(*args):
def stdoutln(*args):
args = args + ("\n",)
stdout(*args)

# Shorten long strings for logging.
def clip_long_string(obj):
if isinstance(obj, str) or isinstance(obj, unicode):
if len(obj) > 64:
return '<' + str(len(obj)) + ' bytes: ' + obj[:12] + '...' + obj[-12:] + '>'
return obj
elif isinstance(obj, (list, tuple)):
return [clip_long_string(item) for item in obj]
elif isinstance(obj, dict):
return dict((key, clip_long_string(val)) for key, val in obj.items())
else:
return obj

# Convert protobuff message to json. Shorten very long strings.
def to_json(msg):
if not msg:
return 'null'
try:
return json.dumps(clip_long_string(MessageToDict(msg)))
except Exception as err:
stdoutln("Exception: {}".format(err))

return 'exception'

0 comments on commit 6df29db

Please sign in to comment.