~ubuntu-branches/ubuntu/precise/fofix-dfsg/precise

« back to all changes in this revision

Viewing changes to src/Debug.py

  • Committer: Bazaar Package Importer
  • Author(s): Christian Hammers
  • Date: 2010-02-21 12:09:32 UTC
  • Revision ID: james.westby@ubuntu.com-20100221120932-6bh992d2u8dtj9gr
Tags: upstream-3.121
Import upstream version 3.121

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#####################################################################
 
2
# -*- coding: iso-8859-1 -*-                                        #
 
3
#                                                                   #
 
4
# Frets on Fire                                                     #
 
5
# Copyright (C) 2006 Sami Ky�stil�                                  #
 
6
#                                                                   #
 
7
# This program is free software; you can redistribute it and/or     #
 
8
# modify it under the terms of the GNU General Public License       #
 
9
# as published by the Free Software Foundation; either version 2    #
 
10
# of the License, or (at your option) any later version.            #
 
11
#                                                                   #
 
12
# This program is distributed in the hope that it will be useful,   #
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of    #
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     #
 
15
# GNU General Public License for more details.                      #
 
16
#                                                                   #
 
17
# You should have received a copy of the GNU General Public License #
 
18
# along with this program; if not, write to the Free Software       #
 
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        #
 
20
# MA  02110-1301, USA.                                              #
 
21
#####################################################################
 
22
 
 
23
from OpenGL.GL import *
 
24
from View import Layer
 
25
 
 
26
import gc
 
27
import threading
 
28
import Log
 
29
import Version
 
30
import os
 
31
import datetime
 
32
import zipfile
 
33
import Theme
 
34
import Stage
 
35
 
 
36
class DebugLayer(Layer):
 
37
  """A layer for showing some debug information."""
 
38
  def __init__(self, engine):
 
39
    self.engine = engine
 
40
    #gc.set_debug(gc.DEBUG_LEAK)
 
41
 
 
42
  def className(self, instance):
 
43
    return str(instance.__class__).split(".")[1]
 
44
  
 
45
  def render(self, visibility, topMost):
 
46
    self.engine.view.setOrthogonalProjection(normalize = True)
 
47
    
 
48
    try:
 
49
      font = self.engine.data.font
 
50
      scale = 0.0008
 
51
      glColor3f(.25, 1, .25)
 
52
 
 
53
      x, y = (.05, .05)
 
54
      h = font.getHeight() * scale
 
55
      
 
56
      font.render("Tasks:", (x, y), scale = scale)
 
57
      for task in self.engine.tasks + self.engine.frameTasks:
 
58
        font.render(self.className(task), (x + .1, y), scale = scale)
 
59
        y += h
 
60
        
 
61
      x, y = (.5, .05)
 
62
      font.render("Layers:", (x, y), scale = scale)
 
63
      for layer in self.engine.view.layers + self.engine.view.incoming + self.engine.view.outgoing + self.engine.view.visibility.keys():
 
64
        font.render(self.className(layer), (x + .1, y), scale = scale)
 
65
        y += h
 
66
        
 
67
      x, y = (.05, .4)
 
68
      font.render("Scenes:", (x, y), scale = scale)
 
69
      if "world" in dir(self.engine.server):
 
70
        for scene in self.engine.server.world.scenes:
 
71
          font.render(self.className(scene), (x + .1, y), scale = scale)
 
72
          y += h
 
73
        
 
74
      x, y = (.5, .4)
 
75
      font.render("Loaders:", (x, y), scale = scale)
 
76
      for loader in self.engine.resource.loaders:
 
77
        font.render(str(loader), (x + .1, y), scale = scale)
 
78
        y += h
 
79
        
 
80
      x, y = (.5, .55)
 
81
      font.render("Input:", (x, y), scale = scale)
 
82
      for listener in self.engine.input.mouseListeners + \
 
83
                      self.engine.input.keyListeners + \
 
84
                      self.engine.input.systemListeners + \
 
85
                      self.engine.input.priorityKeyListeners:
 
86
        font.render(self.className(listener), (x + .1, y), scale = scale)
 
87
        y += h
 
88
        
 
89
      x, y = (.05, .55)
 
90
      font.render("System:", (x, y), scale = scale)
 
91
      font.render("%d threads" % threading.activeCount(), (x + .1, y), scale = scale)
 
92
      y += h
 
93
      font.render("%.2f fps" % self.engine.fpsEstimate, (x + .1, y), scale = scale)
 
94
      y += h
 
95
      font.render("%d sessions, server %s" % (len(self.engine.sessions), self.engine.server and "on" or "off"), (x + .1, y), scale = scale)
 
96
      #y += h
 
97
      #font.render("%d gc objects" % len(gc.get_objects()), (x + .1, y), scale = scale)
 
98
      #y += h
 
99
      #font.render("%d collected" % gc.collect(), (x + .1, y), scale = scale)
 
100
 
 
101
    finally:
 
102
      self.engine.view.resetProjection()
 
103
 
 
104
  def gcDump(self):
 
105
    import World
 
106
    before = len(gc.get_objects())
 
107
    coll   = gc.collect()
 
108
    after  = len(gc.get_objects())
 
109
    Log.debug("%d GC objects collected, total %d -> %d." % (coll, before, after))
 
110
    fn = "gcdump.txt"
 
111
    f = open(fn, "w")
 
112
    n = 0
 
113
    gc.collect()
 
114
    for obj in gc.garbage:
 
115
      try:
 
116
        print >>f, obj
 
117
        n += 1
 
118
      except:
 
119
        pass
 
120
    f.close()
 
121
    Log.debug("Wrote a dump of %d GC garbage objects to %s." % (n, fn))
 
122
 
 
123
  def debugOut(self, engine):
 
124
    try:
 
125
      f = open("debug.log", "w+")
 
126
    except IOError:
 
127
      # fallback for unix (games dir read-only)
 
128
      import Resource
 
129
      # evilynux - Under MacOS X, put the logs in ~/Library/Logs
 
130
      if( os.uname()[0] == "Darwin" ):
 
131
        logFile = open(os.path.join(Resource.getWritableResourcePath(), 
 
132
                                    "..", "..", "Logs", Version.appName(),
 
133
                                    "debug.log"), "w+")
 
134
      else: # GNU/Linux et al.
 
135
        f = open(os.path.join(Resource.getWritableResourcePath(), 'debug.log'), "w+")
 
136
    version = Version.version()
 
137
    currentDir = os.getcwd()
 
138
    dataDir = Version.dataPath()
 
139
    translationDir = dataDir + "/translations"
 
140
    modsDir = dataDir + "/mods"
 
141
 
 
142
    f.write("Date = %s\n" % datetime.datetime.now())   
 
143
    f.write("\nVersion = %s\n" %  version)
 
144
    f.write("\nOS = %s\n" % os.name)
 
145
 
 
146
    f.write("\nCurrent Directory = %s\n" % currentDir)
 
147
    self.directoryList(f, currentDir)
 
148
 
 
149
    f.write("\nData Directory = %s\n" % dataDir)
 
150
    self.directoryList(f, dataDir)
 
151
 
 
152
    zip_path = os.path.join(dataDir, 'library.zip')
 
153
    # no library.zip on regular unix installation
 
154
    if os.path.exists(zip_path):
 
155
      f.write("\nLibrary.zip\n")
 
156
      zip = zipfile.ZipFile(zip_path, 'r')
 
157
      for info in zip.infolist():
 
158
        fileName = info.filename
 
159
        fileCSize = info.compress_size
 
160
        fileSize = info.file_size
 
161
        fileDate = datetime.datetime(*(info.date_time))
 
162
        f.write("%s, %s, %s, %s\n" % (fileName, fileCSize, fileSize, fileDate))
 
163
    
 
164
    f.write("\nTranslation Directory = %s\n" % translationDir)
 
165
    self.directoryList(f, translationDir)
 
166
    
 
167
    f.write("\nMods Directory = %s\n" % modsDir)
 
168
    self.directoryList(f, modsDir)
 
169
 
 
170
    mods = os.listdir(modsDir)
 
171
 
 
172
    for mod in mods:
 
173
      modDir = os.path.join(modsDir, mod)
 
174
      if os.path.isdir(modDir):
 
175
        f.write("\nMod Directory = %s\n" % modDir)
 
176
        self.directoryList(f, modDir)
 
177
 
 
178
    f.write("\nFretsonfire.ini\n")   
 
179
    engine.config.config.write(f)
 
180
 
 
181
    # No write() in Theme
 
182
    #f.write("\nTheme.ini\n")
 
183
    #Theme.write(f, engine.config)
 
184
 
 
185
    f.write("\nStage.ini\n")
 
186
    stage = Stage.Stage(self, self.engine.resource.fileName("stage.ini"))
 
187
    stage.config.write(f)
 
188
    f.close()
 
189
 
 
190
  def directoryList(self, f, root):
 
191
    files = os.listdir(root)
 
192
    
 
193
    for fileName in files:
 
194
      fileSize = os.path.getsize(os.path.join(root, fileName))
 
195
      mTime = datetime.datetime.utcfromtimestamp(os.path.getmtime(os.path.join(root, fileName)))
 
196
      cTime = datetime.datetime.utcfromtimestamp(os.path.getctime(os.path.join(root, fileName)))
 
197
      aTime = datetime.datetime.utcfromtimestamp(os.path.getatime(os.path.join(root, fileName)))
 
198
 
 
199
      f.write("%s, %s, %s, %s, %s\n" % (fileName, fileSize, mTime, cTime, aTime))