YARP
Yet Another Robot Platform
LogComponent.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
7 
8 #include <yarp/os/Os.h>
9 
10 #include <atomic>
11 #include <cstring>
12 #include <cstdlib>
13 
14 namespace {
15 
16 inline bool from_env(const char* name, bool defaultvalue)
17 {
18  const char *strvalue = std::getenv(name);
19 
20  if(!strvalue) { return defaultvalue; }
21 
22  if(strcmp(strvalue, "1") == 0) { return true; }
23  if(strcmp(strvalue, "true") == 0) { return true; }
24  if(strcmp(strvalue, "True") == 0) { return true; }
25  if(strcmp(strvalue, "TRUE") == 0) { return true; }
26  if(strcmp(strvalue, "on") == 0) { return true; }
27  if(strcmp(strvalue, "On") == 0) { return true; }
28  if(strcmp(strvalue, "ON") == 0) { return true; }
29 
30  if(strcmp(strvalue, "0") == 0) { return false; }
31  if(strcmp(strvalue, "false") == 0) { return false; }
32  if(strcmp(strvalue, "False") == 0) { return false; }
33  if(strcmp(strvalue, "FALSE") == 0) { return false; }
34  if(strcmp(strvalue, "off") == 0) { return false; }
35  if(strcmp(strvalue, "Off") == 0) { return false; }
36  if(strcmp(strvalue, "OFF") == 0) { return false; }
37 
38  return defaultvalue;
39 }
40 
41 std::atomic<bool> quiet(from_env("YARP_QUIET", false));
42 std::atomic<bool> verbose(from_env("YARP_VERBOSE", false) &&
43  !quiet.load());
44 
45 std::atomic<yarp::os::Log::LogType> minimumOsPrintLevel(
46  (quiet.load() ? yarp::os::Log::WarningType :
48 
49 } // namespace
50 
52  const char* msg,
53  const char* file,
54  const unsigned int line,
55  const char* func,
56  double systemtime,
57  double networktime,
58  double externaltime,
59  const char* comp_name)
60 {
61  if (type >= minimumOsPrintLevel.load()) {
62  yarp::os::Log::printCallback()(type, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
63  }
64 }
65 
67 {
68  minimumOsPrintLevel = minumumLogType;
69 }
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:849
@ DebugType
Definition: Log.h:75
@ InfoType
Definition: Log.h:76
@ WarningType
Definition: Log.h:77
void print_callback(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name)
void setMinumumLogType(yarp::os::Log::LogType minumumLogType)
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:31