Commit 68a27d05acd7e2e9100f7c3b2c27e106ddeb74d5

Authored by Christian Herdtweck
1 parent a025857f

record_base: use log_helper

Showing 1 changed file with 19 additions and 13 deletions
oletools/record_base.py
@@ -42,6 +42,7 @@ from __future__ import print_function @@ -42,6 +42,7 @@ from __future__ import print_function
42 # 2018-09-11 v0.54 PL: - olefile is now a dependency 42 # 2018-09-11 v0.54 PL: - olefile is now a dependency
43 # 2019-01-30 PL: - fixed import to avoid mixing installed oletools 43 # 2019-01-30 PL: - fixed import to avoid mixing installed oletools
44 # and dev version 44 # and dev version
  45 +# 2019-05-24 CH: - use log_helper
45 46
46 __version__ = '0.60.dev1' 47 __version__ = '0.60.dev1'
47 48
@@ -64,7 +65,6 @@ __version__ = '0.60.dev1' @@ -64,7 +65,6 @@ __version__ = '0.60.dev1'
64 import sys 65 import sys
65 import os.path 66 import os.path
66 from io import SEEK_CUR 67 from io import SEEK_CUR
67 -import logging  
68 68
69 import olefile 69 import olefile
70 70
@@ -74,6 +74,7 @@ PARENT_DIR = os.path.normpath(os.path.dirname(os.path.dirname( @@ -74,6 +74,7 @@ PARENT_DIR = os.path.normpath(os.path.dirname(os.path.dirname(
74 if PARENT_DIR not in sys.path: 74 if PARENT_DIR not in sys.path:
75 sys.path.insert(0, PARENT_DIR) 75 sys.path.insert(0, PARENT_DIR)
76 del PARENT_DIR 76 del PARENT_DIR
  77 +from oletools.common.log_helper import log_helper
77 78
78 79
79 ############################################################################### 80 ###############################################################################
@@ -100,8 +101,11 @@ ENTRY_TYPE2STR = { @@ -100,8 +101,11 @@ ENTRY_TYPE2STR = {
100 } 101 }
101 102
102 103
  104 +logger = log_helper.get_or_create_silent_logger('record_base')
  105 +
  106 +
103 def enable_olefile_logging(): 107 def enable_olefile_logging():
104 - """ enable logging olefile e.g., to get debug info from OleFileIO """ 108 + """ enable logging in olefile e.g., to get debug info from OleFileIO """
105 olefile.enable_logging() 109 olefile.enable_logging()
106 110
107 111
@@ -139,7 +143,7 @@ class OleRecordFile(olefile.OleFileIO): @@ -139,7 +143,7 @@ class OleRecordFile(olefile.OleFileIO):
139 143
140 def iter_streams(self): 144 def iter_streams(self):
141 """ find all streams, including orphans """ 145 """ find all streams, including orphans """
142 - logging.debug('Finding streams in ole file') 146 + logger.debug('Finding streams in ole file')
143 147
144 for sid, direntry in enumerate(self.direntries): 148 for sid, direntry in enumerate(self.direntries):
145 is_orphan = direntry is None 149 is_orphan = direntry is None
@@ -147,7 +151,7 @@ class OleRecordFile(olefile.OleFileIO): @@ -147,7 +151,7 @@ class OleRecordFile(olefile.OleFileIO):
147 # this direntry is not part of the tree --> unused or orphan 151 # this direntry is not part of the tree --> unused or orphan
148 direntry = self._load_direntry(sid) 152 direntry = self._load_direntry(sid)
149 is_stream = direntry.entry_type == olefile.STGTY_STREAM 153 is_stream = direntry.entry_type == olefile.STGTY_STREAM
150 - logging.debug('direntry {:2d} {}: {}'.format( 154 + logger.debug('direntry {:2d} {}: {}'.format(
151 sid, '[orphan]' if is_orphan else direntry.name, 155 sid, '[orphan]' if is_orphan else direntry.name,
152 'is stream of size {}'.format(direntry.size) if is_stream else 156 'is stream of size {}'.format(direntry.size) if is_stream else
153 'no stream ({})'.format(ENTRY_TYPE2STR[direntry.entry_type]))) 157 'no stream ({})'.format(ENTRY_TYPE2STR[direntry.entry_type])))
@@ -216,8 +220,8 @@ class OleRecordStream(object): @@ -216,8 +220,8 @@ class OleRecordStream(object):
216 220
217 # read first few bytes, determine record type and size 221 # read first few bytes, determine record type and size
218 rec_type, rec_size, other = self.read_record_head() 222 rec_type, rec_size, other = self.read_record_head()
219 - # logging.debug('Record type {0} of size {1}'  
220 - # .format(rec_type, rec_size)) 223 + # logger.debug('Record type {0} of size {1}'
  224 + # .format(rec_type, rec_size))
221 225
222 # determine what class to wrap this into 226 # determine what class to wrap this into
223 rec_clz, force_read = self.record_class_for_type(rec_type) 227 rec_clz, force_read = self.record_class_for_type(rec_type)
@@ -348,25 +352,25 @@ def test(filenames, ole_file_class=OleRecordFile, @@ -348,25 +352,25 @@ def test(filenames, ole_file_class=OleRecordFile,
348 if an error occurs while parsing a stream of type in must_parse, the error 352 if an error occurs while parsing a stream of type in must_parse, the error
349 will be raised. Otherwise a message is printed 353 will be raised. Otherwise a message is printed
350 """ 354 """
351 - logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) 355 + log_helper.enable_logging(False, 'debug' if verbose else 'info')
352 if do_per_record is None: 356 if do_per_record is None:
353 def do_per_record(record): # pylint: disable=function-redefined 357 def do_per_record(record): # pylint: disable=function-redefined
354 pass # do nothing 358 pass # do nothing
355 if not filenames: 359 if not filenames:
356 - logging.info('need file name[s]') 360 + logger.info('need file name[s]')
357 return 2 361 return 2
358 for filename in filenames: 362 for filename in filenames:
359 - logging.info('checking file {0}'.format(filename)) 363 + logger.info('checking file {0}'.format(filename))
360 if not olefile.isOleFile(filename): 364 if not olefile.isOleFile(filename):
361 - logging.info('not an ole file - skip') 365 + logger.info('not an ole file - skip')
362 continue 366 continue
363 ole = ole_file_class(filename) 367 ole = ole_file_class(filename)
364 368
365 for stream in ole.iter_streams(): 369 for stream in ole.iter_streams():
366 - logging.info(' parse ' + str(stream)) 370 + logger.info(' parse ' + str(stream))
367 try: 371 try:
368 for record in stream.iter_records(): 372 for record in stream.iter_records():
369 - logging.info(' ' + str(record)) 373 + logger.info(' ' + str(record))
370 do_per_record(record) 374 do_per_record(record)
371 except Exception: 375 except Exception:
372 if not must_parse: 376 if not must_parse:
@@ -374,7 +378,9 @@ def test(filenames, ole_file_class=OleRecordFile, @@ -374,7 +378,9 @@ def test(filenames, ole_file_class=OleRecordFile,
374 elif isinstance(stream, must_parse): 378 elif isinstance(stream, must_parse):
375 raise 379 raise
376 else: 380 else:
377 - logging.info(' failed to parse', exc_info=True) 381 + logger.info(' failed to parse', exc_info=True)
  382 +
  383 + log_helper.end_logging()
378 return 0 384 return 0
379 385
380 386