Salome HOME
[EDF27562] : Fix clustertest
[modules/kernel.git] / src / Basics / libSALOMELog.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  Author : Konstantin Leontev (OpenCascade)
21 //  Module : KERNEL
22 //  $Header$
23 //
24
25 #include "libSALOMELog.hxx"
26
27 #include <string>
28 #include <iostream>
29
30 enum class VerbosityMode { undefined, nolog, withlog };
31
32 static VerbosityMode isActivated = VerbosityMode::undefined;
33
34 namespace SALOME
35 {
36
37 // ============================================================================
38 /*!
39  *  Called by any log message macros to decide about log output in Release and
40  *  Debug mode dynamically rely on SALOME_VERBOSE environment variable.
41  *  Checks SALOME_VERBOSE only on the very first call and returns cached result
42  *  for all followed calls.
43  *  Returns true if SALOME_VERBOSE is positioned and not empty and if its
44  *  numeric value greater than 0.
45  */
46 // ============================================================================
47
48   bool VerbosityActivated()
49   {
50     auto isEnvVarSet = []() -> VerbosityMode
51     {
52       const char* envVar = std::getenv("SALOME_VERBOSE");
53
54       if (envVar && (envVar[0] != '\0'))
55       {
56         try
57         {
58           const long long numValue = std::stoll(envVar);
59           return numValue > 0?VerbosityMode::withlog:VerbosityMode::nolog;
60         }
61         catch(const std::exception& e)
62         {
63           std::cerr << e.what() << '\n';
64         }
65       }
66
67       return VerbosityMode::nolog;
68     };
69
70     if(isActivated == VerbosityMode::undefined)
71       isActivated = isEnvVarSet();
72     return isActivated == VerbosityMode::withlog;
73   }
74
75   void SetVerbosityActivated(bool flag)
76   {
77     isActivated = flag ? VerbosityMode::withlog:VerbosityMode::nolog;
78   }
79 }