EnvironmentVariable.cc
1 #include "EnvironmentVariable.h"
2 #include <iostream>
3 #include <fstream>
4 #include <stdlib.h>
5 #include "TString.h"
6 
16 const char* getEnv(const char* envName, const char* description, bool failHard=true){
17 
18  const char* envVar = std::getenv(envName);
19  if(!envVar){
20  const char* RED = "\x1b[31m";
21  const char* RESET = "\x1b[0m";
22 
23  // print fatal error or warning
24  if(failHard){
25  std::cerr << RED << "Fatal error! " << RESET;
26  }
27  else{
28  std::cerr << RED << "Warning! " << RESET;
29  }
30 
31  // what we couldn't find
32  std::cerr << "Could not find environment variable "
33  << RED << envName << RESET << std::endl;
34 
35  // helpful message, if you gave me one.
36  if(description){
37  std::cerr << description << std::endl;
38  }
39 
40  // maybe actually give up
41  if(failHard){
42  std::cerr << "Giving up." << std::endl;
43  exit(1);
44  }
45  }
46  return envVar;
47 }
48 
49 
57 
58 #ifdef ANITA_BUILD_TOOL
59  const char* icemc_src_dir = getEnv("ICEMC_SRC_DIR", "Without this environment variable I can't find input data or config files!", true);
60  return icemc_src_dir;
61 #else
62  const char* icemc_src_dir = getEnv("ICEMC_SRC_DIR", "Will guess icemc source directory is present working directory", false);
63  if(!icemc_src_dir){
64  icemc_src_dir = ".";
65  }
66  return icemc_src_dir;
67 
68 #endif
69 }
70 
71 const char* EnvironmentVariable::ICEMC_VERSION(TString outputdir){
72 
73  system(Form("git rev-parse HEAD > %s/gitversion.txt", outputdir.Data()));
74  static std::string gitversion;
75  std::ifstream gitversionfile (Form("%s/gitversion.txt", outputdir.Data()));
76  if (gitversionfile.is_open())
77  {
78  getline (gitversionfile,gitversion);
79  gitversionfile.close();
80  }
81 
82  return gitversion.c_str();
83 
84 }
const char * ICEMC_SRC_DIR()