Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/logger.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file logger.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
import types
20
from time import gmtime, strftime
21
22
23
class Logger:
24
def __init__(self, filepath=None, is_stdout=True,
25
timeformat="%a, %d %b %Y %H:%M:%S"):
26
self._filepath = filepath
27
self._logfile = None
28
self._callbacks = {}
29
self._is_stdout = is_stdout
30
self._timeformat = timeformat
31
32
def start(self, text="Start logging."):
33
# print 'Logger.start:',self._filepath,self._filepath is not None
34
if self._filepath is not None:
35
ttext = strftime(self._timeformat, gmtime())+' '+text
36
self._logfile = open(self._filepath, 'w')
37
self._logfile.write(ttext+'\n')
38
if self._is_stdout:
39
print text
40
41
def add_callback(self, function, key='message'):
42
self._callbacks[key] = function
43
44
def get_clallbackfunc(self, key):
45
46
return self._callbacks.get(key, None)
47
48
def del_callback(self, key):
49
del self._callbacks[key]
50
51
def progress(self, percent):
52
self.w(percent, key='progress')
53
54
def w(self, data, key='message', **kwargs):
55
# print 'Logger.w:',self._logfile is not None,self._is_stdout,data
56
57
if key == 'progress':
58
text = '%d %% completed.' % data
59
else:
60
text = str(data)
61
62
if self._logfile is not None:
63
self._logfile.write(strftime(self._timeformat, gmtime())+' '+text+'\n')
64
65
elif self._callbacks.has_key(key):
66
kwargs['key'] = key
67
self._callbacks[key](data, **kwargs)
68
# elif type(data)==types.StringType:
69
# print data
70
if self._is_stdout:
71
print text
72
73
def stop(self, text="End logging."):
74
75
if self._logfile is not None:
76
ttext = strftime(self._timeformat, gmtime())+' '+text
77
self._logfile.write(ttext+'\n')
78
self._logfile.close()
79
self._logfile = None
80
81
if self._is_stdout:
82
print text
83
84