XRootD
XrdClEnv.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #include <cstdlib>
20 
21 #include "XrdCl/XrdClEnv.hh"
22 #include "XrdCl/XrdClDefaultEnv.hh"
23 #include "XrdCl/XrdClLog.hh"
24 #include "XrdCl/XrdClConstants.hh"
25 
26 namespace XrdCl
27 {
28  //----------------------------------------------------------------------------
29  // Get string
30  //----------------------------------------------------------------------------
31  bool Env::GetString( const std::string &k, std::string &value )
32  {
33  std::string key = UnifyKey( k );
34  XrdSysRWLockHelper scopedLock( pLock );
35  StringMap::iterator it;
36  it = pStringMap.find( key );
37  if( it == pStringMap.end() )
38  {
39  Log *log = DefaultEnv::GetLog();
40  log->Debug( UtilityMsg,
41  "Env: trying to get a non-existent string entry: %s",
42  key.c_str() );
43  return false;
44  }
45  value = it->second.first;
46  return true;
47  }
48 
49  //----------------------------------------------------------------------------
50  // Put string
51  //----------------------------------------------------------------------------
52  bool Env::PutString( const std::string &k, const std::string &value )
53  {
54  std::string key = UnifyKey( k );
55  XrdSysRWLockHelper scopedLock( pLock, false );
56 
57  //--------------------------------------------------------------------------
58  // Insert the string if it's not there yet
59  //--------------------------------------------------------------------------
60  StringMap::iterator it;
61  it = pStringMap.find( key );
62  if( it == pStringMap.end() )
63  {
64  pStringMap[key] = std::make_pair( value, false );
65  return true;
66  }
67 
68  //--------------------------------------------------------------------------
69  // The entry exists and it has been imported from the shell
70  //--------------------------------------------------------------------------
71  Log *log = DefaultEnv::GetLog();
72  if( it->second.second )
73  {
74  log->Debug( UtilityMsg,
75  "Env: trying to override a shell-imported string entry: %s",
76  key.c_str() );
77  return false;
78  }
79  log->Debug( UtilityMsg,
80  "Env: overriding entry: %s=\"%s\" with \"%s\"",
81  key.c_str(), it->second.first.c_str(), value.c_str() );
82  pStringMap[key] = std::make_pair( value, false );
83  return true;
84  }
85 
86  //----------------------------------------------------------------------------
87  // Get int
88  //----------------------------------------------------------------------------
89  bool Env::GetInt( const std::string &k, int &value )
90  {
91  std::string key = UnifyKey( k );
92  XrdSysRWLockHelper scopedLock( pLock );
93  IntMap::iterator it;
94  it = pIntMap.find( key );
95  if( it == pIntMap.end() )
96  {
97  Log *log = DefaultEnv::GetLog();
98  log->Debug( UtilityMsg,
99  "Env: trying to get a non-existent integer entry: %s",
100  key.c_str() );
101  return false;
102  }
103  value = it->second.first;
104  return true;
105  }
106 
107  //----------------------------------------------------------------------------
108  // Put int
109  //----------------------------------------------------------------------------
110  bool Env::PutInt( const std::string &k, int value )
111  {
112  std::string key = UnifyKey( k );
113  XrdSysRWLockHelper scopedLock( pLock, false );
114 
115  //--------------------------------------------------------------------------
116  // Insert the string if it's not there yet
117  //--------------------------------------------------------------------------
118  IntMap::iterator it;
119  it = pIntMap.find( key );
120  if( it == pIntMap.end() )
121  {
122  pIntMap[key] = std::make_pair( value, false );
123  return true;
124  }
125 
126  //--------------------------------------------------------------------------
127  // The entry exists and it has been imported from the shell
128  //--------------------------------------------------------------------------
129  Log *log = DefaultEnv::GetLog();
130  if( it->second.second )
131  {
132  log->Debug( UtilityMsg,
133  "Env: trying to override a shell-imported integer entry: %s",
134  key.c_str() );
135  return false;
136  }
137  log->Debug( UtilityMsg,
138  "Env: overriding entry: %s=%d with %d",
139  key.c_str(), it->second.first, value );
140 
141  pIntMap[key] = std::make_pair( value, false );
142  return true;
143  }
144 
145  //----------------------------------------------------------------------------
146  // Import int
147  //----------------------------------------------------------------------------
148  bool Env::ImportInt( const std::string &k, const std::string &shellKey )
149  {
150  std::string key = UnifyKey( k );
151  XrdSysRWLockHelper scopedLock( pLock, false );
152  std::string strValue = GetEnv( shellKey );
153  if( strValue == "" )
154  return false;
155 
156  Log *log = DefaultEnv::GetLog();
157  char *endPtr;
158  int value = (int)strtol( strValue.c_str(), &endPtr, 0 );
159  if( *endPtr )
160  {
161  log->Error( UtilityMsg,
162  "Env: Unable to import %s as %s: %s is not a proper integer",
163  shellKey.c_str(), key.c_str(), strValue.c_str() );
164  return false;
165  }
166 
167  log->Info( UtilityMsg, "Env: Importing from shell %s=%d as %s",
168  shellKey.c_str(), value, key.c_str() );
169 
170  pIntMap[key] = std::make_pair( value, true );
171  return true;
172  }
173 
174  //----------------------------------------------------------------------------
175  // Import string
176  //----------------------------------------------------------------------------
177  bool Env::ImportString( const std::string &k, const std::string &shellKey )
178  {
179  std::string key = UnifyKey( k );
180  XrdSysRWLockHelper scopedLock( pLock, false );
181  std::string value = GetEnv( shellKey );
182  if( value == "" )
183  return false;
184 
185  Log *log = DefaultEnv::GetLog();
186  log->Info( UtilityMsg, "Env: Importing from shell %s=%s as %s",
187  shellKey.c_str(), value.c_str(), key.c_str() );
188  pStringMap[key] = std::make_pair( value, true );
189  return true;
190  }
191 
192  //------------------------------------------------------------------------
193  // Get default integer value for the given key
194  //------------------------------------------------------------------------
195  bool Env::GetDefaultIntValue( const std::string &k, int &value )
196  {
197  std::string key = UnifyKey( k );
198  auto itr = theDefaultInts.find( key );
199  if( itr == theDefaultInts.end() ) return false;
200  value = itr->second;
201  return true;
202  }
203 
204  //------------------------------------------------------------------------
205  // Get default string value for the given key
206  //------------------------------------------------------------------------
207  bool Env::GetDefaultStringValue( const std::string &k, std::string &value )
208  {
209  std::string key = UnifyKey( k );
210  auto itr = theDefaultStrs.find( key );
211  if( itr == theDefaultStrs.end() ) return false;
212  value = itr->second;
213  return true;
214  }
215 
216  //----------------------------------------------------------------------------
217  // Get a string from the environment
218  //----------------------------------------------------------------------------
219  std::string Env::GetEnv( const std::string &key )
220  {
221  char *var = getenv( key.c_str() );
222  if( !var )
223  return "";
224  return var;
225  }
226 }
static Log * GetLog()
Get default log.
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:110
bool PutString(const std::string &key, const std::string &value)
Definition: XrdClEnv.cc:52
bool GetDefaultIntValue(const std::string &key, int &value)
Definition: XrdClEnv.cc:195
bool ImportString(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:177
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:148
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
bool GetDefaultStringValue(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:207
Handle diagnostics.
Definition: XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
void Info(uint64_t topic, const char *format,...)
Print an info.
Definition: XrdClLog.cc:265
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition: XrdClLog.cc:282
const uint64_t UtilityMsg
static std::unordered_map< std::string, std::string > theDefaultStrs
static std::unordered_map< std::string, int > theDefaultInts