1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author : Konstantin Leontev (OpenCascade)
25 #include "libSALOMELog.hxx"
31 enum class VerbosityMode { undefined, nolog, withlog };
33 static VerbosityMode isActivated = VerbosityMode::undefined;
37 static constexpr char ERROR_LEVEL_VALUE = 0;
38 static constexpr char ERROR_LEVEL_VALUE_STR[] = "ERROR";
39 static constexpr char WARNING_LEVEL_VALUE = 1;
40 static constexpr char WARNING_LEVEL_VALUE_STR[] = "wARNING";
41 static constexpr char INFO_LEVEL_VALUE = 2;
42 static constexpr char INFO_LEVEL_VALUE_STR[] = "INFO";
43 static constexpr char DEBUG_LEVEL_VALUE = 7;
44 static constexpr char DEBUG_LEVEL_VALUE_STR[] = "DEBUG";
45 static constexpr char UNDEFINED_LEVEL_VALUE=99;
47 enum class VerbosityLevelType { error_level=ERROR_LEVEL_VALUE, warning_level=WARNING_LEVEL_VALUE, info_level=INFO_LEVEL_VALUE, debug_level=DEBUG_LEVEL_VALUE, undefined_level=UNDEFINED_LEVEL_VALUE };
48 static VerbosityLevelType verbosityLevel = VerbosityLevelType::undefined_level;
50 static VerbosityLevelType FromIntToVerbosityLevel(char value)
54 case ERROR_LEVEL_VALUE:
55 return VerbosityLevelType::error_level;
56 case WARNING_LEVEL_VALUE:
57 return VerbosityLevelType::warning_level;
58 case INFO_LEVEL_VALUE:
59 return VerbosityLevelType::info_level;
60 case DEBUG_LEVEL_VALUE:
61 return VerbosityLevelType::debug_level;
63 throw std::range_error("FromIntToVerbosityLevel : Invalid value for verbosity level !");
66 static VerbosityLevelType FromStrToVerbosityLevel(const std::string& val)
68 if(val == ERROR_LEVEL_VALUE_STR)
69 return VerbosityLevelType::error_level;
70 if(val == WARNING_LEVEL_VALUE_STR)
71 return VerbosityLevelType::warning_level;
72 if(val == INFO_LEVEL_VALUE_STR)
73 return VerbosityLevelType::info_level;
74 if(val == DEBUG_LEVEL_VALUE_STR)
75 return VerbosityLevelType::debug_level;
76 throw std::range_error("FromStrToVerbosityLevel : Invalid str value for verbosity level !");
79 static std::string FromVerbosityLevelToStr(VerbosityLevelType level)
83 case VerbosityLevelType::error_level:
84 return std::string(ERROR_LEVEL_VALUE_STR);
85 case VerbosityLevelType::warning_level:
86 return std::string(WARNING_LEVEL_VALUE_STR);
87 case VerbosityLevelType::info_level:
88 return std::string(INFO_LEVEL_VALUE_STR);
89 case VerbosityLevelType::debug_level:
90 return std::string(DEBUG_LEVEL_VALUE_STR);
92 throw std::range_error("FromVerbosityLevelToStr : not managed verbosity level !");
96 // ============================================================================
98 * Called by any log message macros to decide about log output in Release and
99 * Debug mode dynamically rely on SALOME_VERBOSE environment variable.
100 * Checks SALOME_VERBOSE only on the very first call and returns cached result
101 * for all followed calls.
102 * Returns true if SALOME_VERBOSE is positioned and not empty and if its
103 * numeric value greater than 0.
105 // ============================================================================
107 bool VerbosityActivated()
109 auto isEnvVarSet = []() -> VerbosityMode
111 const char *envVar = std::getenv("SALOME_VERBOSE");
113 if (envVar && (envVar[0] != '\0'))
115 const int numValue = std::stoi(envVar);
116 return numValue > 0?VerbosityMode::withlog:VerbosityMode::nolog;
119 return VerbosityMode::nolog;
122 if(isActivated == VerbosityMode::undefined)
123 isActivated = isEnvVarSet();
124 return isActivated == VerbosityMode::withlog;
127 void SetVerbosityActivated(bool flag)
129 isActivated = flag ? VerbosityMode::withlog:VerbosityMode::nolog;
132 VerbosityLevelType VerbosityLevel()
134 auto isEnvVarSet = []() -> VerbosityLevelType
136 const char *envVar = std::getenv("SALOME_VERBOSE_LEVEL");
137 if (envVar && (envVar[0] != '\0'))
139 const int numValue = std::stoi(envVar);
140 return FromIntToVerbosityLevel( static_cast<char>(numValue) );
142 return VerbosityLevelType::info_level;
144 if(verbosityLevel == VerbosityLevelType::undefined_level)
145 verbosityLevel = isEnvVarSet();
146 return verbosityLevel;
149 void BASICS_EXPORT SetVerbosityLevel(VerbosityLevelType level)
151 verbosityLevel = level;
154 void SetVerbosityLevelStr(const std::string& level)
156 verbosityLevel = FromStrToVerbosityLevel(level);
159 std::string VerbosityLevelStr()
161 return FromVerbosityLevelToStr( VerbosityLevel() );