Please note that the CVS and issue trackers have moved to GitHub. These Trac pages are no longer kept up-to-date.

root/seattle/trunk/deploymentscripts/deploy_logging.py@5637

Revision 2520, 4.3 KB (checked in by konp, 10 years ago)

Committed the deployment scripts, just missing complete readme. Moved old files into /old before deletion.

Line 
1"""
2<Program Name>
3  deploy_logging.py
4
5<Started>
6  May 2009
7
8<Author>
9  n2k8000@u.washington.edu
10  Konstantin Pik
11
12<Purpose>
13  This is the file contains logging-related functions that are used by the
14  deployment script.  This file is not to be executed by itself, but is to
15  be used with other deploy_* modules.
16
17<Usage>
18  See deploy_main.py.
19 
20"""
21import deploy_main
22import make_summary
23import thread
24import os
25
26# Lock on the log file - since we are going to launch multiple threads
27# that'll want to log things, we need to be careful. This lock should
28# take care of that.
29filelock = thread.allocate_lock()
30
31# So I don't have to type this out in the log file each time for a separator.
32sep='------------------------------------------------------------------------'
33
34
35def print_to_log(description_string, out, err, return_code):
36  """
37  <Purpose>
38    helper that logs out, err, returncode to the proper files after formatting
39    the output correctly.
40   
41  <Arguments>
42    description_string:
43      A description string to put in front of the stdout (e.g.: "Info")
44    out:
45      stdout
46    err:
47      stderr
48    return_code:
49      the exit code of the call
50   
51   
52  <Exceptions>
53    None.
54
55  <Side Effects>
56    None.
57
58  <Returns>
59    No returns.
60  """
61 
62  out, err = format_stdout_and_err(out, err)
63 
64  if out:
65    log(description_string, out+' (exit code'+str(return_code)+')')
66  if err:
67    logerror(err+' (exit code'+str(return_code)+')')
68
69 
70
71def format_stdout_and_err(out, err):
72  """
73  <Purpose>
74    Helper that formats by removing trailing spaces and \n's, \t's, etc
75     
76  <Arguments>
77    out: a string, stdout by convention
78    err: a string, stderr by convection
79
80    Note: can be any two strings.
81   
82  <Exceptions>
83    None.
84
85  <Side Effects>
86    None.
87
88  <Returns>
89    No returns.
90  """
91 
92  out = out.strip('\n\t\r ')
93  err = err.strip('\n\t\r ')
94  return out, err
95
96 
97 
98def logerror(data):
99  """
100  <Purpose>
101    Wrapper to log to an error log file, and prints to console in case we're
102      running in near-silent mode (-v).
103     
104  <Arguments>
105    The error description/etc.
106   
107  <Exceptions>
108    None.
109
110  <Side Effects>
111    None.
112
113  <Returns>
114    No returns.
115  """
116 
117  # format data
118  data = str(data)
119  data = data.strip('\n ')
120  data = data.replace('\n', ' | ')
121  # don't want to OVERspam (this'll get printed if verbosity >=1)
122  if deploy_main.verbosity < 1 :
123    print 'ERROR:'+str(data)
124  log('ERROR:', data, 'deploy.err')
125
126 
127 
128 
129# Controller = person running the tests
130def log(identifier, data, fn='controller'):
131  """
132  <Purpose>
133    Logs to <fn> the <data> with <host> as the identifier for that line
134     
135  <Arguments>
136    identifier:
137      identifier in front of the line. Used to show "where" an event happend
138    data:
139      the string to log to file
140    fn:
141      Optional. Default is controller (and .log is always added a fn). Where
142      the log file is.
143   
144  <Exceptions>
145    Possible exception when can't open/write log file.
146
147  <Side Effects>
148    None.
149
150  <Returns>
151    No returns.
152  """
153
154  # multiple threads will try to write to this log file, gotta be careful
155  filelock.acquire()
156
157  try:
158    # if for some reason the logging directory does not exist, then create it
159    if not os.path.isdir('deploy.logs'):
160      os.mkdir('deploy.logs')
161   
162    # logfile will be put in the local directory
163    logfile = open('deploy.logs/'+fn+'.log', 'a')
164
165    # format data
166    data = str(data).strip('\n\r ')
167   
168    if data:
169      # if we have an identifier thenlog it with the time
170      if identifier:
171        logfile.write('\n'+deploy_main.get_current_time()+" | "+str(identifier)+":  "+data) 
172      else:
173        # if it's empty then log just the data, no time or anything.
174        # konp: FIXED 'no attribute err'
175        logfile.write('\n'+data)
176
177      # if verbose, print to console as well
178      if deploy_main.verbosity > 0:
179        print data
180
181  except Exception, e:
182    print "Error writing logfile ("+str(e)+")"
183  finally:
184    # close file and release lock
185    logfile.close()
186    filelock.release()
187
188
189   
190def build_summary():
191  """
192  <Purpose>
193    This function collects all the important log files from the subdirectories
194    and outputs them in a summary.log
195
196  <Arguments>
197    None.
198
199  <Exceptions>
200    None.
201
202  <Side Effects>
203    None.
204
205  <Returns>
206    None.
207  """
208
209  # make a call to make_summary.py
210  make_summary.build_summary()
Note: See TracBrowser for help on using the browser.