handle_error.py :  » Template-Engines » Clearsilver » clearsilver-0.10.5 » python » examples » base » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Clearsilver 
Clearsilver » clearsilver 0.10.5 » python » examples » base » handle_error.py

import traceback, sys, string, time, socket, os
import who_calls

#DUMP_DIR = "/neo/data/bugs"
DUMP_DIR = "/tmp/bugs"

Warning = "handle_error.Warning"

# levels
LV_MESSAGE = "LV_MESSAGE"
LV_WARNING = "LV_WARNING"
LV_ERROR   = "LV_ERROR"

Count = 0

gErrorCount = 0
DISABLE_DUMP = 0

def exceptionReason():
  return "%s.%s" % (str(sys.exc_type), str(sys.exc_value))

def exceptionString():
  tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
  return string.join(tb_list,"")

  #### old way
  import StringIO
  ## get the traceback message  
  sfp = StringIO.StringIO()
  traceback.print_exc(file=sfp)
  exception = sfp.getvalue()
  sfp.close()

  return exception


def handleException (msg=None, lvl=LV_ERROR, dump = 1):
  global gErrorCount
  gErrorCount = gErrorCount + 1

  tb_list = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
  if msg:
    sys.stderr.write ("%s\n" % msg)
  else:
    msg = "Unhandled Exception"
        
  sys.stderr.write (string.join(tb_list,""))
  try:
    if dump: dump_bug(lvl, "handleException", msg, string.join(tb_list, ""))
  except:
    handleException("Unable to dump_bug", dump = 0)

def handleWarning (msg=""):
  header = "*** handleWarning: %s\n" % msg
  sys.stderr.write(header)
  tb = who_calls.pretty_who_calls(strip=1) + "\n"
  sys.stderr.write(tb)

  try:
    dump_bug(LV_WARNING, "handleException", msg, tb)
  except:
    handleException("Unable to dump_bug", dump = 0)

def checkPaths():
  paths = (DUMP_DIR, 
           os.path.join (DUMP_DIR, "tmp"),
           os.path.join (DUMP_DIR, "new"))
  for path in paths:
    if not os.path.isdir(path):
      os.mkdir(path, 0755)


def dump_bug (level, etype, msg, location=None, nhdf=None):
    global DISABLE_DUMP
    if DISABLE_DUMP: return

    now = int(time.time())
    pid = os.getpid()

    import neo_cgi, neo_util
    hdf = neo_util.HDF()
    hdf.setValue("Required.Level", level)
    hdf.setValue("Required.When", str(int(time.time())))
    hdf.setValue("Required.Type", etype)
    hdf.setValue("Required.Title", msg)
    hdf.setValue("Optional.Hostname", socket.gethostname())
    if location:
        hdf.setValue("Optional.Location", location)

    for (key, value) in os.environ.items():
        hdf.setValue ("Environ.%s" % key, value)

    global Count
    Count = Count + 1
    fname = "%d.%d_%d.%s" % (now, pid, Count, socket.gethostname())
    checkPaths()

    tpath = os.path.join (DUMP_DIR, "tmp", fname)
    npath = os.path.join (DUMP_DIR, "new", fname)
    hdf.writeFile(tpath)
    os.rename(tpath, npath)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.