DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1from ._daq_ers_py import *
2
3import sys
4import ers
5import inspect
6import platform
7import os
8import sys
9import re
10import time
11import getpass
12import _thread
13import logging
14
15class PyContext( object ):
16 "extracts context for an ERS issue"
17 __file = re.sub( r'\.pyc$', '.py', __file__ )
18
19 def __init__( self, issue ):
20 self.stack = [f[0] for f in inspect.stack() \
21 if f[1] != self.__file \
22 and ( 'self' not in f[0].f_locals\
23 or not isinstance(f[0].f_locals['self'], ers.PyIssue))]
24 self.package_name = issue.__class__.__module__
25 class_name = lambda : 'self' in self.stack[0].f_locals \
26 and self.stack[0].f_locals['self'].__class__.__name__ + '.'\
27 or ''
28
29 self.function_name = class_name() + self.stack[0].f_code.co_name;
30 try:
31 self.function_name += inspect.formatargvalues( *inspect.getargvalues(self.stack[0]) )
32 except Exception as exx:
33 self.function_name += '(...)'
34
35 self.file_name = self.stack[0].f_code.co_filename
36 self.line_number = self.stack[0].f_lineno
37 self.host_name = platform.node()
38 self.cwd = os.getcwd()
39 self.process_id = os.getpid()
40 # Need to truncate this to fit 32 bit signed int
41 self.thread_id = int(_thread.get_ident()%2**31)
42 self.user_id = os.getuid()
43 self.user_name = getpass.getuser()
44 self.application_name = os.getenv( "DUNEDAQ_APPLICATION_NAME", "Undefined" )
45
46
48 "base class for Python ERS exceptions"
49 __verbosity = int( os.getenv( "DUNEDAQ_ERS_VERBOSITY_LEVEL", "2" ) )
50
51 def __init__( self, message, kwargs, cause ):
52 Exception.__init__( self, message, kwargs, cause )
53 self.timetime = time.time()
54# self.severity = ers.severity.Error
55 self.messagemessage = message
56 self.causecause = cause
57 self.__context = PyContext(self)
58 self.context_parameters = vars(self.__context)
59 self.qualifiersqualifiers = [ self.__context.package_name ]
60 self.__parameters = dict([ (str(k), str(v)) for (k,v) in list(kwargs.items()) ])
61 self.__dict__.update( kwargs )
62 self.rpc = ers.RemoteProcessContext(self.__context.host_name,self.__context.process_id,self.__context.thread_id,
63 self.__context.cwd,self.__context.user_id,self.__context.user_name,
64 self.__context.application_name)
65 self.rc = ers.RemoteContext(self.__context.package_name,self.__context.file_name,self.__context.line_number,self.__context.function_name,self.rpc)
66 #self.lc = ers.LocalContext(self.__context.application_name, self.__context.file_name, self.__context.line_number, self.__context.function_name, False)
67 self.ai = ers.AnyIssue( self.__context.function_name, self.rc, message )
68
69 @property
70 def context( self ):
71 return self.__context
72
73 @property
74 def parameters( self ):
75 return self.__parameters
76
77 def isInstanceOf( self, cname ):
78 return self.__class__.__name__ == cname and True or False
79
80 def __str__( self ):
81 return self.messagemessage
82
83'''Wrap a message string as an ERS issue'''
84def message(message):
85 # This class collects information needed for message context
86 ct = PyContext(message)
87 # Still need to do something about these
88 package_name = "erspy"
89 type="Warning"
90 # Create Context using the ers-python bindings
91 rpc = ers.RemoteProcessContext(ct.host_name,ct.process_id,ct.thread_id,ct.cwd,ct.user_id,ct.user_name,ct.application_name)
92 rc = ers.RemoteContext(package_name,ct.file_name,ct.line_number,ct.function_name,rpc)
93 # Create and return an ers issue
94 return ers.AnyIssue(type, rc, message)
95
96
97'''Bunch of functions to wrap a text string and inject it as an ers message of different types'''
98def pydebug( msg, lvl ):
99 "sends msg to the debug stream"
100 ers.debug( isinstance( msg, ers.AnyIssue ) and msg or message( msg ), lvl )
101
102def pylog( msg ):
103 "sends msg to the log stream"
104 ers.log( isinstance( msg, ers.AnyIssue ) and msg or message( msg ) )
105
106def pyinfo( msg ):
107 "sends msg to the information stream"
108 ers.info( isinstance( msg, ers.AnyIssue ) and msg or message( msg ) )
109
110'''These just inject an issue into the more serious streams.
111 The only thing these add over just calling the bound ers function directly is that they
112 warn you if you use the wrong type of argument.
113 Mainly here because the old ers.py had them, and they provide a consistency of interface
114 rather than forcing the user to use these calls for some things and direct ers calls for others'''
115def pywarning( issue ):
116 "sends issue to the warning stream"
117 assert isinstance(issue,ers.AnyIssue), \
118 'Only an instance of ers.Issue sub-class can be sent to the ers.warning stream'
119 ers.warning( issue )
120
121def pyerror( issue ):
122 "sends issue to the error stream"
123 assert isinstance(issue, ers.AnyIssue), \
124 'Only an instance of ers.Issue sub-class can be sent to the ers.error stream'
125 ers.error( issue )
126
127def pyfatal( issue ):
128 "sends issue to the fatal stream"
129 assert isinstance(issue,ers.AnyIssue), \
130 'Only an instance of ers.Issue sub-class can be sent to the ers.fatal stream'
131 ers.fatal( issue )
Base class for any user define issue.
Definition Issue.hpp:69
const Context & context() const
Context of the issue.
Definition Issue.hpp:100
const std::vector< std::string > & qualifiers() const
return array of qualifiers
Definition Issue.hpp:106
const std::string & message() const
General cause of the issue.
Definition Issue.hpp:103
const string_map & parameters() const
return array of parameters
Definition Issue.hpp:109
std::string time(const std::string &format="%Y-%b-%d %H:%M:%S", bool isUTC=false) const
string representation of local time of the issue
Definition Issue.hpp:218
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:97
list line_number
Definition __init__.py:36
list file_name
Definition __init__.py:35
list function_name
Definition __init__.py:29
__init__(self, issue)
Definition __init__.py:19
__init__(self, message, kwargs, cause)
Definition __init__.py:51
isInstanceOf(self, cname)
Definition __init__.py:77
__str__(self)
Definition __init__.py:80
list qualifiers
Definition __init__.py:59
void warning(const Issue &issue)
Definition ers.hpp:115
void info(const Issue &issue)
Definition ers.hpp:95
void log(const Issue &issue)
Definition ers.hpp:102
pyinfo(msg)
Definition __init__.py:106
pyfatal(issue)
Definition __init__.py:127
pylog(msg)
Definition __init__.py:102
pywarning(issue)
Definition __init__.py:115
void debug(const Issue &issue, int level=debug_level())
Definition ers.hpp:74
pydebug(msg, lvl)
Definition __init__.py:98
message(message)
Definition __init__.py:84
void fatal(const Issue &issue)
Definition ers.hpp:88
pyerror(issue)
Definition __init__.py:121
void error(const Issue &issue)
Definition ers.hpp:81