Repository: pymedusa/Medusa

Issue:
Error while searching in viewlog (develop)
### Before submitting your issue:

Enable debug logging in SickRage settings, reproduce the error (be sure to disable after the bug is fixed)

Branch/Commit: develop
OS: windows
What you did: opened viewlog and typed something in searchbox
What happened: medusa gave error and froze on the viewlog screen
What you expected: not to get error supplied in logs
Logs:

```
PASTE LOGS HERE
```

https://0paste.com/9324-b348eb3b



Discussion:
2016-10-13 16:20:44 DEBUG    MAIN :: [ed345b5] Internal name cache for Haters Back Off set to: [ haters back off ]
2016-10-13 16:20:44 DEBUG    MAIN :: [ed345b5] Internal name cache for Zapped! set to: [ zapped ]
2016-10-13 16:20:44 DEBUG    MAIN :: [ed345b5] Internal name cache for Crazyhead set to: [ crazyhead ]
2016-10-13 16:20:44 DEBUG    MAIN :: [ed345b5] Internal name cache for Beck set to: [ beck ]
2016-10-13 16:20:44 DEBUG    MAIN :: [ed345b5] Finished updating network timezones
2016-10-13 16:21:24 ERROR    Thread-28 :: [ed345b5] Failed doing web ui callback: Traceback (most recent call last):
  File "C:\SickRageMedusa*******_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_******_\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>
Traceback (most recent call last):
  File "C:\SickRageMedusa\**_****_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_******_\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>
2016-10-13 16:21:24 DEBUG    TORNADO :: [ed345b5] Failed doing web ui request u'viewlog': Traceback (most recent call last):
  File "C:\SickRageMedusa\**_******_\server\web\core\base.py", line 253, in get
    results = yield self.async_call(method)
  File "C:\SickRageMedusa\lib\tornado\gen.py", line 1008, in run
    value = future.result()
  File "C:\SickRageMedusa\lib\concurrent\futures_base.py", line 398, in result
    return self.__get_result()
  File "C:\SickRageMedusa\lib\concurrent\futures\thread.py", line 55, in run
    result = self.fn(_self.args, **self.kwargs)
  File "C:\SickRageMedusa\*******_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_*******\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>

2016-10-13 16:21:27 ERROR    Thread-30 :: [ed345b5] Failed doing web ui callback: Traceback (most recent call last):
  File "C:\SickRageMedusa*******_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_******_\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>
Traceback (most recent call last):
  File "C:\SickRageMedusa\**_****_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_******_\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>
2016-10-13 16:21:27 DEBUG    TORNADO :: [ed345b5] Failed doing web ui request u'viewlog': Traceback (most recent call last):
  File "C:\SickRageMedusa\**_******_\server\web\core\base.py", line 253, in get
    results = yield self.async_call(method)
  File "C:\SickRageMedusa\lib\tornado\gen.py", line 1008, in run
    value = future.result()
  File "C:\SickRageMedusa\lib\concurrent\futures_base.py", line 398, in result
    return self.__get_result()
  File "C:\SickRageMedusa\lib\concurrent\futures\thread.py", line 55, in run
    result = self.fn(_self.args, **self.kwargs)
  File "C:\SickRageMedusa\*******_**\server\web\core\base.py", line 271, in async_call
    result = function(__kwargs)
  File "C:\SickRageMedusa\_********_\server\web\core\error_logs.py", line 133, in viewlog
    predicate=lambda l: filter_logline(l, min_level=min_level,
  File "C:\SickRageMedusa\**_******_\logger.py", line 148, in read_loglines
    if predicate(logline):
  File "C:\SickRageMedusa\**_******_\server\web\core\error_logs.py", line 135, in <lambda>
    search_query=log_search))]
  File "C:\SickRageMedusa\**_*******\logger.py", line 237, in filter_logline
    if (not logline.message or search_query not in logline.message.lower()) and (
  File "C:\Python27\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 24: character maps to <undefined>

2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] Starting new thread: POSTPROCESSOR
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] Processing folder D:\DOWNLOAD\complete\TV
2016-10-13 16:30:14 INFO     POSTPROCESSOR :: [ed345b5] PostProcessing Path: D:\DOWNLOAD\complete\TV
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] PostProcessing Dirs: []
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] PostProcessing Files: []
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] PostProcessing VideoFiles: []
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] PostProcessing RarContent:
2016-10-13 16:30:14 DEBUG    POSTPROCESSOR :: [ed345b5] PostProcessing VideoInRar:
2016-10-13 16:30:14 INFO     POSTPROCESSOR :: [ed345b5] Successfully processed

Release: Would.I.Lie.to.You.S10E06.720p.WEB.h264-HEAT
Show Folder: Would I Lie To You
Show Title: "Would I Lie To You?"

@ratoaq2 is that an issue from removing ek?


Resolving patch:
diff --git a/medusa/logger.py b/medusa/logger.py
--- a/medusa/logger.py
+++ b/medusa/logger.py
@@ -183,7 +183,7 @@ def reverse_readlines(filename, buf_size=2097152, encoding=default_encoding):
     :return:
     :rtype: collections.Iterable of str
     """
-    new_line = b'\n'
+    new_line = '\n'
     with io.open(filename, 'rb') as fh:
         segment = None
         offset = 0
@@ -193,6 +193,7 @@ def reverse_readlines(filename, buf_size=2097152, encoding=default_encoding):
             offset = min(file_size, offset + buf_size)
             fh.seek(file_size - offset)
             buf = fh.read(min(remaining_size, buf_size))
+            buf = text_type(buf.decode(sys.getfilesystemencoding()) if os.name == 'nt' else buf, errors='replace')
             remaining_size -= buf_size
             lines = buf.split(new_line)
             # the first line of the buffer is probably not a complete line so
@@ -371,7 +372,7 @@ def from_line(cls, line):
         :return:
         :rtype: LogLine or None
         """
-        lines = line.split(b'\n')
+        lines = line.split('\n')
         match = LogLine.log_re.match(lines[0])
         if not match:
             return


Test patch:
diff --git a/tests/test_logger.py b/tests/test_logger.py
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -6,6 +6,8 @@
 from medusa.logger import DEBUG, INFO, LogLine, WARNING
 import pytest
 
+from six import text_type
+
 
 class TestStandardLoggingApi(object):
     @pytest.mark.parametrize('p', [
@@ -86,6 +88,9 @@ def test_reverse_readlines(create_file, line_pattern):
     no_lines = 10000
     filename = create_file(filename='samplefile.log', lines=[line_pattern.format(n=i) for i in range(0, no_lines)])
     expected = [line_pattern.format(n=no_lines - i - 1) for i in range(0, no_lines)]
+    for i, v in enumerate(expected):
+        if not isinstance(v, text_type):
+            expected[i] = text_type(v, errors='replace')
 
     # When
     actual = list(sut.reverse_readlines(filename, buf_size=1024))