def CreateLogpoint(self, location, log_format_string, log_level=None, condition=None, user_email=None, labels=None): """Creates a logpoint in the debuggee. Args: location: The breakpoint source location, which will be interpreted by the debug agents on the machines running the Debuggee. Usually of the form file:line-number log_format_string: The message to log, optionally containin {expression}- style formatting. log_level: String (case-insensitive), one of 'info', 'warning', or 'error', indicating the log level that should be used for logging. condition: An optional conditional expression in the target's programming language. The snapshot will be taken when the expression is true. user_email: The email of the user who created the snapshot. labels: A dictionary containing key-value pairs which will be stored with the snapshot definition and reported when the snapshot is queried. Returns: The created Breakpoint message. Raises: InvalidLocationException: if location is empty or malformed. InvalidLogFormatException: if log_format is empty or malformed. """ if not location: raise errors.InvalidLocationException( 'The location must not be empty.') if not log_format_string: raise errors.InvalidLogFormatException( 'The log format string must not be empty.') labels_value = None if labels: labels_value = self._debug_messages.Breakpoint.LabelsValue( additionalProperties=[ self._debug_messages.Breakpoint.LabelsValue.AdditionalProperty( key=key, value=value) for key, value in six.iteritems(labels)]) location = self._LocationFromString(location) if log_level: log_level = ( self._debug_messages.Breakpoint.LogLevelValueValuesEnum( log_level.upper())) log_message_format, expressions = SplitLogExpressions(log_format_string) request = ( self._debug_messages. ClouddebuggerDebuggerDebuggeesBreakpointsSetRequest( debuggeeId=self.target_id, breakpoint=self._debug_messages.Breakpoint( location=location, condition=condition, logLevel=log_level, logMessageFormat=log_message_format, expressions=expressions, labels=labels_value, userEmail=user_email, action=(self._debug_messages.Breakpoint. ActionValueValuesEnum.LOG)), clientVersion=self.CLIENT_VERSION)) try: response = self._debug_client.debugger_debuggees_breakpoints.Set(request) except apitools_exceptions.HttpError as error: raise errors.UnknownHttpError(error) return self.AddTargetInfo(response.breakpoint)
def _LocationFromString(self, location): """Converts a file:line location string into a SourceLocation. Args: location: A string of the form file:line. Returns: The corresponding SourceLocation message. Raises: InvalidLocationException: if the line is not of the form path:line """ components = location.split(':') if len(components) != 2: raise errors.InvalidLocationException( 'Location must be of the form "path:line"') try: return self._debug_messages.SourceLocation(path=components[0], line=int(components[1])) except ValueError: raise errors.InvalidLocationException( 'Location must be of the form "path:line", where "line" must be an ' 'integer.')
def ListBreakpoints(self, location_regexp=None, resource_ids=None, include_all_users=False, include_inactive=False, restrict_to_type=None): """Returns all breakpoints matching the given IDs or patterns. Lists all breakpoints for this debuggee, and returns every breakpoint where the location field contains the given pattern or the ID is exactly equal to the pattern (there can be at most one breakpoint matching by ID). Args: location_regexp: A list of regular expressions to compare against the location ('path:line') of the breakpoints. If both location_regexp and resource_ids are empty or None, all breakpoints will be returned. resource_ids: Zero or more resource IDs in the form expected by the resource parser. These breakpoints will be retrieved regardless of the include_all_users or include_inactive flags include_all_users: If true, search breakpoints created by all users. include_inactive: If true, search breakpoints that are in the final state. This option controls whether regular expressions can match inactive breakpoints. If an object is specified by ID, it will be returned whether or not this flag is set. restrict_to_type: An optional breakpoint type (LOGPOINT_TYPE or SNAPSHOT_TYPE) Returns: A list of all matching breakpoints. Raises: InvalidLocationException if a regular expression is not valid. """ resource_ids = resource_ids or [] location_regexp = location_regexp or [] ids = set([ self._resource_parser.Parse( r, params={ 'debuggeeId': self.target_id }, collection='clouddebugger.debugger.debuggees.breakpoints'). Name() for r in resource_ids ]) patterns = [] for r in location_regexp: try: patterns.append(re.compile(r'^(.*/)?(' + r + ')$')) except re.error as e: raise errors.InvalidLocationException( 'The location pattern "{0}" is not a valid Python regular ' 'expression: {1}'.format(r, e)) request = (self._debug_messages. ClouddebuggerDebuggerDebuggeesBreakpointsListRequest( debuggeeId=self.target_id, includeAllUsers=include_all_users, includeInactive=include_inactive or bool(ids), clientVersion=self.CLIENT_VERSION)) try: response = self._debug_client.debugger_debuggees_breakpoints.List( request) except apitools_exceptions.HttpError as error: raise errors.UnknownHttpError(error) if not patterns and not ids: return self._FilteredDictListWithInfo(response.breakpoints, restrict_to_type) if include_inactive: # Match everything (including inactive breakpoints) against all ids and # patterns. result = [ bp for bp in response.breakpoints if _BreakpointMatchesIdOrRegexp(bp, ids, patterns) ] else: # Return everything that is listed by ID, plus every breakpoint that # is not inactive (i.e. isFinalState is false) which matches any pattern. # Breakpoints that are inactive should not be matched against the # patterns. result = [ bp for bp in response.breakpoints if _BreakpointMatchesIdOrRegexp( bp, ids, [] if bp.isFinalState else patterns) ] # Check if any ids were missing, and fetch them individually. This can # happen if an ID for another user's breakpoint was specified, but the # all_users flag was false. This code will also raise an error for any # missing IDs. missing_ids = ids - set([bp.id for bp in result]) if missing_ids: raise errors.BreakpointNotFoundError( missing_ids, self._BreakpointDescription(restrict_to_type)) # Verify that all patterns matched at least one breakpoint. for p in patterns: if not [ bp for bp in result if _BreakpointMatchesIdOrRegexp(bp, [], [p]) ]: raise errors.NoMatchError( self._BreakpointDescription(restrict_to_type), p.pattern) return self._FilteredDictListWithInfo(result, restrict_to_type)