Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/logger.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file logger.py15# @author Joerg Schweizer16# @date 20121718import types19from time import gmtime, strftime202122class Logger:23def __init__(self, filepath=None, is_stdout=True,24timeformat="%a, %d %b %Y %H:%M:%S"):25self._filepath = filepath26self._logfile = None27self._callbacks = {}28self._is_stdout = is_stdout29self._timeformat = timeformat3031def start(self, text="Start logging."):32# print 'Logger.start:',self._filepath,self._filepath is not None33if self._filepath is not None:34ttext = strftime(self._timeformat, gmtime())+' '+text35self._logfile = open(self._filepath, 'w')36self._logfile.write(ttext+'\n')37if self._is_stdout:38print text3940def add_callback(self, function, key='message'):41self._callbacks[key] = function4243def get_clallbackfunc(self, key):4445return self._callbacks.get(key, None)4647def del_callback(self, key):48del self._callbacks[key]4950def progress(self, percent):51self.w(percent, key='progress')5253def w(self, data, key='message', **kwargs):54# print 'Logger.w:',self._logfile is not None,self._is_stdout,data5556if key == 'progress':57text = '%d %% completed.' % data58else:59text = str(data)6061if self._logfile is not None:62self._logfile.write(strftime(self._timeformat, gmtime())+' '+text+'\n')6364elif self._callbacks.has_key(key):65kwargs['key'] = key66self._callbacks[key](data, **kwargs)67# elif type(data)==types.StringType:68# print data69if self._is_stdout:70print text7172def stop(self, text="End logging."):7374if self._logfile is not None:75ttext = strftime(self._timeformat, gmtime())+' '+text76self._logfile.write(ttext+'\n')77self._logfile.close()78self._logfile = None7980if self._is_stdout:81print text828384