]> SALOME platform Git repositories - tools/eficas.git/blobdiff - Noyau/N_info.py
Salome HOME
gestion des listes et label sur 2
[tools/eficas.git] / Noyau / N_info.py
index 59a6621702fcbfca6eee5e5092d37da77c24f1b9..c97197f73d49daafa9f8850d91e8a08f2075ba17 100644 (file)
@@ -1,23 +1,22 @@
-#@ MODIF N_info Noyau  DATE 17/08/2011   AUTEUR COURTOIS M.COURTOIS 
-# -*- coding: iso-8859-1 -*-
-#            CONFIGURATION MANAGEMENT OF EDF VERSION
-# ======================================================================
-# COPYRIGHT (C) 1991 - 2011  EDF R&D                  WWW.CODE-ASTER.ORG
-# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
-# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
-# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
-# (AT YOUR OPTION) ANY LATER VERSION.
+# coding=utf-8
+# Copyright (C) 2007-2013   EDF R&D
 #
-# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
-# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
-# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
-# GENERAL PUBLIC LICENSE FOR MORE DETAILS.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
 #
-# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
-# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
-#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
-# ======================================================================
-# RESPONSABLE COURTOIS M.COURTOIS
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+
 
 """Module to manage information printing : debug, info, error.
 Should replace 'print' and 'UTMESS' calls at least in the supervisor
@@ -32,11 +31,13 @@ import traceback
 from functools import partial
 from subprocess import Popen, PIPE
 
-from N_utils import Enum
+from N_utils import Enum, Singleton
+from strfunc import convert
+
 
 def default_print(text):
     """Basic print function."""
-    print text
+    print convert(text)
 
 LEVEL = Enum(
     'DEBUG',
@@ -45,17 +46,20 @@ LEVEL = Enum(
     'ERROR'
 )
 
+
 class Category(object):
+
     """Define a category of message for different parts of the code.
     This allows to store different parameters for each category of message."""
+
     def __init__(self):
         self._level = LEVEL.INFO
         self._fmt = "%-8s"
         self._header = {
-            LEVEL.DEBUG : "DEBUG",
-            LEVEL.INFO : None,
-            LEVEL.WARN : "WARNING",
-            LEVEL.ERROR : "ERROR",
+            LEVEL.DEBUG: "DEBUG",
+            LEVEL.INFO: None,
+            LEVEL.WARN: "WARNING",
+            LEVEL.ERROR: "ERROR",
         }
 
     def set_level(self, level):
@@ -91,8 +95,13 @@ REGEXP_ORIG = re.compile('File [\'\"]*(.*?)[\'\"]*, *line ([0-9]+), *in (.*)')
 
 # slighty different and very simplier than logger objects
 # from the logging module.
-class InfoLevel(object):
+
+
+class InfoLevel(Singleton):
+
     """Store informations level."""
+    _singleton_id = 'N_info.InfoLevel'
+
     def __init__(self, level):
         """Initialization"""
         self._parts = []
@@ -100,7 +109,7 @@ class InfoLevel(object):
             part.level = level
         self.reset_print_function()
         self._msg_callback = []
-        #self.extend_message(ALL, stack_header_callback)
+        # self.extend_message(ALL, stack_header_callback)
         self.extend_message(ALL, insert_header)
 
     def add(self, category):
@@ -163,10 +172,10 @@ class InfoLevel(object):
         # how to use callbacks ? valk ?
         from Utilitai.Utmess import MessageLog
         code = {
-            LEVEL.DEBUG : 'I',
-            LEVEL.INFO : 'I',
-            LEVEL.WARN : 'A',
-            LEVEL.ERROR : 'F',
+            LEVEL.DEBUG: 'I',
+            LEVEL.INFO: 'I',
+            LEVEL.WARN: 'A',
+            LEVEL.ERROR: 'F',
         }
         valk = kwargs.get('valk', ())
         vali = kwargs.get('vali', ())
@@ -213,13 +222,15 @@ def insert_header(category, level, msg, args, kwargs):
         msg = header + msg
     return msg, args
 
+
 def stack_header_callback(category, level, msg, args, kwargs):
     """To insert the origin."""
     if level <= LEVEL.DEBUG:
         stack_id = -5 + kwargs.get('stack_id', 0)
         stack = traceback.format_stack(limit=10)[stack_id]
         mat = REGEXP_ORIG.search(stack)
-        origin = '[%s:%s in %s] ' % (osp.basename(mat.group(1)), mat.group(2), mat.group(3))
+        origin = '[%s:%s in %s] ' % (
+            osp.basename(mat.group(1)), mat.group(2), mat.group(3))
         msg = origin + msg
     return msg, args
 
@@ -235,15 +246,18 @@ _pid = os.getpid()
 
 RE_VMPEAK = re.compile('VmPeak:\s*([0-9]+)\s*([kMGBo]+)', re.M | re.I)
 
+
 def memory_used(pid):
     """Return the current VmPeak value."""
     p = Popen(['cat', '/proc/%s/status' % pid], stdout=PIPE)
     output = p.communicate()[0]
     mat = RE_VMPEAK.search(output)
-    return int(mat.group(1)) / 1024.
+    mem = mat and int(mat.group(1)) or 0.
+    return mem / 1024.
 
 current_memory_used = partial(memory_used, _pid)
 
+
 def mem_msg_callback(category, level, msg, args, kwargs):
     """Callback to add memory infos to message."""
     if level <= LEVEL.DEBUG:
@@ -255,15 +269,13 @@ def mem_msg_callback(category, level, msg, args, kwargs):
 if __name__ == "__main__":
     message.set_level(SUPERV, LEVEL.WARN)
     message.set_level(MISS, LEVEL.DEBUG)
-    message.debug(None, "debug message")
+    message.debug(None, "debug message")
     message.info(ALL, "information message")
     message.warn(None, "warning message")
     message.error(ALL, "error message")
     message.add_memory_info()
-    message.debug(MISS, "debug supervisor message")
+    message.debug(MISS, "debug supervisor message")
     message.info(SUPERV, "information supervisor message")
     message.warn(SUPERV, "warning supervisor message")
     message.error(SUPERV, "error supervisor message")
     message.critical(MISS, "test the critical alias")
-
-