ya2 · news · projects · code · about

removed GameObject
[pmachines.git] / ya2 / utils / log.py
CommitLineData
8ee66edd
FC
1from logging import basicConfig, info, INFO, DEBUG, getLogger
2from configparser import ConfigParser
3from sys import platform, argv
8ce16d6c
FC
4from platform import system
5from pathlib import Path
6from glob import glob
e65a09cf 7# from datetime import datetime
8ee66edd
FC
8from pprint import pprint
9from os import getcwd, environ
e65a09cf 10from os.path import exists
8ee66edd
FC
11from traceback import print_stack
12from sys import version_info
13# from platform import system, release, architecture, platform, processor, \
14# version, machine
15# from multiprocessing import cpu_count
8ce16d6c
FC
16from panda3d.core import Filename, GraphicsWindow, PandaSystem
17from panda3d.bullet import get_bullet_version
b35b1f62 18from ya2.p3d.p3d import LibP3d
8ee66edd
FC
19
20
21lev = INFO
22opt_path = ''
23if platform in ['win32', 'linux'] and not exists('main.py'):
24 # it is the deployed version for windows
8ce16d6c 25 opt_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
8ee66edd
FC
26opath = LibP3d.fixpath(opt_path + '/options.ini') if opt_path else \
27 'options.ini'
28if exists(opath):
29 with open(opath) as json_file:
e65a09cf 30 # optfile = load(json_file)
8ee66edd
FC
31 optfile = ConfigParser()
32 optfile.read(opath)
e65a09cf
FC
33 # optfile['development']['verbose']
34 # and int(optfile['development']['verbose']) or \
35 if optfile['development']['verbose_log'] and \
36 int(optfile['development']['verbose_log']):
8ee66edd
FC
37 lev = DEBUG
38
39basicConfig(level=lev, format='%(asctime)s %(message)s', datefmt='%H:%M:%S')
40getLogger().setLevel(lev) # it doesn't work otherwise
41
42
aed9737a 43class LogMgrBase: # headless log manager
8ee66edd
FC
44
45 @staticmethod
46 def init_cls():
47 return LogMgr if base.win else LogMgrBase
48
aed9737a 49 def __init__(self):
8ee66edd
FC
50 self.log_cfg()
51
52 def log(self, msg, verbose=False):
e65a09cf
FC
53 if verbose and not self.eng.cfg.dev_cfg.verbose_log:
54 return
8ee66edd
FC
55 info(msg)
56
8ce16d6c
FC
57 @property
58 def is_appimage(self):
59 par_path = str(Path(__file__).parent.absolute())
60 is_appimage = par_path.startswith('/tmp/.mount_Pmachi')
61 return is_appimage and par_path.endswith('/usr/bin')
62
dd32d640
FC
63 # @property
64 # def curr_path(self):
65 # # this is different from the music's one since it does not work
66 # # with the version in windows
67 # if sys.platform == 'darwin':
68 # return dirname(__file__) + '/../Resources/'
69 # # return dirname(__file__)
70 # par_path = str(Path(__file__).parent.absolute())
71 # if self.is_appimage:
72 # return str(Path(par_path).absolute())
73 # is_snap = par_path.startswith('/snap/')
74 # is_snap = is_snap and par_path.endswith('/x1')
75 # if is_snap:
76 # return str(Path(par_path).absolute())
77 # #return getcwd()
78 # #curr_path = dirname(__file__)
79 # curr_path = str(Path(__file__).parent.parent.parent.absolute())
80 # info('current path: %s' % curr_path)
81 # return curr_path
82
8ce16d6c
FC
83 @property
84 def curr_path(self):
dd32d640
FC
85 if system() == 'Windows':
86 return ''
87 if exists('main.py'):
88 return ''
89 else:
90 par_path = str(Path(__file__).parent.absolute())
8ce16d6c 91 if self.is_appimage:
dd32d640
FC
92 par_path = str(Path(par_path).absolute())
93 par_path += '/'
94 return par_path
8ce16d6c
FC
95
96 @property
97 def build_version(self):
98 appimg_mnt = glob('/tmp/.mount_Pmachi*')
99 if appimg_mnt:
e65a09cf
FC
100 # with open(
101 # appimg_mnt[0] + '/usr/bin/appimage_version.txt') as fver:
dd32d640 102 with open(self.curr_path + 'assets/bld_version.txt') as fver:
8ce16d6c
FC
103 return fver.read().strip()
104 try:
dd32d640 105 with open(self.curr_path + 'assets/bld_version.txt') as fver:
8ce16d6c
FC
106 return fver.read().strip()
107 except FileNotFoundError:
dd32d640 108 info('not found ' + self.curr_path + 'assets/bld_version.txt')
8ce16d6c
FC
109 return 'notfound'
110
8ee66edd
FC
111 def log_cfg(self):
112 if '--version' in argv:
113 path = str(Filename.get_user_appdata_directory())
114 home = '/home/flavio' # we must force this for wine
115 if path.startswith('/c/users/') and exists(home + '/.wine/'):
116 path = home + '/.wine/drive_' + path[1:]
8ce16d6c
FC
117 info('writing %s' % path + '/pmachines/obs_version.txt')
118 with open(path + '/pmachines/obs_version.txt', 'w') as f:
e65a09cf 119 # f.write(self.eng.logic.version)
8ce16d6c 120 f.write(self.build_version)
8ee66edd 121 if not platform.startswith('win'):
e65a09cf
FC
122 from os import ttyname
123 # here because it doesn't work on windows
8ee66edd 124 import sys
baf27697
FC
125 try:
126 with open(ttyname(0), 'w') as fout:
127 sys.stdout = fout
e65a09cf
FC
128 print('version: ' + self.build_version)
129 # self.eng.logic.version)
baf27697
FC
130 except OSError: # it doesn't work with crontab
131 print('version: ' + self.build_version)
e65a09cf
FC
132 messages = ['version: ' + self.build_version]
133 # self.eng.logic.version]
8ee66edd
FC
134 messages += ['argv[0]: %s' % argv[0]]
135 messages += ['getcwd: %s' % getcwd()]
136 messages += ['__file__: %s' % __file__]
137 for elm in environ.items():
138 messages += ['env::%s: %s' % elm]
139 # os_info = (system(), release(), version())
140 # messages += ['operative system: %s %s %s' % os_info]
141 # messages += ['architecture: ' + str(architecture())]
142 # messages += ['machine: ' + machine()]
143 # messages += ['platform: ' + platform()]
144 # messages += ['processor: ' + processor()]
145 # try:
146 # messages += ['cores: ' + str(cpu_count())]
147 # except NotImplementedError: # on Windows
148 # messages += ['cores: not implemented']
8ce16d6c 149 lib_ver = PandaSystem.get_version_string()
8ee66edd
FC
150 try:
151 import psutil
152 mem = psutil.virtual_memory().total / 1000000000.0
153 messages += ['memory: %s GB' % round(mem, 2)]
e65a09cf
FC
154 except ImportError:
155 info("can't import psutil") # windows
8ce16d6c 156 lib_commit = PandaSystem.get_git_commit()
8ee66edd
FC
157 py_ver = [str(elm) for elm in version_info[:3]]
158 messages += ['python version: %s' % '.'.join(py_ver)]
159 messages += ['panda version: %s %s' % (lib_ver, lib_commit)]
8ce16d6c
FC
160 messages += ['bullet version: ' + str(get_bullet_version())]
161 messages += ['appdata: ' + str(Filename.get_user_appdata_directory())]
e65a09cf
FC
162 if base.win and isinstance(base.win, GraphicsWindow):
163 # not headless
8ee66edd
FC
164 print(base.win.get_keyboard_map())
165 list(map(self.log, messages))
166
167 @staticmethod
168 def log_tasks():
169 info('tasks: %s' % taskMgr.getAllTasks())
170 info('do-laters: %s' % taskMgr.getDoLaters())
171
172 @staticmethod
173 def plog(obj):
174 print('\n\n')
175 print_stack()
176 pprint(obj)
177 print('\n\n')
178
179
180class LogMgr(LogMgrBase):
181
182 def log_cfg(self):
183 LogMgrBase.log_cfg(self)
8ce16d6c
FC
184 messages = [base.win.get_gsg().get_driver_vendor()]
185 messages += [base.win.get_gsg().get_driver_renderer()]
e65a09cf
FC
186 shad_maj = base.win.get_gsg().\
187 get_driver_shader_version_major()
188 shad_min = base.win.get_gsg().\
189 get_driver_shader_version_minor()
8ee66edd 190 messages += ['shader: {maj}.{min}'.format(maj=shad_maj, min=shad_min)]
8ce16d6c
FC
191 messages += [base.win.get_gsg().get_driver_version()]
192 drv_maj = base.win.get_gsg().get_driver_version_major()
193 drv_min = base.win.get_gsg().get_driver_version_minor()
8ee66edd
FC
194 drv = 'driver version: {maj}.{min}'
195 messages += [drv.format(maj=drv_maj, min=drv_min)]
8ce16d6c
FC
196 fullscreen = None
197 if isinstance(base.win, GraphicsWindow):
198 fullscreen = base.win.get_properties().get_fullscreen()
199 messages += ['fullscreen: ' + str(fullscreen)]
e65a09cf 200
8ce16d6c
FC
201 def resolution():
202 if not isinstance(base.win, GraphicsWindow):
203 return 800, 600
204 win_prop = base.win.get_properties()
205 return win_prop.get_x_size(), win_prop.get_y_size()
206 res_x, res_y = resolution()
8ee66edd
FC
207 res_tmpl = 'resolution: {res_x}x{res_y}'
208 messages += [res_tmpl.format(res_x=res_x, res_y=res_y)]
209 list(map(self.log, messages))