OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging, os, re |
| 6 import common |
| 7 from constants import CLEANUP_LOGS_PAUSED_FILE |
| 8 from autotest_lib.client.bin import utils |
| 9 from autotest_lib.client.common_lib import error |
| 10 |
| 11 class LogReader(object): |
| 12 """ |
| 13 A class to read system log files. |
| 14 """ |
| 15 |
| 16 def __init__(self, filename='/var/log/messages'): |
| 17 self._start_line = 1 |
| 18 self._filename = filename |
| 19 if not os.path.exists(CLEANUP_LOGS_PAUSED_FILE): |
| 20 raise error.TestError('LogReader created without ' + |
| 21 CLEANUP_LOGS_PAUSED_FILE) |
| 22 |
| 23 |
| 24 def set_start_by_regexp(self, index, regexp): |
| 25 """Set the start of logs based on a regular expression. |
| 26 |
| 27 @param index: line matching regexp to start at, earliest log at 0. |
| 28 Negative numbers indicate matches since end of log. |
| 29 """ |
| 30 regexp_compiled = re.compile(regexp) |
| 31 file_handle = open(self._filename, 'r') |
| 32 starts = [] |
| 33 line_number = 1 |
| 34 for line in file_handle: |
| 35 if regexp_compiled.match(line): |
| 36 starts.append(line_number) |
| 37 line_number += 1 |
| 38 if index < -len(starts): |
| 39 self._start_line = 1 |
| 40 elif index >= len(starts): |
| 41 self.set_start_by_current() |
| 42 else: |
| 43 self._start_line = starts[index] |
| 44 |
| 45 |
| 46 def set_start_by_reboot(self, index): |
| 47 """ Set the start of logs (must be system log) based on reboot. |
| 48 |
| 49 @param index: reboot to start at, earliest log at 0. Negative |
| 50 numbers indicate reboots since end of log. |
| 51 """ |
| 52 return self.set_start_by_regexp(index, |
| 53 r'.*000\] Linux version \d') |
| 54 |
| 55 |
| 56 def set_start_by_current(self, relative=1): |
| 57 """ Set start of logs based on current last line. |
| 58 |
| 59 @param relative: line relative to current to start at. 1 means |
| 60 to start the log after this line. |
| 61 """ |
| 62 lines = utils.system_output('wc -l %s' % self._filename) |
| 63 self._start_line = int(lines.split(' ')[0]) + relative |
| 64 |
| 65 |
| 66 def get_logs(self): |
| 67 """ Get logs since the start line. |
| 68 |
| 69 Start line is set by set_start_* functions or |
| 70 since the start of the file if none were called. |
| 71 |
| 72 @return string of contents of file since start line. |
| 73 """ |
| 74 return utils.system_output('tail -n +%d %s' % |
| 75 (self._start_line, self._filename)) |
| 76 |
| 77 def can_find(self, string): |
| 78 """ Try to find string in the logs. |
| 79 |
| 80 @return boolean indicating if we found the string. |
| 81 """ |
| 82 return string in self.get_logs() |
| 83 |
| 84 |
| 85 class LogRotationPauser(object): |
| 86 """ |
| 87 Class to control when logs are rotated from either server or client. |
| 88 |
| 89 Assumes all setting of CLEANUP_LOGS_PAUSED_FILE is done by this class |
| 90 and that all calls to begin and end are properly |
| 91 nested. For instance, [ a.begin(), b.begin(), b.end(), a.end() ] is |
| 92 supported, but [ a.begin(), b.begin(), a.end(), b.end() ] is not. |
| 93 We do support redundant calls to the same class, such as |
| 94 [ a.begin(), a.begin(), a.end() ]. |
| 95 """ |
| 96 def __init__(self, host=None): |
| 97 self._host = host |
| 98 self._begun = False |
| 99 self._is_nested = True |
| 100 |
| 101 |
| 102 def _run(self, command, *args, **dargs): |
| 103 if self._host: |
| 104 return self._host.run(command, *args, **dargs).exit_status |
| 105 else: |
| 106 return utils.system(command, *args, **dargs) |
| 107 |
| 108 |
| 109 def begin(self): |
| 110 """Make sure that log rotation is disabled.""" |
| 111 if self._begun: |
| 112 return |
| 113 print "in begin " + str(self._begun) |
| 114 self._is_nested = (self._run(('[ -r %s ]' % |
| 115 CLEANUP_LOGS_PAUSED_FILE), |
| 116 ignore_status=True) == 0) |
| 117 print "in begin is nested: " + str(self._is_nested) |
| 118 if self._is_nested: |
| 119 print logging.__file__ |
| 120 logging.info('File %s was already present' % |
| 121 CLEANUP_LOGS_PAUSED_FILE) |
| 122 print 1 |
| 123 else: |
| 124 self._run('touch ' + CLEANUP_LOGS_PAUSED_FILE) |
| 125 print 2 |
| 126 self._begun = True |
| 127 |
| 128 |
| 129 def end(self): |
| 130 print "in end" + str(self._begun) |
| 131 assert self._begun |
| 132 if not self._is_nested: |
| 133 self._run('rm -f ' + CLEANUP_LOGS_PAUSED_FILE) |
| 134 else: |
| 135 logging.info('Leaving existing %s file' % CLEANUP_LOGS_PAUSED_FILE) |
| 136 self._begun = False |
OLD | NEW |