Commit 68a27d05acd7e2e9100f7c3b2c27e106ddeb74d5
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 | 42 | # 2018-09-11 v0.54 PL: - olefile is now a dependency |
| 43 | 43 | # 2019-01-30 PL: - fixed import to avoid mixing installed oletools |
| 44 | 44 | # and dev version |
| 45 | +# 2019-05-24 CH: - use log_helper | |
| 45 | 46 | |
| 46 | 47 | __version__ = '0.60.dev1' |
| 47 | 48 | |
| ... | ... | @@ -64,7 +65,6 @@ __version__ = '0.60.dev1' |
| 64 | 65 | import sys |
| 65 | 66 | import os.path |
| 66 | 67 | from io import SEEK_CUR |
| 67 | -import logging | |
| 68 | 68 | |
| 69 | 69 | import olefile |
| 70 | 70 | |
| ... | ... | @@ -74,6 +74,7 @@ PARENT_DIR = os.path.normpath(os.path.dirname(os.path.dirname( |
| 74 | 74 | if PARENT_DIR not in sys.path: |
| 75 | 75 | sys.path.insert(0, PARENT_DIR) |
| 76 | 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 | 101 | } |
| 101 | 102 | |
| 102 | 103 | |
| 104 | +logger = log_helper.get_or_create_silent_logger('record_base') | |
| 105 | + | |
| 106 | + | |
| 103 | 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 | 109 | olefile.enable_logging() |
| 106 | 110 | |
| 107 | 111 | |
| ... | ... | @@ -139,7 +143,7 @@ class OleRecordFile(olefile.OleFileIO): |
| 139 | 143 | |
| 140 | 144 | def iter_streams(self): |
| 141 | 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 | 148 | for sid, direntry in enumerate(self.direntries): |
| 145 | 149 | is_orphan = direntry is None |
| ... | ... | @@ -147,7 +151,7 @@ class OleRecordFile(olefile.OleFileIO): |
| 147 | 151 | # this direntry is not part of the tree --> unused or orphan |
| 148 | 152 | direntry = self._load_direntry(sid) |
| 149 | 153 | is_stream = direntry.entry_type == olefile.STGTY_STREAM |
| 150 | - logging.debug('direntry {:2d} {}: {}'.format( | |
| 154 | + logger.debug('direntry {:2d} {}: {}'.format( | |
| 151 | 155 | sid, '[orphan]' if is_orphan else direntry.name, |
| 152 | 156 | 'is stream of size {}'.format(direntry.size) if is_stream else |
| 153 | 157 | 'no stream ({})'.format(ENTRY_TYPE2STR[direntry.entry_type]))) |
| ... | ... | @@ -216,8 +220,8 @@ class OleRecordStream(object): |
| 216 | 220 | |
| 217 | 221 | # read first few bytes, determine record type and size |
| 218 | 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 | 226 | # determine what class to wrap this into |
| 223 | 227 | rec_clz, force_read = self.record_class_for_type(rec_type) |
| ... | ... | @@ -348,25 +352,25 @@ def test(filenames, ole_file_class=OleRecordFile, |
| 348 | 352 | if an error occurs while parsing a stream of type in must_parse, the error |
| 349 | 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 | 356 | if do_per_record is None: |
| 353 | 357 | def do_per_record(record): # pylint: disable=function-redefined |
| 354 | 358 | pass # do nothing |
| 355 | 359 | if not filenames: |
| 356 | - logging.info('need file name[s]') | |
| 360 | + logger.info('need file name[s]') | |
| 357 | 361 | return 2 |
| 358 | 362 | for filename in filenames: |
| 359 | - logging.info('checking file {0}'.format(filename)) | |
| 363 | + logger.info('checking file {0}'.format(filename)) | |
| 360 | 364 | if not olefile.isOleFile(filename): |
| 361 | - logging.info('not an ole file - skip') | |
| 365 | + logger.info('not an ole file - skip') | |
| 362 | 366 | continue |
| 363 | 367 | ole = ole_file_class(filename) |
| 364 | 368 | |
| 365 | 369 | for stream in ole.iter_streams(): |
| 366 | - logging.info(' parse ' + str(stream)) | |
| 370 | + logger.info(' parse ' + str(stream)) | |
| 367 | 371 | try: |
| 368 | 372 | for record in stream.iter_records(): |
| 369 | - logging.info(' ' + str(record)) | |
| 373 | + logger.info(' ' + str(record)) | |
| 370 | 374 | do_per_record(record) |
| 371 | 375 | except Exception: |
| 372 | 376 | if not must_parse: |
| ... | ... | @@ -374,7 +378,9 @@ def test(filenames, ole_file_class=OleRecordFile, |
| 374 | 378 | elif isinstance(stream, must_parse): |
| 375 | 379 | raise |
| 376 | 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 | 384 | return 0 |
| 379 | 385 | |
| 380 | 386 | ... | ... |