< prev index next >

test/native/logging/logTestUtils.inline.hpp

Print this page
rev 12435 : 8170936: Logging: LogFileOutput.invalid_file_test crashes when executed twice.
Reviewed-by: duke


  42 static inline void delete_file(const char* filename) {
  43   if (!file_exists(filename)) {
  44     return;
  45   }
  46   int ret = remove(filename);
  47   EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "
  48       << os::strerror(errno) << " (" << errno << ")";
  49 }
  50 
  51 static inline void create_directory(const char* name) {
  52   assert(!file_exists(name), "can't create directory: %s already exists", name);
  53   bool failed;
  54 #ifdef _WINDOWS
  55   failed = !CreateDirectory(name, NULL);
  56 #else
  57   failed = mkdir(name, 0777);
  58 #endif
  59   assert(!failed, "failed to create directory %s", name);
  60 }
  61 














  62 static inline void init_log_file(const char* filename, const char* options = "") {
  63   LogStreamHandle(Error, logging) stream;
  64   bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream);
  65   guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options);
  66   log_debug(logging)("%s", LOG_TEST_STRING_LITERAL);
  67   success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream);
  68   guarantee(success, "Failed to disable logging to file '%s'", filename);
  69 }
  70 
  71 // Read a complete line from fp and return it as a resource allocated string.
  72 // Returns NULL on EOF.
  73 static inline char* read_line(FILE* fp) {
  74   assert(fp != NULL, "invalid fp");
  75   int buflen = 512;
  76   char* buf = NEW_RESOURCE_ARRAY(char, buflen);
  77   long pos = ftell(fp);
  78 
  79   char* ret = fgets(buf, buflen, fp);
  80   while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
  81     // retry with a larger buffer




  42 static inline void delete_file(const char* filename) {
  43   if (!file_exists(filename)) {
  44     return;
  45   }
  46   int ret = remove(filename);
  47   EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "
  48       << os::strerror(errno) << " (" << errno << ")";
  49 }
  50 
  51 static inline void create_directory(const char* name) {
  52   assert(!file_exists(name), "can't create directory: %s already exists", name);
  53   bool failed;
  54 #ifdef _WINDOWS
  55   failed = !CreateDirectory(name, NULL);
  56 #else
  57   failed = mkdir(name, 0777);
  58 #endif
  59   assert(!failed, "failed to create directory %s", name);
  60 }
  61 
  62 static inline void delete_empty_directory(const char* name) {
  63 #ifdef _WINDOWS
  64   if (!file_exists(name)) {
  65     return;
  66   }
  67   bool failed;
  68   failed = !RemoveDirectory(name);
  69   EXPECT_FALSE(failed) << "failed to remove directory '" << name
  70           << "': LastError = " << GetLastError();
  71 #else
  72   delete_file(name);
  73 #endif
  74 }
  75 
  76 static inline void init_log_file(const char* filename, const char* options = "") {
  77   LogStreamHandle(Error, logging) stream;
  78   bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream);
  79   guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options);
  80   log_debug(logging)("%s", LOG_TEST_STRING_LITERAL);
  81   success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream);
  82   guarantee(success, "Failed to disable logging to file '%s'", filename);
  83 }
  84 
  85 // Read a complete line from fp and return it as a resource allocated string.
  86 // Returns NULL on EOF.
  87 static inline char* read_line(FILE* fp) {
  88   assert(fp != NULL, "invalid fp");
  89   int buflen = 512;
  90   char* buf = NEW_RESOURCE_ARRAY(char, buflen);
  91   long pos = ftell(fp);
  92 
  93   char* ret = fgets(buf, buflen, fp);
  94   while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
  95     // retry with a larger buffer


< prev index next >