Function getBashOutput

Synopsis

#include <src/easylogging++.h>

static const std::string getBashOutput(const char *command)

Description

Runs command on terminal and returns the output.

@detail This is applicable only on unix based systems, for all other OS, an empty string is returned.

Parameters

command - Bash command

Returns
Result of bash output or empty string if no result found.

Source

Lines 1067-1093 in src/easylogging++.cc. Line 1163 in src/easylogging++.h.

const std::string OS::getBashOutput(const char* command) {
#if (ELPP_OS_UNIX && !ELPP_OS_ANDROID && !ELPP_CYGWIN)
  if (command == nullptr) {
    return std::string();
  }
  FILE* proc = nullptr;
  if ((proc = popen(command, "r")) == nullptr) {
    ELPP_INTERNAL_ERROR("\nUnable to run command [" << command << "]", true);
    return std::string();
  }
  char hBuff[4096];
  if (fgets(hBuff, sizeof(hBuff), proc) != nullptr) {
    pclose(proc);
    const std::size_t buffLen = strlen(hBuff);
    if (buffLen > 0 && hBuff[buffLen - 1] == '\n') {
      hBuff[buffLen - 1] = '\0';
    }
    return std::string(hBuff);
  } else {
    pclose(proc);
  }
  return std::string();
#else
  ELPP_UNUSED(command);
  return std::string();
#endif  // (ELPP_OS_UNIX && !ELPP_OS_ANDROID && !ELPP_CYGWIN)
}





Add Discussion

Log in to comment