• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <atomic>
2 #include <sstream>
3 
4 #include <log/log.h>
5 #include <utils/SystemClock.h>
6 
7 namespace android {
8 namespace camera_sensor_listener {
9 namespace {
10 
11 // Based on input filename, generate output filename including
12 // full path and timestamp.
13 // Input:
14 //   input_filename: partial filename, usually log type.
15 // Output:
16 //   output_full_filename: final output filename with full path.
17 // Example:
18 //   input_filename: gyro_direct_log
19 //   output_full_filename:
20 //     /vendor/bin/lib_sensor_listener_test/gyro_direct_log_%h%m%s.txt
GenerateLogFilename(const std::string & input_filename,std::string * output_full_filename)21 bool GenerateLogFilename(const std::string& input_filename,
22                          std::string* output_full_filename) {
23   const char* path = "/vendor/bin/lib_sensor_listener_test/";
24   struct stat directory_stat;
25   memset(&directory_stat, 0, sizeof(directory_stat));
26   if (stat(path, &directory_stat) != 0) {
27     // Create the directory if it doesn't exist.
28     if (errno == ENOENT) {
29       if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
30         ALOGE("%s: %d: createing %s failed. (errno: %d)", __func__, __LINE__,
31               path, errno);
32         return false;
33       }
34     } else {
35       ALOGE("%s: %d: stat(%s) failed: (errno: %d).", __func__, __LINE__, path,
36             errno);
37       return false;
38     }
39   }
40 
41   char timebuf[256] = {0};
42   struct timeval now_time;
43   gettimeofday(&now_time, nullptr);
44   strftime(timebuf, 256, "%H%M%S", localtime(&now_time.tv_sec));
45 
46   std::stringstream ss;
47   ss << path << input_filename << "_" << timebuf << "_" << now_time.tv_usec
48      << ".txt";
49   ss >> *output_full_filename;
50   return true;
51 }
52 
53 }  // namespace
54 }  // namespace camera_sensor_listener
55 }  // namespace android
56