Changeset 135:e69efd5776a1 in rrlib_util


Ignore:
Timestamp:
11.06.2015 15:04:15 (4 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
14.08
Parents:
129:2aeacbc68ea9 (diff), 134:fb5983113932 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Phase:
public
Message:

Merge with 13.10

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sFileIOUtils.cpp

    r122 r135  
    488488  } 
    489489 
    490   assert(tokens.size() == 4); 
    491   RRLIB_LOG_PRINT(USER, "found_name <", tokens[0], "> , ip <", tokens[3], ">"); 
    492  
    493   inet_aton(tokens[3].c_str(), &address); 
     490  if (tokens.size() == 4) 
     491  { 
     492    RRLIB_LOG_PRINTF(USER, "found_name <%s> , ip <%s> \n", tokens[0].c_str(), tokens[3].c_str()); 
     493    inet_aton(tokens[3].c_str(), &address); 
     494  } 
    494495  return address; 
    495496} // HostToIpViaHost() 
  • sFileIOUtils.cpp

    r131 r135  
    5656//---------------------------------------------------------------------- 
    5757#include <cassert> 
     58#include <cstring> 
     59#include <cerrno> 
    5860 
    5961//---------------------------------------------------------------------- 
     
    132134  if (system(sys_call.str().c_str()) == -1) 
    133135  { 
    134     RRLIB_LOG_PRINTF(USER, "sFileIOUtils>> Error creating dir: '%s'\n", path.c_str()); 
     136    RRLIB_LOG_PRINT(USER, "sFileIOUtils>> Error creating dir: '", path, "'"); 
    135137//     std::stringstream error_msg; 
    136138//     error_msg << "sFileIOUtils>> Error creating Dir:\n" 
     
    141143  else 
    142144  { 
    143 //     RRLIB_LOG_PRINTF(USER, "sFileIOUtils>> Directory: '%s' successfully created.\n", path.c_str()); 
     145//     RRLIB_LOG_PRINT(USER, "Directory: '", path, "' successfully created."); 
    144146    return true; 
    145147  } 
     
    155157  if (fgets(buf, sizeof(buf), pipe) == 0) 
    156158  { 
    157     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::CreateTempfile() >> Error creating temp file!\n"); 
     159    RRLIB_LOG_PRINT(ERROR, "Error creating temp file!"); 
    158160    exit(EXIT_FAILURE); 
    159161  } 
     
    183185  if (system(cmd.str().c_str()) != 0) 
    184186  { 
    185     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils>> Execution of command '%s' failed!\n", cmd.str().c_str()); 
     187    RRLIB_LOG_PRINT(ERROR, "Execution of command '", cmd.str(), "' failed!"); 
    186188    exit(EXIT_FAILURE); 
    187189  } 
     
    198200  if (system(cmd.str().c_str()) != 0) 
    199201  { 
    200     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils>> Execution of command '%s' failed!\n", cmd.str().c_str()); 
     202    RRLIB_LOG_PRINT(ERROR, "Execution of command '", cmd.str(), "' failed!"); 
    201203    exit(EXIT_FAILURE); 
    202204  } 
     
    223225 
    224226  return true; 
    225 } 
     227} // FileExists() 
     228 
     229 
     230//---------------------------------------------------------------------- 
     231// class sFileIOUtils::CountLineNumbers() 
     232//---------------------------------------------------------------------- 
     233size_t sFileIOUtils::CountLineNumbers(const std::string &filename) 
     234{ 
     235  std::ifstream file(filename); 
     236  return std::count(std::istreambuf_iterator<char>(file), 
     237                    std::istreambuf_iterator<char>(), '\n'); 
     238} // CountLineNumbers() 
     239 
    226240 
    227241 
     
    233247  return ShellExpandFilename(file_name, file_name); 
    234248} // ShellExpandFilename() 
     249 
     250//---------------------------------------------------------------------- 
     251// class sFileIOUtils::ShellExpandFilename() 
     252//---------------------------------------------------------------------- 
     253std::string sFileIOUtils::ShellExpandFilename(const std::string &file_name) 
     254{ 
     255  std::string result(""); 
     256  if (ShellExpandFilename(result, file_name)) 
     257    return result; 
     258  else 
     259    return ""; 
     260} // ShellExpandFilename() 
     261 
    235262 
    236263//---------------------------------------------------------------------- 
     
    262289      error_msg = "Unknown error"; 
    263290    } 
    264     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::%s >> Could not expand '%s': %s!\n", __FUNCTION__, file_name.c_str(), error_msg); 
     291    RRLIB_LOG_PRINT(ERROR, "Could not expand '", file_name, "': ", error_msg, "!"); 
    265292    return false; 
    266293  } 
     
    313340 
    314341 
    315  
    316342//---------------------------------------------------------------------- 
    317343// class sFileIOUtils::GetHostName() 
    318344//---------------------------------------------------------------------- 
    319 std::string sFileIOUtils::GetHostName(bool fqdn) 
    320 { 
    321   //RRLIB_LOG_PRINTF(USER, "sFileIOUtils::GetHostName() >>> started\n"); 
    322   std::string cmd = fqdn ? "hostname -f" : "hostname"; 
    323   FILE * pipe = popen(cmd.c_str(), "r"); 
    324   char buf[ 1024 ]; 
    325   if (fgets(buf, sizeof(buf), pipe) == 0) 
    326   { 
    327     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::GetHostName(bool fqdn) >> Error querying host name!\n"); 
     345std::string sFileIOUtils::GetHostName() 
     346{ 
     347  char name[1024]; 
     348  int result = gethostname(name, 1024); 
     349  if (result) 
     350  { 
     351    RRLIB_LOG_PRINT(ERROR, "got error: <", strerror(errno), ">"); 
    328352    exit(EXIT_FAILURE); 
    329353  } 
    330   pclose(pipe); 
    331   std::string name(buf); 
    332   sStringUtils::TrimWhitespace(name); 
    333   //boost::trim(name); 
    334   //RRLIB_LOG_PRINTF(USER, "sFileIOUtils::GetHostName() >>> finished with name <%s>\n", name.c_str()); 
     354  RRLIB_LOG_PRINT(DEBUG, "got hostname: <", name, ">"); 
     355 
    335356  return name; 
    336357} // GetHostName() 
     
    347368  { 
    348369    address = * ((struct in_addr *) host_ent->h_addr_list[ 0 ]); 
    349     RRLIB_LOG_PRINTF(USER, "ip address of host <%s> = <%s>\n", name.c_str(), inet_ntoa(address)); 
    350   } 
    351   else 
    352   { 
    353     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::GetIpAddressOfHost() >>> could not get ip address of host <%s>\n", name.c_str()); 
     370    RRLIB_LOG_PRINT(USER, "ip address of host <", name, "> = <", inet_ntoa(address), ">"); 
     371  } 
     372  else 
     373  { 
     374    RRLIB_LOG_PRINT(ERROR, "could not get ip address of host <", name, ">"); 
    354375  } 
    355376  return address; 
     
    362383struct in_addr sFileIOUtils::HostToIpViaNslookup(const std::string & name) 
    363384{ 
    364   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaNslookup() >>> started with host <%s>\n", name.c_str()); 
     385  RRLIB_LOG_PRINT(USER, "started with host <", name, ">"); 
    365386  struct in_addr address; 
    366387  address.s_addr = 0; 
     
    368389  std::stringstream command; 
    369390  command << "nslookup \"" << name << "\""; 
    370   RRLIB_LOG_PRINTF(USER, "command = <%s>\n", command.str().c_str()); 
     391  RRLIB_LOG_PRINT(USER, "command = <", command, ">"); 
    371392  FILE * pipe = popen(command.str().c_str(), "r"); 
    372393  if (!pipe) 
     
    382403  } 
    383404  pclose(pipe); 
    384   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaNslookup() >>> host = <%s> , result = <%s> \n", name.c_str(), result.str().c_str()); 
     405  RRLIB_LOG_PRINT(USER, "host = <", name, ">, result = <", result.str(), ">"); 
    385406 
    386407  std::string result_str(result.str()); 
     
    389410  if (pos == std::string::npos) 
    390411  { 
    391     RRLIB_LOG_PRINTF(ERROR, "Could not find token <%s> in nslookup result ... returning <%s> \n", search_token.c_str(), inet_ntoa(address)); 
     412    RRLIB_LOG_PRINT(ERROR, "Could not find token <", search_token, "> in nslookup result ... returning <", inet_ntoa(address), ">"); 
    392413    return address; 
    393414  } 
    394415  result_str.erase(0, pos + search_token.length()); 
    395   RRLIB_LOG_PRINTF(USER, "pos = %zd\n", pos); 
     416  RRLIB_LOG_PRINT(USER, "pos = ", pos); 
    396417 
    397418  search_token = "Address:"; 
     
    399420  if (pos == std::string::npos) 
    400421  { 
    401     RRLIB_LOG_PRINTF(ERROR, "Could not find token <%s> in nslookup result ... returning <%s> \n", search_token.c_str(), inet_ntoa(address)); 
     422    RRLIB_LOG_PRINT(ERROR, "Could not find token <", search_token, "> in nslookup result ... returning <", inet_ntoa(address), ">"); 
    402423    return address; 
    403424  } 
    404   RRLIB_LOG_PRINTF(USER, "pos = %zd\n", pos); 
     425  RRLIB_LOG_PRINT(USER, "pos = ", pos); 
    405426 
    406427  std::string found_name(result_str.substr(0, pos)); 
     
    411432  //boost::trim(ip); 
    412433 
    413   RRLIB_LOG_PRINTF(USER, "found_name <%s> , name <%s> \n", found_name.c_str(), name.c_str()); 
     434  RRLIB_LOG_PRINT(USER, "found_name <", found_name, "> , name <", name, ">"); 
    414435 
    415436  if (found_name != name) 
    416437  { 
    417     RRLIB_LOG_PRINTF(ERROR, "nslookup failed .... returning <%s> \n", inet_ntoa(address)); 
     438    RRLIB_LOG_PRINT(ERROR, "nslookup failed .... returning <", inet_ntoa(address), ">"); 
    418439    return address; 
    419440  } 
    420   RRLIB_LOG_PRINTF(USER, "found_name <%s> , ip <%s> \n", found_name.c_str(), ip.c_str()); 
     441  RRLIB_LOG_PRINT(USER, "found_name <", found_name, "> , ip <", ip, ">"); 
    421442 
    422443  inet_aton(ip.c_str(), &address); 
     
    432453struct in_addr sFileIOUtils::HostToIpViaHost(const std::string & name) 
    433454{ 
    434   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaHost() >>> started with host <%s>\n", name.c_str()); 
     455  RRLIB_LOG_PRINT(USER, "started with host <", name, ">"); 
    435456  struct in_addr address; 
    436457  address.s_addr = 0; 
     
    438459  std::stringstream command; 
    439460  command << "host -t A \"" << name << "\""; 
    440   RRLIB_LOG_PRINTF(USER, "command = <%s>\n", command.str().c_str()); 
     461  RRLIB_LOG_PRINT(USER, "command = <", command.str(), ">"); 
    441462  FILE * pipe = popen(command.str().c_str(), "r"); 
    442463  if (!pipe) 
     
    452473  } 
    453474  pclose(pipe); 
    454   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaHost() >>> host = <%s> , result = <%s> \n", name.c_str(), result.str().c_str()); 
     475  RRLIB_LOG_PRINT(USER, "host = <", name, "> , result = <", result.str(), ">"); 
    455476 
    456477  std::string result_str(result.str()); 
     
    459480 
    460481  for_each(tokens.begin(), tokens.end(), sStringUtils::Trim<std::string>()); 
    461   RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::HostToIpViaHost() >>> got %d tokens:\n", tokens.size()); 
     482  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got ", tokens.size(), " tokens:"); 
    462483  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, Join(tokens, "\n")); 
     484  if (tokens.size() != 4) 
     485  { 
     486    RRLIB_LOG_PRINT(WARNING, "Could not determine IP for host name ", name); 
     487    return address; 
     488  } 
    463489 
    464490  if (tokens.size() == 4) 
     
    475501struct in_addr sFileIOUtils::HostToIpViaHostsFile(const std::string & name) 
    476502{ 
    477   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaHostsFile() >>> started with host <%s>\n", name.c_str()); 
     503  RRLIB_LOG_PRINT(USER, "started with host <", name, ">"); 
    478504  struct in_addr address; 
    479505  address.s_addr = 0; 
     
    501527          { 
    502528            ip_address = *iter; 
    503             RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIpViaHostsFile() >>> got ip <%s> of host <%s> from hosts file\n", ip_address.c_str(), name.c_str()); 
     529            RRLIB_LOG_PRINT(USER, "sFileIOUtils::HostToIpViaHostsFile() >>> got ip <", ip_address, "> of host <", name, "> from hosts file"); 
    504530            found = true; 
    505531            break; 
     
    519545struct in_addr sFileIOUtils::HostToIp(const std::string & name) 
    520546{ 
    521   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIp() >>> started with host <%s>\n", name.c_str()); 
     547  RRLIB_LOG_PRINT(USER, "started with host <", name, ">"); 
    522548 
    523549  struct in_addr address; 
     
    526552  if ((address = sFileIOUtils::HostToIpViaHostsFile(name)).s_addr != 0) 
    527553  { 
    528     RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIp() >>> got ip <%s> of host <%s> from hosts_file\n", inet_ntoa(address), name.c_str()); 
     554    RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> from hosts_file"); 
    529555  } 
    530556  else if ((address = sFileIOUtils::HostToIpViaHost(name)).s_addr != 0) 
    531557  { 
    532     RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIp() >>> got ip <%s> of host <%s> via <host -t A>\n", inet_ntoa(address), name.c_str()); 
     558    RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> via <host -t A>"); 
    533559  } 
    534560  else if ((address = sFileIOUtils::HostToIpViaNslookup(name)).s_addr != 0) 
    535561  { 
    536     RRLIB_LOG_PRINTF(USER, "sFileIOUtils::HostToIp() >>> got ip <%s> of host <%s> via <nslookup>\n", inet_ntoa(address), name.c_str()); 
    537   } 
    538   else 
    539   { 
    540     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::HostToIp() >>> could not get ip of host <%s>\n", name.c_str()); 
     562    RRLIB_LOG_PRINT(USER, "got ip <", inet_ntoa(address), "> of host <", name, "> via <nslookup>"); 
     563  } 
     564  else 
     565  { 
     566    RRLIB_LOG_PRINT(ERROR, "could not get ip of host <", name, ">"); 
    541567  } 
    542568 
     
    566592                << target_directory; 
    567593 
    568   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::RSyncFile() >>> executing <%s> ...\n", rsync_command.str().c_str()); 
     594  RRLIB_LOG_PRINT(USER, "executing <", rsync_command.str(), "> ..."); 
    569595  int ret = system(rsync_command.str().c_str()); 
    570   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::RSyncFile() >>> ... done.\n"); 
    571  
    572   RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::RSyncFile() >>> finished with result %d\n", ret); 
     596  RRLIB_LOG_PRINT(USER, " ... done.\n"); 
     597 
     598  RRLIB_LOG_PRINT(ERROR, "finished with result ", ret); 
    573599  return ret; 
    574600} // RSyncFile() 
     
    601627                << target_directory; 
    602628 
    603   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::RSyncFiles() >>> executing <%s> ...\n", rsync_command.str().c_str()); 
     629  RRLIB_LOG_PRINT(USER, "executing <", rsync_command.str(), "> ..."); 
    604630  int ret = system(rsync_command.str().c_str()); 
    605   RRLIB_LOG_PRINTF(USER, "sFileIOUtils::RSyncFiles() >>> ... done.\n"); 
    606  
    607   RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::RSyncFiles() >>> finished with result %d\n", ret); 
     631  RRLIB_LOG_PRINT(USER, "... done.\n"); 
     632 
     633  RRLIB_LOG_PRINT(ERROR, "finished with result ", ret); 
    608634  return ret; 
    609635} // RSyncFiles() 
     
    615641bool sFileIOUtils::CheckAndGetFile(const std::string &file_name, std::string &full_local_file_name, const std::string& resource_repository, const std::string& resource_server, const std::string& local_resource_directory, const std::string& server_resource_directory, bool use_cache) 
    616642{ 
    617   RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> started with local_resource_directory <%s>, resource_repository <%s>, file_name <%s>, resource_server <%s>, server_resource_directory <%s>\n", local_resource_directory.c_str(), resource_repository.c_str(), file_name.c_str(), resource_server.c_str(), server_resource_directory.c_str()); 
     643  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "started with local_resource_directory <", local_resource_directory, ">, resource_repository <", resource_repository, ">, file_name <", file_name, ">, resource_server <", resource_server, ">, server_resource_directory <", server_resource_directory, ">"); 
    618644 
    619645  //!##################################################################################################### 
     
    636662  full_local_file_name = "./" + resource_repository + file_name; 
    637663 
    638   RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> 1. check: trying to load <%s>\n", full_local_file_name.c_str()); 
     664  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "1. check: trying to load <", full_local_file_name, ">"); 
    639665  ifstream try_1(full_local_file_name.c_str()); 
    640666  if (try_1) 
     
    654680    if (!sFileIOUtils::ShellExpandFilename(expanded_local_resource_directory, local_resource_directory + "/")) 
    655681    { 
    656       RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::%s >> Could not expand local resource directory!\n", __FUNCTION__); 
     682      RRLIB_LOG_PRINT(ERROR, "Could not expand local resource directory!"); 
    657683      return false; 
    658684    } 
    659685    full_local_file_name = (expanded_local_resource_directory + resource_repository + file_name); 
    660686 
    661     RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> 2. check: trying to load <%s>\n", full_local_file_name.c_str()); 
     687    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "2. check: trying to load <", full_local_file_name, ">"); 
    662688    ifstream try_2(full_local_file_name.c_str()); 
    663689    if (try_2) 
     
    677703      { 
    678704        local_host = sFileIOUtils::cached_local_host; 
    679         RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> retrieved local_host <%s> from cache\n", local_host.c_str()); 
     705        RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "retrieved local_host <", local_host, "> from cache"); 
    680706      } 
    681707      else 
     
    694720        { 
    695721          server_ip_address = pos->second; 
    696           RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> retrieved ip address <%s> of host <%s> from cache\n", server_ip_address.c_str(), server.c_str()); 
     722          RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "retrieved ip address <", server_ip_address, "> of host <", server, "> from cache"); 
    697723        } 
    698724        else 
    699725        { 
    700726          server_ip_address = inet_ntoa(sFileIOUtils::HostToIp(server)); 
    701           RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> got ip address <%s> of host <%s>\n", server_ip_address.c_str(), server.c_str()); 
     727          RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got ip address <", server_ip_address, "> of host <", server, ">"); 
    702728 
    703729          if (server_ip_address == "0.0.0.0") 
    704730          { 
    705             RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::constructor >>> zero ip address ... clearing server\n"); 
     731            RRLIB_LOG_PRINT(ERROR, "zero ip address ... clearing server"); 
    706732            server = ""; 
    707733          } 
     
    716742      if (server != "")   //&& server != local_host) 
    717743      { 
    718         RRLIB_LOG_PRINTF(DEBUG_VERBOSE_1, "sFileIOUtils::CheckAndGetFile() >>> 3. check: trying to rsync <%s:%s> to <%s> \n", server_ip_address.c_str(), (server_resource_directory + resource_repository + file_name).c_str(), expanded_local_resource_directory.c_str()); 
     744        RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "3. check: trying to rsync <", server_ip_address, ":", server_resource_directory + resource_repository + file_name, "> to <", expanded_local_resource_directory, ">"); 
    719745 
    720746        // rsync from <resource_server:server_resource_directory + resource_repository> 
     
    723749        if (sFileIOUtils::RSyncFile("", server_ip_address, server_resource_directory + "/./" + resource_repository, file_name, expanded_local_resource_directory, "R") != 0) 
    724750        { 
    725           RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::CheckAndGetFile() >>> could not rsync file <%s> from server <%s> at <%s> ... skipping\n", file_name.c_str(), server.c_str(), (server_resource_directory + resource_repository).c_str()); 
     751          RRLIB_LOG_PRINT(ERROR, "could not rsync file <", file_name, "> from server <", server, "> at <", server_resource_directory + resource_repository, "> ... skipping"); 
    726752          return false; 
    727753        } 
     
    730756      else 
    731757      { 
    732         RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::CheckAndGetFile() >>> could neither load <%s> locally nor from resource server ... skipping\n", file_name.c_str()); 
     758        RRLIB_LOG_PRINT(ERROR, "could neither load <", file_name, "> locally nor from resource server ... skipping"); 
    733759        return false; 
    734760      } 
     
    740766  if (!try_3) 
    741767  { 
    742     RRLIB_LOG_PRINTF(ERROR, "sFileIOUtils::CheckAndGetFile() >>> final check failed: could not load <%s> ... skipping\n", full_local_file_name.c_str()); 
     768    RRLIB_LOG_PRINT(ERROR, "final check failed: could not load <", full_local_file_name, "> ... skipping"); 
    743769    return false; 
    744770  } 
Note: See TracChangeset for help on using the changeset viewer.