Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature nowrap #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Features
- Aggregate streams matching a regular expression.

* Colored output.

- Highlight found text in logs

* List existing groups

- ``$ apilogs groups``
Expand All @@ -41,6 +43,11 @@ Features

- ``$ apilogs get /var/log/syslog ALL --watch``


* Truncate logs as they are shown

- ``$ apilogs get /var/log/syslog ALL --no-wrap``

* Human-friendly time filtering:

- ``--start='23/1/2015 14:23'``
Expand Down
10 changes: 8 additions & 2 deletions apilogs/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def add_date_range_arguments(parser):
action='store_true',
dest='watch',
help="Query for new log lines constantly")

get_parser.add_argument("-G",
"--no-group",
action='store_false',
Expand All @@ -120,7 +120,13 @@ def add_date_range_arguments(parser):
action='store_true',
dest='output_ingestion_time_enabled',
help="Add ingestion time to the output")


get_parser.add_argument("-T",
"--no-wrap",
action='store_true',
dest='truncate',
help="Do not wrap log lines in output, truncate to length of line.")

get_parser.add_argument("-H",
"--highlight",
action='append',
Expand Down
95 changes: 91 additions & 4 deletions apilogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, **kwargs):
self.filter_pattern = kwargs.get('filter_pattern')
self.highlight = kwargs.get('highlight')
self.watch = kwargs.get('watch')
self.truncate = kwargs.get('truncate')
self.color_enabled = kwargs.get('color_enabled')
self.output_stream_enabled = kwargs.get('output_stream_enabled')
self.output_group_enabled = kwargs.get('output_group_enabled')
Expand Down Expand Up @@ -228,10 +229,6 @@ def consumer():

# Strip any tail line feeds
message = event['message'].rstrip("\r\n")
if self.highlight:
for value in self.highlight:
if value and not value.isspace():
message = message.replace(value, self.color(value, 'blue', 'on_yellow'))
output = []
if self.output_group_enabled:
output.append(
Expand Down Expand Up @@ -262,6 +259,19 @@ def consumer():
'blue'
)
)
if self.truncate:
# Replace tabs and line feeds with spaces so we can calculate real length
message = message.replace('\t', ' ').replace('\r', '').replace('\n', ' ')
s = self.getTerminalSize()
# Find the length of the pefixed log info
ps = len(' '.join(output))
# Calculate what to truncate from message as long as prefix is not longer than current term column length
ts = (s[0] - ps) - 4 if ps < s[0] else (ps + len(message) + 1)
message = message[:ts] + (message[ts:] and '...')
if self.highlight:
for value in self.highlight:
if value and not value.isspace():
message = message.replace(value, self.color(value, 'blue', 'on_yellow'))

output.append(message)
print(' '.join(output))
Expand Down Expand Up @@ -390,3 +400,80 @@ def parse_datetime(self, datetime_text):
raise exceptions.UnknownDateError(datetime_text)

return int(total_seconds(date - datetime(1970, 1, 1))) * 1000

def getTerminalSize(self):
import platform
current_os = platform.system()
tuple_xy=None
if current_os == 'Windows':
tuple_xy = self._getTerminalSize_windows()
if tuple_xy is None:
tuple_xy = self._getTerminalSize_tput()
# needed for window's python in cygwin's xterm!
if current_os == 'Linux' or current_os == 'Darwin' or current_os.startswith('CYGWIN'):
tuple_xy = self._getTerminalSize_linux()
if tuple_xy is None:
tuple_xy = (80, 25) # default value
return tuple_xy

def _getTerminalSize_windows(self):
res=None
try:
from ctypes import windll, create_string_buffer

# stdin handle is -10
# stdout handle is -11
# stderr handle is -12

h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
except:
return None
if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
else:
return None

def _getTerminalSize_tput(self):
# get terminal width
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
try:
import subprocess
proc=subprocess.Popen(["tput", "cols"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
output=proc.communicate(input=None)
cols=int(output[0])
proc=subprocess.Popen(["tput", "lines"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
output=proc.communicate(input=None)
rows=int(output[0])
return (cols,rows)
except:
return None

def _getTerminalSize_linux(self):
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name='apilogs',
version='1.3',
version='1.4',
url='http://github.com/rpgreen/apilogs',
license='BSD',
author='Ryan Green',
Expand Down