YARP
Yet Another Robot Platform
LogComponent.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/os/Os.h>
12 
13 #include <atomic>
14 #include <cstring>
15 #include <cstdlib>
16 
17 namespace {
18 
19 inline bool from_env(const char* name, bool defaultvalue)
20 {
21  const char *strvalue = std::getenv(name);
22 
23  if(!strvalue) { return defaultvalue; }
24 
25  if(strcmp(strvalue, "1") == 0) { return true; }
26  if(strcmp(strvalue, "true") == 0) { return true; }
27  if(strcmp(strvalue, "True") == 0) { return true; }
28  if(strcmp(strvalue, "TRUE") == 0) { return true; }
29  if(strcmp(strvalue, "on") == 0) { return true; }
30  if(strcmp(strvalue, "On") == 0) { return true; }
31  if(strcmp(strvalue, "ON") == 0) { return true; }
32 
33  if(strcmp(strvalue, "0") == 0) { return false; }
34  if(strcmp(strvalue, "false") == 0) { return false; }
35  if(strcmp(strvalue, "False") == 0) { return false; }
36  if(strcmp(strvalue, "FALSE") == 0) { return false; }
37  if(strcmp(strvalue, "off") == 0) { return false; }
38  if(strcmp(strvalue, "Off") == 0) { return false; }
39  if(strcmp(strvalue, "OFF") == 0) { return false; }
40 
41  return defaultvalue;
42 }
43 
44 std::atomic<bool> quiet(from_env("YARP_QUIET", false));
45 std::atomic<bool> verbose(from_env("YARP_VERBOSE", false) &&
46  !quiet.load());
47 
48 std::atomic<yarp::os::Log::LogType> minimumOsPrintLevel(
49  (quiet.load() ? yarp::os::Log::WarningType :
51 
52 } // namespace
53 
55  const char* msg,
56  const char* file,
57  const unsigned int line,
58  const char* func,
59  double systemtime,
60  double networktime,
61  double externaltime,
62  const char* comp_name)
63 {
64  if (type >= minimumOsPrintLevel.load()) {
65  yarp::os::Log::printCallback()(type, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
66  }
67 }
68 
70 {
71  minimumOsPrintLevel = minumumLogType;
72 }
yarp::os::impl::LogComponent::setMinumumLogType
void setMinumumLogType(yarp::os::Log::LogType minumumLogType)
Definition: LogComponent.cpp:69
yarp::os::Log::InfoType
@ InfoType
Definition: Log.h:79
yarp::os::getenv
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:34
yarp::os::Log::WarningType
@ WarningType
Definition: Log.h:80
yarp::os::impl::LogComponent::print_callback
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)
Definition: LogComponent.cpp:54
LogComponent.h
yarp::os::Log::LogType
LogType
Definition: Log.h:75
Os.h
yarp::os::Log::printCallback
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:852
yarp::os::Log::DebugType
@ DebugType
Definition: Log.h:78